【问题标题】:arduino to android bluetooth: detecting if both are connectedarduino 到 android 蓝牙:检测两者是否已连接
【发布时间】:2014-04-29 06:39:08
【问题描述】:

目标:

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

我正在使用什么:

Bluesmirf silver with arduino uno 和 note 3

到目前为止我做了什么:

[ARDUINO 代码]

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 应用程序连接后发送 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 周前才刚刚开始编程,所以这变得越来越困难。任何帮助将不胜感激。

【问题讨论】:

    标签: android bluetooth connection arduino bluesmirf


    【解决方案1】:

    [更新 #1]

    我已经成功地用这个 sn-p 在这里的 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,我可以使用这个 sn-p 正确读取“h”。

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

    新问题

    我无法判断 arduino 何时失去与 android 应用的连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 2013-02-19
      • 1970-01-01
      • 2018-06-25
      相关资源
      最近更新 更多