Android蓝牙阅读消息没有获得消息的第一个字符

时间:2014-08-13 05:59:29

标签: java android bluetooth

当我尝试实现从蓝牙设备读取数据并进行测试的模块时,它会生成如下消息

enter image description here

原始邮件应为

08-13 13:49:48.961: D/message(21658): [2J
08-13 13:49:48.961: D/message(21658): V00.02
08-13 13:49:48.961: D/message(21658): Initializing us Timer: 2000220us elapsed
08-13 13:49:49.051: D/message(21658): Initializing EEPROM: Found 24C02 first byte: 0x00
08-13 13:49:49.561: D/message(21658): Initizalizing Compass: Compass calibration data found 
08-13 13:49:49.561: D/message(21658): MC5883 found
08-13 13:49:50.181: D/message(21658): Initializing secondary I2C-Bus: Found MPU6050
08-13 13:49:50.401: D/message(21658): ACC calibration data has been loaded TrimNick: 0.000000 TrimRoll: 0.000000
08-13 13:49:50.401: D/message(21658): Initializing IMU: OK!
08-13 13:49:50.401: D/message(21658): Initializing PPM input capture: ppm config data loaded
08-13 13:49:50.411: D/message(21658): Initializing flight control: Flightcontrol OK!
08-13 13:49:50.411: D/message(21658): Initializing motor output:  OK!
08-13 13:49:50.871: D/message(21658): Init Ublox GPS module
08-13 13:49:51.631: D/message(21658): Entering Main Loop

当涉及执行时......它生成如下

Logcat Messsage

08-13 13:49:48.961: D/message(21658): [2J
08-13 13:49:48.961: D/message(21658): 00.02
08-13 13:49:48.961: D/message(21658): nitializing us Timer: 2000220us elapsed
08-13 13:49:49.051: D/message(21658): nitializing EEPROM: Found 24C02 first byte: 0x00
08-13 13:49:49.561: D/message(21658): nitizalizing Compass: Compass calibration data found 
08-13 13:49:49.561: D/message(21658): MC5883 found
08-13 13:49:50.181: D/message(21658): nitializing secondary I2C-Bus: Found MPU6050
08-13 13:49:50.401: D/message(21658): CC calibration data has been loaded TrimNick: 0.000000 TrimRoll: 0.000000
08-13 13:49:50.401: D/message(21658): nitializing IMU: OK!
08-13 13:49:50.401: D/message(21658): nitializing PPM input capture: ppm config data loaded
08-13 13:49:50.411: D/message(21658): nitializing flight control: Flightcontrol OK!
08-13 13:49:50.411: D/message(21658): nitializing motor output:  OK!
08-13 13:49:50.871: D/message(21658): nit Ublox GPS module
08-13 13:49:51.631: D/message(21658): ntering Main Loop

以下是我的代码:

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    private InputStreamReader aReader = null;
    private DataInputStream in  = null;
    public ConnectedThread(BluetoothSocket socket) {
        Log.d(TAG, "create ConnectedThread");



    mmSocket = socket;


    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the BluetoothSocket input and output streams
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
        in = new DataInputStream(tmpIn);
        aReader = new InputStreamReader( in );
        mBufferedReader = new BufferedReader( aReader );

    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}

public void run() {
    Log.i(TAG, "BEGIN mConnectedThread");
    byte[] buffer = new byte[8192];
    int bytes;
    // Keep listening to the InputStream while connected
    while (true) {
        try {
            bytes = mBufferedReader.read();
            Bundle bundle = new Bundle();
            StringBuilder aString = new StringBuilder();
            String line = mBufferedReader.readLine();
            aString.append(line);
            Log.d("message" , aString.toString() );
            bundle.putString(RemoteBluetooth.READ,  aString.toString() );
            Message msg = mHandler.obtainMessage(RemoteBluetooth.MESSAGE_READ ,  bytes, -1, buffer);
            msg.setData(bundle);
            mHandler.sendMessage(msg);

        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost();
            Log.e( TAG, "Could not connect to device", e );
            close( mBufferedReader );
            close( aReader );
            break;
        }
    }
}

我不确定如何将我的阅读代码更正为readLine()的其他方法。以便阅读从蓝牙设备发送的所有完整消息

1 个答案:

答案 0 :(得分:1)

您无法一次性接收所有消息。所以试试这个

while (true) {
    try {
        Bundle bundle = new Bundle();
        String line = mBufferedReader.readLine();
        Log.d("message" , line );
        bundle.putString(RemoteBluetooth.READ, line);
        Message msg=mHandler.obtainMessage(RemoteBluetooth.MESSAGE_READ,line.length(),-1,buffer);
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    } catch (IOException e) {
        Log.e(TAG, "disconnected", e);
        connectionLost();
        Log.e( TAG, "Could not connect to device", e );
        close( mBufferedReader );
        close( aReader );
        break;
    }
}