gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:39:14 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com