2017-05-23 13 views
0

ブルートゥース重量計デバイスに接続するアンドロイドアプリを開発します。このデバイスはデータを送信し、このアプリはこのデータを解析してアプリに表示します。Android:Bluetoothデバイスが「?」のような不明な文字を返す

私の問題は、Bluetoothデバイスからデータを読み取ることです。

デバイスが正常にペア設定され、アンドロイドアプリに接続され、デバイスがデータを正しく送信していますが、このデータを受け取ったときに「 」などの不明な文字があります。

どのようにしてデータを読み込むか解析できますか? Bluetoothデバイスからデータを読み取るには、以下のコードを確認してください。ここ

public DeviceConnectThread(BluetoothSocket socket, Handler mHandler) { 
    this.mHandler = mHandler; 
    mSocket = 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) { 
     Log.d(TAG, "<---------------- I/O stream exceptopn ----------->"); 
     e.printStackTrace(); 
    } 

    mInStream = tmpIn; 
    mOutStream = tmpOut; 
} 

public void run() { 

    byte[] buffer = new byte[1024]; // buffer store for the stream 
    int bytes = 0; // bytes returned from read() 

    // Keep listening to the InputStream until an exception occurs 
    while (true) { 
     try { 
      // Read from the InputStream 
      bytes = mInStream.read(buffer); 


      // Send the obtained bytes to the UI activity 
      mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer) 
        .sendToTarget(); 


     } catch (IOException e) { 
      Log.d(TAG, "Message read exception \n"); 
      e.printStackTrace(); 
      break; 
     } 
    } 

} 

private final Handler mHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     Log.d(TAG, "Handler message >> " + msg); 
     byte[] buf = (byte[]) msg.obj; 

     switch (msg.what) { 

      case Constants.MESSAGE_WRITE: 
       // construct a string from the buffer 
       String writeMessage = new String(buf); 
       Log.i(TAG, "Write Message : " + writeMessage); 
       showMessage("Message Sent : " + writeMessage); 
       break; 
      case Constants.MESSAGE_READ: 
       // construct a string from the valid bytes in the buffer 
       String readMessage = new String(buf, 0, msg.arg1); 

       showMessage("Message Received : " + readMessage); 
       tvReadableData.setText("Readable Data : \n" + readMessage); 
       tvReadableData.setVisibility(View.VISIBLE); 
       MainActivity.tvExp.setVisibility(View.GONE); 
       break; 

いただきました間違った????

+0

Bluetoothデバイスのストリーム内に人間が判読できる文字列は必ずしも存在しません。実際には、デバイスのマニュアルでビットをチェックする必要があります。 –

+0

@MuratK。マニュアルごとにビット範囲がBIT11-BIT0であるかもしれませんが、私は自分のコードでどこに設定する必要があるのか​​分かりませんが、設定してください。私のマニュアルhttp://prntscr.com/fb6h1cのスクリーンショットです –

答えて

0

最後に解決策が見つかりました。

Bluetoothデバイスは "bytes"でデータを返しますので、このバイトを "BITS"(デバイスマニュアルのとおり)に変換する必要があります。

この「BITS」マニュアルをよく読んで別々のデータを読んでください。

bytes = mInStream.read(buffer); 
      Log.d(TAG, "byte >> " + Arrays.toString(buffer)); 
      String bit = HexUtil.byteToBit(buffer); 
      Log.d(TAG, "Bit " + bit); 
      int data = Integer.parseInt(bit.substring(0, 1), 2); 
      Log.d(TAG, "Data = " + data); 



public static String byteToBit(byte[] bytes) { 
    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < Byte.SIZE * bytes.length; i++) { 
     sb.append((bytes[i/Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' 
       : '1'); 
    } 
    return sb.toString(); 
} 
関連する問題