4

I'm creating insecure rfcomm connection from my HTC One X to SENA bluetooth ESD110 Serial module with this code:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(addr);
BluetoothSocket socket = null;

try{
socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(SERVICE_UUID));

 mBluetoothAdapter.cancelDiscovery();

socket.connect();
sendMessage(socket,"working!");
socket.close();

The point here is to connect to the module with auto paring, and it's all working, but it's taking about 15 seconds for the connection to be established.

The debug remains at:

socket.connect();

for 15 seconds and only after that connecting and transfering the string to the module (takes 1 sec for that).

Is it possible that the phone is processing some other data (services discovery and more) before connecting to the MAC-address and how will I make it connect FASTER? Or can it be HTC hardware issue?

JJJ
  • 32,902
  • 20
  • 89
  • 102
itai
  • 302
  • 5
  • 15

2 Answers2

1

EDIT if a slow Service Discovery is your Problem you can try avoiding that by skipping the whole service discovery by using Reflection to the createRfcommSocket method directly - I do that for other reasons in my BT-SPP related Projects - it looks like this:

BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bd = bta.getRemoteDevice(mac);
Method m = bd.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
bt_connection = (BluetoothSocket) m.invoke(bd, Integer.valueOf(1));

Pre-Comment: do you have to scan all the time? If you have the option to remember the mac you could connect faster. The long waiting-time imho comes from discovery - you can only connect if this is really finished ( not directly done after mBluetoothAdapter.cancelDiscovery(); ) - and this can take some time ..

ligi
  • 39,001
  • 44
  • 144
  • 244
  • hi ligi, i done the connection twice with the same 'device' with the mac-address of my phone - no scanning, it took 15s for the first connection to made, and 15s to the second one.. i think the phone is 'scanning' the bluetooth module for avalible services and it taking too long, but i allready gave the phone the service uuid i need... – itai Jun 20 '12 at 12:05
  • hi, thanks for the help, i edited the code but i have some error using the reflaction: `BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:01:95:13:5C:4F"); Method m = device.getClass().getMethod("createInsecureRfcommSocket",new Class[] { int.class }); BluetoothSocket socket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));` but i get : IllegalAccessException - Unhandled exception type InvocationTargetException – itai Jun 20 '12 at 16:52
  • createInsecureRfcommSocket/createRfcommSocket/createInsecureRfcommSocketToServiceRecord/createeRfcommSocketToServiceRecord - all reflaction to this function return that errors..... – itai Jun 20 '12 at 17:09
  • can you tyy putting a m.setAccessible(true); before invoking ? – ligi Jun 20 '12 at 20:05
  • actually i'm getting: Unhandled exception type NoSuchMethodException on line: `Method m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });` – itai Jun 21 '12 at 05:41
  • I was too having exactly the same problem in that Bluetooth connection to an SPP module always took perhaps 10 - 20 seconds before succeeding. I always had the above technique in my code for making a connection, *but* it was only used as a fall-back if `.connect()` failed and gave the well-known "unable to start discovery" exception, for which the above reflection method is a well known workaround for this. However, I changed my code to just use the reflection method straight away without bothering to try the `.connect()` method first, and it now connects immediately. So +1 from me. – Trevor Aug 11 '12 at 19:42
0

Is it faster the second time? Finding the phone the first time (before your device has the timing of the target device) takes a while.

However, even if it has to find the target, it shouldn't take 15 seconds.

Perhaps you are encountering this bug: http://code.google.com/p/android/issues/detail?id=29039

Note that there is one situation in which creating a connection works but takes much longer then normal.

Tom
  • 17,103
  • 8
  • 67
  • 75