views:

1037

answers:

4

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.

+1  A: 

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");
Brad Hein
Thanks, but I am actually already using the UUID you provided.I was able to get the Bluetooth Chat app to work, but only if I connect from my device (computer) to the phone, and not the other way around. If I try to connect from the phone to the computer (from within the application menu) it will try for a few seconds, then Toast an error like "Connection failed" or something.
mxrider
Did you explicitly add an RFCOMM service on your computer? Otherwise the computer has no reason to respond to RFCOMM requests. Just as your computer wouldn't respond to FTP requests unless you installed an FTP server and opened port 21.
Brad Hein
I'm confused - are u trying to make the bluetooth chat work between two phones or between phone and PC? I think only the former makes sense but i may not be understanding your ultimate goal. Can you clarify?
Brad Hein
I don't have two Android phones to test this on, but I have been testing with two PCs (using PuTTY on windows and Terminal on mac) as well as a special headset device that uses SPP. It wasn't working as a client on any of them (only as a server). I have fixed the issue now, as indicated by my post.
mxrider
A: 

Does the Bluetooth chat app works between a phone and PC (or any phone other than andorid)?

Garima Srivastava
It would work, but only if the PC established the connection -- thus making my phone the server. I wanted to have the phone be the one connecting out (thus acting as the client).
mxrider
has anyone got any code for the pc side of things. or am i missing the obvious?
Mark
+1  A: 

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.

mxrider
A: 

I have experienced the exact same problem, see http://stackoverflow.com/questions/3255406/trouble-connecting-android-bluetooth-client-socket-to-ubuntu-server-socket

You state that not calling listenUsingRfcommWithServiceRecord solved your problem, however in my application I did not ever call this (see my post). I would appreaciate it highly if anyone could point me towards a solution ?

UlrikK