- Forum-Beiträge: 17
14.03.2012, 16:48:59 via Website
14.03.2012 16:48:59 via Website
i implemented an app that connects to an bluetooth device and collects data from the device.
The device sends data all 100 ms and faster. Most of the time all data received is correct but sometimes it is corrupted(the end of the data is copied in the middle of the data...) i tryed a lot and recogniced that this problem comes up more often when i'm debuging und the device.
doese someone know whats happening there?
my code:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private String mSocketType;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
//ParcelUuid[] uuids = servicesFromDevice(device);
Method m = null;
try {
m = device.getClass().getMethod("createInsecureRfcommSocket",
new Class[] { int.class });
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tmp = (BluetoothSocket)m.invoke(device, Integer.valueOf(1));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* try {
tmp = device.createRfcommSocketToServiceRecord(my_uuid);
//Log.d(TAG, "uuids.length: "+ uuids.length);
//Log.d(TAG, "uuid: " + uuids[1].getUuid());
//tmp = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + "create() failed",e);
}*/
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread SocketType:" );
setName("ConnectThread" );
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
run=true;
} catch (IOException e) {
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() " +
" socket during connection failure", e2);
}
Log.e(TAG, e.getMessage());
connectionFailed();
return;
}
// Reset the ConnectThread because we're done
synchronized (connection.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect " + " socket failed", e);
}
}
}
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
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();
} 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[1024];
int bytes;
int cnt=0;
String help="";
// Keep listening to the InputStream while connected
Log.i(TAG, "ConnectedThread run= "+ run);
while (run) {
try {
cnt++;
//Log.i(TAG, ""+cnt);
//Log.i(TAG, "Read from InputStream");
// Read from the InputStream
bytes = mmInStream.read(buffer);
Log.i(TAG, "data received: "+ bytes+ " bytes");
// Send the obtained bytes to the UI Activity
//recBytes=bytes;
//recData=buffer;
/*for (int i=0;i<bytes;i++)
{
help=help+" "+(int)buffer[i];
}
Log.i(TAG, help);
help="";*/
//Log.i(TAG,"timestamp: "+ ((((int)buffer[3]&0xFF)<<8)+((int)buffer[4]&0xFF)));
//mHandler.obtainMessage(main.MESSAGE_READ, 0, -1, "")
//.sendToTarget();
mHandler.obtainMessage(main.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
//connectionLost();
// Start the service over to restart listening mode
//connection.this.start();
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
/* mHandler.obtainMessage(main.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();*/
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
}
would be nice if someone can help me =)
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.