2011-02-19 13 views
2

Bluetoothプログラマブルマイクロコントローラと通信しようとしています。マイクロコントローラのBluetoothデバイスは、BluetoothシリアルCOMポート番号4で(具体的に)通信します。Android:Bluetoothシリアル(Comポート)のAndroid端末との通信

質問:このCOMポート(番号4)からデータを読み取るには、Androidアプリケーションを取得するにはどうすればよいですか?

私はUUIDがよく知られているユニークなIDであることを知っていますが、このデバイスでは機能しますが、COMポートの指定とは何の関係もないと思います。

static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
btSocket = btDevice.createRfcommSocketToServiceRecord(myUUID); 
btSocket.connect(); 
valid.append(btDevice.getName() + "\n" + btDevice.getAddress()); 
north.append("Socket Connected"); 
InputStream mmInStream = btSocket.getInputStream(); 
OutputStream mmOutStream = btSocket.getOutputStream(); 
byte[] buffer = new byte[10]; 
int bytes; 
StringBuffer str = new StringBuffer(); 
while (true)       {        
    try { 
    mmOutStream.write("a".getBytes()); 

     //Reads a # of bytes until the end of stream is reached 
     bytes = mmInStream.read(buffer); 
     //Transform to string 
       str.append(buffer.toString()+"\t");       //Clear the buffer 
     Log.e("DATA", "THE DATA: "+ str.toString()); 
     south.setText(str.toString()); 
     str.delete(0,str.length()); 
     } catch (IOException e) { 
     break; 
} }} 

答えて

1

私が作成したカスタムBluetoothデバイスでこの問題が発生しました。私mmDeviceがあなたのbtDeviceが

public ConnectThread(BluetoothDevice device) throws 
     SecurityException, NoSuchMethodException, IllegalArgumentException, 
      IllegalAccessException, InvocationTargetException { 
      mmDevice = device; 
      BluetoothSocket tmp = null; 

      // Force a BluetoothSocket for a connection with the 
      // given BluetoothDevice 

      Method m = mmDevice.getClass().getMethod("createRfcommSocket", 
          new Class[]{int.class}); 

     mmSocket = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1)); 
    } 

です:代わりに、あなたの接続のスレッドでcreateRfcommSocketToServiceRecordを使用するには、次のようなものを試してみてください。

これは、未知のデバイスとスマートフォンとの間のソケット接続を強制します。私が聞いたことから、Androidには「類似していない」デバイスを接続する際に問題があります。試してみる価値。

3

COMポートは、マイクロコントローラにのみ存在し、接続されているBluetoothデバイスではないものです。 Bluetoothデバイスは、マイクロコントローラがどのCOMポートに接続するのに使用されたかを知らない。 Bluetoothデバイスのマイクロへの接続は、TXおよびRXラインを介して行われます。特定のCOMポートに割り当てられたマイクロのピンに接続されているという事実は、Bluetoothデバイスにとっては無関係であり、未知です。