0

私はこの例に従いました: http://english.cxem.net/arduino/arduino5.php

sparkfun Bluetoothデバイスを使用してarduino unoボードをセットアップしました。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) に Bluetooth チップが接続されています。これは、シリアル モニターで使用されるピンと同じです。Bluetooth チップをこれらに接続すると、シリアル モニタは引き続きデータを受信しますが、送信できなくなります。したがって、問題はArduino側にあります。

また、このStackOverflow の回答では、ピン 0 と 1 でシリアル モニターと Bluetooth の両方を同時に使用することはできないと述べています。

それでもシリアル モニタを使用する場合は、Bluetooth チップを別のデジタル ピンに接続し、ここで説明されているように SoftwareSerial Arduino ライブラリを使用します。

この投稿で説明されているように、Bluetooth チャット サンプルに簡単な変更を加えましたが、私の Android は Arduino からの受信に問題はありません。

于 2013-01-24T03:32:08.257 に答える