6
votes

I am developing an application which uses Bluetooth to connect to a device and send/receive data. I am doing all of my testing with a Nexus One phone.

I have never been able to establish a SPP (serial port) connection from my phone to any device. However, I have been able to connect from a device (my laptop) to my phone using a Mac equivalent of PuTTY (The only exception to this is the "Bluetooth File Transfer" app from the Marketplace seems to work, but I don't think that uses RFCOM/SPP...).

I keep seeing this message in my LogCat logs:

ERROR/BluetoothService.cpp(78): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)

as well as these:

java.io.IOException: Operation Canceled
java.io.IOException: Software caused connection abort

I have tried using the UUID of "00001101-0000-1000-8000-00805F9B34FB" and I have also tried using the:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));

method instead of device.createRfcommSocketToServiceRecord(UUID); as well--with no luck.

I am using the BluetoothChat example and variations of that code to do all of my testing...

Solutions or suggestions would be great...or even a better/less complex example of some testing code I can run on the phone, or a python script or something I can run on my computer to help debug?

Thanks! I hope this isn't a bug with the Android OS, but if it is I hope to find a workaround.


EDIT: I should also note that most devices show up as "paired, but not connected" in the Bluetooth settings.


EDIT 2: The solution seems to be simply disabling any Bluetooth listening. See my answer post for more information.

2
did you use reflection with intent or by coincidence? The reason I ask is, I'm wondering if you used it as a way to work around a particular bug in the Bluetooth implementation? Ordinarily I would think one could have just used BluetoothSocket bs = createRfCommSocket(.... Thanks!Brad Hein
@Brad - It was intentional, because I have seen several people online having issues with createRfcommSocketToServiceRecord which were only resolved by using this unusual workaround. See: stackoverflow.com/questions/2660968/… --and-- stackoverflow.com/questions/2661932/…idolize

2 Answers

7
votes

The solution, as it turns out, was to disable the server functionality of the Bluetooth service. By only using createRfcommSocketToServiceRecord and never calling listenUsingRfcommWithServiceRecord (in the BluetoothChat example this means never starting the "AcceptThread") the problem was fixed.

Even though these two calls are supposed to be totally separated and have no affect on each other (according to the Android docs), simply commenting out listenUsingRfcommWithServiceRecord fixed my supposedly unrelated issue.

I can take the Bluetooth Chat program unedited and it will not be able to establish an outgoing connection to ANY bluetooth device I have tested (laptops, desktops, headsets, etc.), but if I remove that one thing it works flawlessly as a client.

Anyway, I hope this will help someone else if they come across the same issue. This must be a bug with the Android OS, or possibly the firmware on the Nexus One.

1
votes

I would ignore the stopDiscovery error - its good that you're cancelling discovery before making your connection. Per the SDK docs:

Because discovery is a heavyweight precedure for the Bluetooth adapter, this method should always be called before attempting to connect to a remote device with connect(). Discovery is not managed by the Activity, but is run as a system service, so an application should always call cancel discovery even if it did not directly request a discovery, just to be sure.

So with that said, were you able to get the Bluetooth Chat example to work before you made any modifications to the code?

The UUID you want for SPP/RFCOMM is:

static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);

or defined another way (both accomplish the same thing).

static final UUID UUID_RFCOMM_GENERIC = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");