0

我遵循了这个例子:http ://english.cxem.net/arduino/arduino5.php

我有一个 arduino uno 板,上面安装了一个 sparkfun 蓝牙设备。我可以将数据从 android 连接并发送到 arduino,我看到这些数据在串行监视器中弹出,但我无法从 arduino(串行监视器)发送数据并返回到 Android。

我正在使用在活动的 onResume 方法中启动的 ConnectThread。这是我的线程的代码:

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d("THREAD", "constructor");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.d("THREAD", "inside run" );
            byte[] buffer = new byte[256];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                Log.d("in loop", "waiting for data");
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                    h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
                    Log.d("recieve", "b " + bytes);
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(String message) {
            Log.d("TAG", "...Data to send: " + message + "...");
            byte[] msgBuffer = message.getBytes();
            try {
                mmOutStream.write(msgBuffer);
            } catch (IOException e) {
                Log.d("TAG", "...Error data send: " + e.getMessage() + "...");     
              }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }



    }

我是否需要使用始终等待数据的服务,因为串行监视器仅在我按发送时发送数据?

编辑:Arduino代码:

    #include <SoftwareSerial.h>  

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

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("$$$");  // 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
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

   void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    char c = (char) Serial.read();
     // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print(c); 
  }
  // and loop forever and ever!
}
4

1 回答 1

1

不,您不需要服务,线程应该可以正常工作。此代码实际上与通过 Android SDK 提供的 BluetoothChat 示例中提供的代码相同,因此它应该可以工作。

通过使用串行监视器,您正在向 Arduino 发送数据,所以我假设您正在使用 Serial.println("data string") 或类似的东西回显该数据。但是,由于您阅读了您引用的文章,因此您将蓝牙芯片连接到 Arduino 上的 RX/TX 引脚(0 和 1),这与串行监视器使用的引脚相同。我发现当蓝牙芯片连接到这些时,串口监视器仍然可以接收数据,但不能再发送数据。所以你的问题在Arduino方面。

此外,这个StackOverflow 答案提到您不能同时在引脚 0 和 1 上使用串行监视器和蓝牙。

如果您仍想使用串行监视器,请将蓝牙芯片连接到不同的数字引脚并使用 SoftwareSerial Arduino 库,如此所述。

如本文所述,我对蓝牙聊天示例进行了简单修改,并且我的 Android 从 Arduino 接收没有问题。

于 2013-01-24T03:32:08.257 回答