5

I'm trying to write a code that only connect to my (for now) only paired device on my Nexus 7 running Android 4.4 KitKat. No matter how many thing I have tried, I still get this error. This is the last code I have tried, which seems to be doing everything I've seen people report as successful.

Can anybody point me to what I'm doing wrong?

BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter adapter = bluetoothManager.getAdapter();//BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
BluetoothDevice bt = adapter.getBondedDevices().iterator().next();
BluetoothDevice actual = adapter.getRemoteDevice(bt.getAddress());
String str = "";
for(BluetoothDevice bd : adapter.getBondedDevices()) {
    str += bd.getName() + "\n";
}
str+= actual;

textView.setText(str);
BluetoothSocket socket = actual.createInsecureRfcommSocketToServiceRecord(MY_UUID);
adapter.cancelDiscovery();
socket.connect();
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print(message);
out.flush();
chengbo
  • 5,789
  • 4
  • 27
  • 41
Mr. Adobo
  • 815
  • 1
  • 12
  • 24
  • 1
    Is that exactly what the exception says? Somehow I doubt it. Please provide the actual text. – user207421 Nov 17 '13 at 08:52
  • 3
    That is exactly what is says, including the bad english. – Mr. Adobo Nov 17 '13 at 08:55
  • 1
    Somehow I got it to work but I do not know how to do this properly. By getting my socket using `BluetoothSocket socket = actual.createInsecureRfcommSocketToServiceRecord(actual.getUuids()[index].getUuid());` and manually hardcoding values for index, I managed to find an UUID that matched. The rest threw exceptions. Not sure what is the correct way to get the UUID I actually need. – Mr. Adobo Nov 17 '13 at 09:00

2 Answers2

1

I had the exactly same error (IOException read failed socket might closed or timeout) with an ELM327 OBD bluetooth adapter.

UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
btDevice = btAdapter.getRemoteDevice(btDeviceAddress);
btAdapter.cancelDiscovery();
btSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();

The ELM327 would allow the first connection and send/receive correctly, but only once. Then, all later connections would fail with the IOException. To get it working again, I had to unpair the ELM327 from the OS, and then I'd be able to connect - just once! And the cycle repeats ...

The problem was solved as described in the post above - create an INSECURE socket.

btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Richard
  • 11
  • 1
0

You can get UUID for Android < 3 by this:

Method method = device.getClass().getMethod("getUuids", null);
ParcelUuid[] phoneUuids = (ParcelUuid[]) method.invoke(device, null);
SERIAL_UUID = phoneUuids[1].getUuid();

Or:

UUID SERIAL_UUID = device.getUuids()[0].getUuid();
Martin Koubek
  • 433
  • 4
  • 8
  • This is wrong: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { (has extras) }} to activity {de.myProject.activities.MainActivity}: java.lang.NullPointerException: Attempt to read from null array – Mulgard Feb 07 '15 at 13:47
  • getUuids() can return null, which you must take into account. – Shahar Sep 19 '15 at 23:52