【问题标题】:Does not get data from Arduino - bluetooth to Android Device不从 Arduino 获取数据 - 蓝牙到 Android 设备
【发布时间】:2016-06-27 08:51:54
【问题描述】:

我想通过蓝牙和 Android 设备进行 Arduino 之间的通信

在 Arduino 中,我正在使用

 SoftwareSerial.h 

下面是代码

#include <SoftwareSerial.h>
int ledPin1 = 5;
int state = 0;
int flag = 0;
SoftwareSerial mySerial(0, 1);
void setup() {
  // put your setup code here, to run once:
pinMode(ledPin1, OUTPUT);

digitalWrite(ledPin1, LOW);

Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    if(Serial.available()>0){
      state = Serial.read();
      flag = 0;
    }
    if (mySerial.available()) {
      int k = mySerial.read();
      mySerial.write(k);
    }
}

在 Android 方面,我使用的是BluetoothSPPLibrary

有一个名为 BluetoothService.java 的类

因为有ConnectedThread

 public void run()
        {
            Log.d(TAG, "run: Called");
            byte[] buffer;
            ArrayList<Integer> arr_byte = new ArrayList<Integer>();

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    int data = mmInStream.read();
                    Log.d(TAG, "run: "+data);
                    if(data == 0x0A) { 
                    } else if(data == 0x0D) {
                        buffer = new byte[arr_byte.size()];
                        for(int i = 0 ; i < arr_byte.size() ; i++) {
                            buffer[i] = arr_byte.get(i).byteValue();
                        }
                        // Send the obtained bytes to the UI Activity
                        mHandler.obtainMessage(BluetoothState.MESSAGE_READ
                                , buffer.length, -1, buffer).sendToTarget();
                        arr_byte = new ArrayList<Integer>();
                    } else {
                        arr_byte.add(data);
                    }
                } catch (IOException e) {
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothService.this.start(BluetoothService.this.isAndroid);
                    break;
                }
            }
        }

但是

 int data = mmInStream.read(); 

这里的数据不是来自上面的代码

【问题讨论】:

    标签: android bluetooth arduino


    【解决方案1】:

    您在这里遗漏了一些信息。您使用的是哪个 Arduino 板?基于您尝试使用串行连接发送蓝牙的事实,我假设您有一个外部蓝牙芯片/板,它是什么?如果您使用的是一个,您绝对确定它的接线正确。是使用蓝牙还是蓝牙低功耗,两者有非常不同的协议。这些信息会有所帮助。

    【讨论】:

    • 这对 OP 的问题有什么帮助?
    • 这很有帮助,因为蓝牙和蓝牙LE之间的通信协议有很大不同,因此在android端接收信息会有所不同。另外,如果不知道他使用的是哪种蓝牙芯片,我们怎么能说 Arduino 代码是准确的呢?另外,如果他没有正确连接芯片怎么办?
    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多