I have a bluetooth device on a list view, it´s a sensor bug BR-BUTTON-S3A. I´m trying to connect to the device (the server), so when I push a button on this remote device I can receive the sign on my app runnig on Android. Here is my code:
//getting the remote device and calling connect metod
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
BluetoothDevice device =(BluetoothDevice)l.getItemAtPosition(position);
ConnectThread ct = new ConnectThread(device);
ct.run();
}
//connecting to remote device on a new thread
public class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
ParcelUuid[] uuids = device.getUuids();
try {
tmp = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
} catch (IOException e) {
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
mmSocket.connect();
} catch (IOException connectException) {
try {
connectException.printStackTrace();
mmSocket.close();
} catch (IOException closeException) { }
return;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
Toast.makeText(getBaseContext(), "InputStream" + tmpIn.toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "OutputStream" + tmpOut.toString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getBaseContext(), "No se obtuvieron los datos del dispositivo bluetooth", Toast.LENGTH_SHORT).show();
}
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
The problem is when I do mmSocket.connect(), then I get the error "Host is down" with no printable stack trace. I tried to get devices bonded before stablish rf connection but I didn´t work either.
Apreciate any help Thanks!
0 comments:
Post a Comment