Android : Android Bluetooth reading message not getting the first character of the message

on Tuesday, August 12, 2014


When i am trying to implement the module of reading data from the bluetooth device and test, it comes to generating the message as follows


enter image description here


The original message should be



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


When it comes to the execution ... it generate as follows


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


The below is my code :



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


I am not sure how my reading code can be corrected to other ways to readLine(). in order to read all the complete messages sent from the Bluetooth device


0 comments:

Post a Comment