4

I have been playing around with the bluetooth API for Android 2.2 (API level 8, HTC Desire) and had an app connecting to an embedded Bluetooth device using:

device.createRfcommSocketToServiceRecord(DEV_UUID);

This generated a pairing request as expected, however to streamline the connection process I wanted to avoid the user interaction when pairing so moved to API level 10 (HTC Desire with CyanogenMod 7) so I could use:

 device.createInsecureRfcommSocketToServiceRecord(DEV_UUID);

When testing this also works as expected (connecting without prompting the user to pair), however when I try to create the secure RfcommSocket under API level 10 as before with 2.2 I get a connection refused exception...

 java.io.IOException: Connection refused
    at android.bluetooth.BluetoothSocket.connectNative(Native Method)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)

As far as I can tell this should still work in the same way, prompting the user to pair?

EDIT:

Just tried again using the following code and the outcome is the same (working for insecure but not for secure), I will try and get my hands on a stock 2.3 device to test on.

        try {
            Method m = dev.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class } );
            BluetoothSocket bs = (BluetoothSocket)m.invoke(dev, devUUID);
            Log.d("TEST", "Method Invoked");
            bs.connect();
            Log.d("TEST", "Connected to socket");
            bs.close();
            Log.d("TEST", "Closed Socket");
        }
DarkRyuu
  • 169
  • 2
  • 3
  • 13
  • This may be unrelated, but have you seen this? http://stackoverflow.com/questions/3353080/android-2-1-htc-desire-is-there-a-bluetooth-problem-corruptedstreamexceptio – Jack Nov 27 '11 at 01:00
  • Thanks Jack, the problems seem to be caused by 2.1 so hopefully I shouldn't be affected, I will try the connection using reflection and see if that helps. – DarkRyuu Nov 27 '11 at 12:13

1 Answers1

4

While looking for the solution of similar problem in my app, I have found this blog from code.google.com

It will help all those who are still looking for this problem solution on SO

http://mobisocial.stanford.edu/news/2011/03/bluetooth-reflection-and-legacy-nfc/ (link not working anymore)

The solution has become very simple now. Just include InsecureBluetooth.java in your project and change 2 lines in BluetoothChatService.java.

tmp = InsecureBluetooth.listenUsingRfcommWithServiceRecord(mAdapter, NAME, MY_UUID, true);

and

tmp   = InsecureBluetooth.createRfcommSocketToServiceRecord(device, MY_UUID, true);

Thats it !

AndyAndroid
  • 4,039
  • 14
  • 44
  • 71
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • This is a nice solution, but can you explain how UUID is used, and the difference between mAdapter and device? I read Android Developers' Bluetooth article but I'm still not totally clear. – Argus9 Mar 28 '13 at 17:40
  • I tried using tmp = InsecureBluetooth.listenUsingRfcommWithServiceRecord(mAdapter, NAME, MY_UUID, true); but it throws an exception "mHandler".... Anyone know how to fix this – Azerue Jan 09 '14 at 17:56
  • Hi @Sourab Sharma, it seems that the link you provided is not available now, is there any other site we could get the code? or could you mail me one copy of it please? My mail is yttyhf2006#gmail.com. Thanks. – YuDenzel Feb 08 '14 at 07:30
  • Hi @Sourab Sharma, I've got one copy of the code @ http://stanford.edu/~tpurtell/InsecureBluetooth.java. Thanks all the same. But I got the problem reported 'connect: Connection is not created' when I call function connect of the BluetoothSocket. – YuDenzel Feb 08 '14 at 08:02