java - Android蓝牙读取消息未获取消息的第一个字符

标签 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 消息

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() 方法。为了读取从蓝牙设备发送的所有完整消息

最佳答案

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

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;
    }
}

关于java - Android蓝牙读取消息未获取消息的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25279029/

相关文章:

java - MySql 表列中未设置默认值

android - android : battery being drained in background 中的 react-native 应用程序

android - 如何停止程序本身?

java - 根据用户输入动态更改应用程序名称

iphone - iOS 上的蓝牙

iphone - 2 台 iOS 设备之间的蓝牙连接

c# - 是否有 Windows 事件来查明蓝牙设备是否正在配对

java - 如何让Activiti Explorer中的Activiti工作流程调用外部Java程序

java - 在Java中创建自定义类,id是保留的还是可以在自己的类中使用id?

java - 加载一个java项目