I have tried to connect two devices by using the Android Bluetooth Chat example on the Android developer site. It\'s only connecting from one of the devices and when I am tryi
I also think your problem is not within your code.
Two Years ago I have modified the BluetoothChat example application. You can find the modified project here - Bluetooth Chat and the binary here: Bluetooth Chat
If this works better between your two devices it might help.
I tested it just now on Android version 2.3.3 (Nexus One) and on Version 4.0.4 (Galaxy Nexus).
I have done this,and i think you get connection lost when the device discoverable change. Please do check the device discoverable in both,you can enable it by code.
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
startActivity(discoverableIntent);
I'm not sure if just copy the code straight away and try to run it if so, bear in mind android.app.ActionBar
; require version 3.0. Also Bluetooth communication is not exactly peer to peer it's Master Slave.
Saurabh, make this change please and watch it work:
if (Build.VERSION.SDK_INT < 9) { // VK: Build.Version_Codes.GINGERBREAD is not accessible yet so using raw int value
// VK: 9 is the API Level integer value for Gingerbread
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
Method m = null;
try {
m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tmp = (BluetoothSocket) m.invoke(device, (UUID) MY_UUID);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}