arduino到android蓝牙:检测两者是否连接

arduino到android蓝牙:检测两者是否连接,android,bluetooth,connection,arduino,bluesmirf,Android,Bluetooth,Connection,Arduino,Bluesmirf,目标: 让ardiuno检查是否通过蓝牙连接到android。然后,如果已连接,则执行操作;如果未连接,则重新连接 private void sendMessage(BluetoothSocket socket, String msg) { OutputStream outStream; try { outStream = socket.getOutputStream(); byte[] byteString = (msg).getBytes();

目标:

让ardiuno检查是否通过蓝牙连接到android。然后,如果已连接,则执行操作;如果未连接,则重新连接

private void sendMessage(BluetoothSocket socket, String msg) {
    OutputStream outStream;
    try {
        outStream = socket.getOutputStream();
        byte[] byteString = (msg).getBytes();
        outStream.write(byteString);
    } catch (IOException e) {
        Log.d("BLUETOOTH_COMMS", e.getMessage());
    }
}
我正在使用的内容:

带arduino uno和音符3的Bluesmirf银币

到目前为止我所做的:

[阿杜伊诺代码]

Bluesmirf处于主模式自动连接。arduino应该检查android应用程序是否发送H字符。如果是,则表示其已连接。如果没有,则需要继续重新连接

#include <SoftwareSerial.h>  
#include <TextFinder.h>

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3
boolean running = false;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{

Serial.begin(9600);             // Begin the serial monitor at 9600bps

bluetooth.begin(115200);        // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$");           // Print three times individually
bluetooth.print("$");
bluetooth.print("$");           // Enter command mode
delay(100);                     // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
delay(100);
bluetooth.begin(9600);          // Start bluetooth serial at 9600

}

void loop()
{


//Check If Connected

if(bluetooth.available())  // If the bluetooth sent any characters
{
  //Check if bluetooth recieved an H and store in a value
  char val = bluetooth.read();

  if(val == 'H')
  {
       running = true;
  }
  else if(val != 'H')
  {
       running = false;
  }
}
else if(!bluetooth.available())
{
   running = false;
}

//Actions to perform if arduino is connected or not connected

if(running == true)
{
//It's connected so wait 5 seconds
delay(5000);
}
else if(running == false)
{
//It's not connected: Attempt to reconnect
bluetooth.print("$");  // Print three times individually
bluetooth.print("$");
bluetooth.print("$");  // Enter command mode
delay(100);  // Short delay, wait for the Mate to send back CMD
bluetooth.println("C,30196692D7C0");
delay(500);
bluetooth.println("---"); 
delay(3000);

}
}
旁注:

我尝试了很多方法让arduino检查它是否连接。我3周前才开始编程,所以这变得越来越困难。任何帮助都将不胜感激。

[UPDATE#1]

我已经成功地用android应用程序发送了一个“h”,其中包含以下代码片段:

//call send method to send this character over bluetooth
sendMessage(socket,"h");

//Method used to send 'h' over bluetooth
    private void sendMessage(BluetoothSocket socket, String msg) {
        OutputStream outStream;
        try {
            outStream = socket.getOutputStream();
            //byte[] byteString = (msg).getBytes();
            byte[] byteString = stringToBytesUTFCustom(msg);
            outStream.write(byteString);
        } catch (IOException e) {
            Log.d("BLUETOOTH_COMMS", e.getMessage());
        }
    }

//Method used to convert
public byte[] stringToBytesUTFCustom(String str) {
    char[] buffer = str.toCharArray();
    byte[] b = new byte[buffer.length << 1];
    for (int i = 0; i < buffer.length; i++) {
        int bpos = i << 1;
        b[bpos] = (byte) ((buffer[i]&0xFF00)>>8);
        b[bpos + 1] = (byte) (buffer[i]&0x00FF);
    }
    return b;
}
新问题

我很难判断arduino何时与android应用程序失去连接

  if (bluetooth.available() > 0) {  // if the data came
    char incomingByte = bluetooth.read(); // read byte
    if(incomingByte == 'h') {
       running = true;
  }
  }