我已经创建了一个Android应用程序,可以使用蓝牙与我的Arduino进行通信.但是当我从我的Android设备向Arduino发送数据时,Arduino没有响应我发送的内容.我可以从我的Android设备连接到我的Arduino.所以这不是问题.
这是我的Android完整脚本.
package nl.handoko.LumaMini; import java.io.IOException; import java.io.OutputStream; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "LumaMini"; private static final int REQUEST_ENABLE_BT = 1; private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private OutputStream outStream = null; Button fourty, thirty, twenty, twelve, automatic, manual; TextView message; // Well known SPP UUID private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // Server's MAC address private static String address = "98:D3:31:30:09:43"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "In onCreate()"); setContentView(R.layout.activity_mainnn); fourty = (Button) findViewById(R.id.button1); thirty = (Button) findViewById(R.id.button4); twenty = (Button) findViewById(R.id.button2); twelve = (Button) findViewById(R.id.button5); automatic = (Button) findViewById(R.id.button3); manual = (Button) findViewById(R.id.button6); message = (TextView) findViewById(R.id.textView1); fourty.setText("40 Leds"); thirty.setText("30 Leds"); twenty.setText("20 Leds"); twelve.setText("12 Leds"); automatic.setText("Automatic"); manual.setText("Manual"); message.setText("Using this app you can take full control of the Luma Mini!" + "When it's running on automatic please switch back to manual first before switching to other versions."); btAdapter = BluetoothAdapter.getDefaultAdapter(); checkBTState(); fourty.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendData("a"); Toast msg = Toast.makeText(getBaseContext(), "40 Leds version", Toast.LENGTH_SHORT); msg.show(); } }); thirty.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendData("b"); Toast msg = Toast.makeText(getBaseContext(), "30 Leds version", Toast.LENGTH_SHORT); msg.show(); } }); twenty.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendData("c"); Toast msg = Toast.makeText(getBaseContext(), "20 Leds version", Toast.LENGTH_SHORT); msg.show(); } }); twelve.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendData("d"); Toast msg = Toast.makeText(getBaseContext(), "12 Leds version", Toast.LENGTH_SHORT); msg.show(); } }); automatic.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendData("e"); Toast msg = Toast.makeText(getBaseContext(), "Run automatically", Toast.LENGTH_SHORT); msg.show(); } }); manual.setOnClickListener(new OnClickListener() { public void onClick(View v) { sendData("f"); Toast msg = Toast.makeText(getBaseContext(), "Manually switch Leds", Toast.LENGTH_SHORT); msg.show(); } }); } @Override public void onResume() { super.onResume(); Log.d(TAG, "...In onResume - Attempting client connect..."); // Set up a pointer to the remote node using it's address. BluetoothDevice device = btAdapter.getRemoteDevice(address); // Two things are needed to make a connection: // A MAC address, which we got above. // A Service ID or UUID. In this case we are using the // UUID for SPP. try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + "."); } // Discovery is resource intensive. Make sure it isn't going on // when you attempt to connect and pass your message. btAdapter.cancelDiscovery(); // Establish the connection. This will block until it connects. Log.d(TAG, "...Connecting to Remote..."); try { btSocket.connect(); Log.d(TAG, "...Connection established and data link opened..."); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); } } // Create a data stream so we can talk to server. Log.d(TAG, "...Creating Socket..."); try { outStream = btSocket.getOutputStream(); } catch (IOException e) { errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + "."); } } @Override public void onPause() { super.onPause(); Log.d(TAG, "...In onPause()..."); if (outStream != null) { try { outStream.flush(); } catch (IOException e) { errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + "."); } } try { btSocket.close(); } catch (IOException e2) { errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); } } private void checkBTState() { // Check for Bluetooth support and then check to make sure it is turned on // Emulator doesn't support Bluetooth and will return null if(btAdapter==null) { errorExit("Fatal Error", "Bluetooth Not supported. Aborting."); } else { if (btAdapter.isEnabled()) { Log.d(TAG, "...Bluetooth is enabled..."); } else { //Prompt user to turn on Bluetooth Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } } private void errorExit(String title, String message){ Toast msg = Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_SHORT); msg.show(); finish(); } private void sendData(String message) { byte[] msgBuffer = message.getBytes(); Log.d(TAG, "...Sending data: " + message + "..."); try { outStream.write(msgBuffer); } catch (IOException e) { String msg = "In onResume() and an exception occurred during write: " + e.getMessage(); msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n"; errorExit("Fatal Error", msg); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
这是我的Arduino脚本:
#include#include #define Rx 2 #define Tx 3 #define seconds 4 char serialData; // Serial data memory int counter1; // Overflow memory int action; // Action trigger int once = 0; // Run only once SoftwareSerial Bluetooth(Rx, Tx); void setup(){ pinMode(Tx, OUTPUT); // Configure Tx as OUTPUT (Transmitter) pinMode(Rx, INPUT); delay(1000); Bluetooth.begin(9600); Serial.begin(1200); delay(1000); Serial.print("Bluetooth ready"); Bluetooth.flush(); TimerOneSetup(); // Run the setup for Timer One for(int i = 5; i <= 8; i++){ // Make pins 5, 6, 7 and 8 OUTPUT pinMode(i, OUTPUT); } } void interrupt1(){ // Timer One loop counter1++; // Count the amount of seconds has passed if (counter1 == seconds){ // Trigger the next action after a several amount of seconds (Default: 4 seconds) action++; counter1 = 0; } if (action > 3){ // Reset action trigger when after all actions were runned action = 0; } } void loop(){ // Endless loop if (Serial.available()){ // Wait for data recieved from Local device serialData = Serial.read(); // Put recieved data in memory Serial.print("Data recieved from Local device: "); Serial.println(serialData); } if (Bluetooth.available()){ // Wait for data recieved from Bluetooth device serialData = Bluetooth.read(); // Put recieved data in memory Serial.print("Data recieved from Bluetooth device: "); Serial.print(serialData); } if (once == 0){ // This script will be run only once serialData = 'e'; // Put switch on automatic on startup once++; // Get into the next stage which may be run only once } switch(serialData){ // Perform action on state of the switch case 'a': fourtyLeds(); // Show the 40 Leds version of the Luma Mini break; case 'b': thirtyLeds(); // Show the 30 Leds version of the Luma Mini break; case 'c': twentyLeds(); // Show the 20 Leds version of the Luma Mini break; case 'd': twelveLeds(); // Show the 12 Leds version of the Luma Mini break; case 'e': while(serialData == 'e'){ // Keep changing different Led versions of the Luma Mini automatically switch(action){ case 0: // Wait for the action trigger to hit the first action fourtyLeds(); // Show the 40 Leds version of the Luma Mini break; case 1: // Wait for the action trigger to hit the second action twelveLeds(); // Show the 12 Leds version of the Luma Mini break; case 2: // Wait for the action trigger to hit the third action twentyLeds(); // Show the 20 Leds version of the Luma Mini break; case 4: // Wait for the action trigger to hit the fourth action thirtyLeds(); // Show the 30 Leds version of the Luma Mini break;} if (Serial.read() == 'f'){ // Wait for data recieved from Local device serialData = Serial.read(); // Put recieved data in memory Serial.print("Data recieved from Local device: "); Serial.println(serialData); } if (Bluetooth.read() == 'f'){ // Wait for data recieved from Bluetooth device serialData = Bluetooth.read(); // Put recieved data in memory Serial.print("Data recieved from Bluetooth device: "); Serial.println(serialData); } break; } } } void BluetoothSetup(){ pinMode(Tx, OUTPUT); // Configure Tx as OUTPUT (Transmitter) pinMode(Rx, INPUT); // Configure Rx as INPUT (Reciever) Bluetooth.begin(9600); // Set Bluetooth baud rate to default baud rate 38400 Bluetooth.print("\r\n+STWMOD=0\r\n"); // Set the Bluetooth to work in slave mode Bluetooth.print("\r\n+STNA=Luma Mini\r\n"); // Set Bluetooth name to Luma Mini Bluetooth.print("\r\n+STOAUT=1\r\n"); // Permit paired device to connect Bluetooth.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here delay(2000); Bluetooth.print("\r\n+INQ=1\r\n"); // Make this Bluetooth Slave inquirable Serial.println("The slave Bluetooth is inquirable!"); delay(2000); Bluetooth.flush(); } void TimerOneSetup(){ Timer1.initialize(1000000); // Initialize Timer One for an overflow exactly every 1 second Timer1.attachInterrupt(interrupt1); // Open the Timer One loop } void fourtyLeds(){ // Show the 40 Leds version of the Luma Mini for(int i = 5; i <= 8; i++){ digitalWrite(i, LOW); } } void thirtyLeds(){ // Show the 30 Leds version of the Luma Mini digitalWrite(5, HIGH); digitalWrite(6, LOW); digitalWrite(7, LOW); digitalWrite(8, LOW); } void twentyLeds(){ // Show the 20 Leds version of the Luma Mini digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(7, LOW); digitalWrite(8, HIGH); } void twelveLeds(){ // Show the 12 Leds version of the Luma Mini for (int i = 5; i <= 8; i++){ digitalWrite(i, HIGH); } }
我的问题:如何将Android数据中的CHAR数据发送到我的Arduino?在我的脚本上编辑什么?