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

时间:2014-03-21 18:15:02

标签: android bluetooth connection arduino bluesmirf

目标:

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

我正在使用的内容:

Bluesmirf银与arduino uno和注3

到目前为止我做了什么:

[ARDUINO CODE]

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);

}
}

[ANDROID CODE]

这是Android应用程序的一种方法,一旦应用程序连接就会发送一个H.

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检查它是否连接。我刚刚开始编程3周前,所以这变得越来越困难。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

[更新#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,我可以使用此片段正确读取'h'。

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

新问题

我无法确定arduino何时与Android应用程序失去联系。