Android:蓝牙设备返回未知字符,如"?"

时间:2017-05-23 10:44:56

标签: android bluetooth

我们开发Android应用程序以连接蓝牙体重秤设备,此设备发送数据和我们的应用程序解析此数据并在应用程序中显示。

所以我的问题是从蓝牙设备读取数据。

设备已成功配对并与Android应用程序连接,并且设备正确发送数据但当我收到此数据时,未知字符如"��"。

那我怎样才能读取或解析数据?请检查以下代码以从蓝牙设备读取数据。

 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;

这里错了????

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案。

蓝牙设备以“字节”返回数据,因此我们需要在“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();
}