Bluetooth connection on Android ICS impossible

I am writing an application that sends bytecodes from a tablet to a ฮผ controller. Everything worked perfectly on Lenovo A1 (Androi 2.3) and Samsung Galaxy Tab 7 Plus N (Android 3.2). Now I have problems with the new Samsung Galaxy Tab 2 tab (Android 4.0).

I can connect to a Bluetooth antenna (which is connected to the ฮผ-controller and transmits via a serial protocol). When I launched the application, I will again be asked to enter a password and connect. After entering the pairing password, my main layout is displayed, but the connection is not established.

LogCat in eclipse tells me:

06-19 16:00:20.656: V/BluetoothSocket.cpp(3189): availableNative 06-19 16:00:20.664: V/BluetoothSocket.cpp(3189): abortNative 06-19 16:00:20.664: V/BluetoothSocket.cpp(3189): ...asocket_abort(49) complete 06-19 16:00:20.664: I/ActivityManager(185): No longer want com.google.android.partnersetup (pid 3220): hidden #16 06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): availableNative 06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): destroyNative 06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): ...asocket_destroy(49) complete 06-19 16:00:20.679: D/KeyguardViewMediator(185): setHidden false 06-19 16:00:20.679: W/System.err(3189): java.io.IOException: socket closed 06-19 16:00:20.679: W/System.err(3189): at android.bluetooth.BluetoothSocket.available(BluetoothSocket.java:370) 06-19 16:00:20.679: W/System.err(3189): at android.bluetooth.BluetoothInputStream.available(BluetoothInputStream.java:40) 06-19 16:00:20.679: W/System.err(3189): at java.io.BufferedInputStream.available(BufferedInputStream.java:114) 06-19 16:00:20.687: W/System.err(3189): at ebs.alphadidact.control.ReceiveThread.run(ReceiveThread.java:79) 

In addition, LogCat receives a thousand times the message:

 V/BluetoothSocket.cpp(3189): availableNative 

So, when I searched on the net, I found several guys with a similar problem, but without a solution. Does anyone know something about this issue?

Perhaps this is a compatibility issue between the antenna and Android 4.0. I donโ€™t think there is an error in my code, because, as I said, the same code works fine on older versions of Android.

+6
source share
5 answers

Well, I found out what the problem is. I'm not sure if this is a Samsung problem or an ICS problem for Android.

I tried to connect to the antenna, as usual, using (to get a Socket):

 clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 

Well, it doesn't seem to work with my antenna and tablets, so I tried:

 clientSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 

It works. The first parameter made the system turn off the antenna, and then again ask to pair.

+4
source

In fact, creating an unsafe socket is similar to connecting two unpaired devices. This is clearly not the best way to handle this.

I found that Android is trying to recover the device, and then rejects the pairing response. After this bizzare behavior he will accept the next connection attempt!

I also tried the Android bugtracker: the Bluetooth RFCOMM Server Socket no longer connects properly to the embedded device on ICS 4.0.3 .

Still waiting for an answer ...

+2
source

Thanks to @fuentessifuentes answer I wrote this method, including backward compatibility:

 private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { if(Build.VERSION.SDK_INT >= 10){ try { final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class }); return (BluetoothSocket) m.invoke(device, SPP_UUID); } catch (Exception e) { Log.e(TAG, "Could not create Insecure RFComm Connection",e); } } return device.createRfcommSocketToServiceRecord(SPP_UUID); } 

Maybe this helps someone from this release.

+2
source

I believe that I see the same problem. I am using the spp terminal application from a Google game, which worked fine after pairing the device with my x droid. But now that my galaxy s3 with the same application and the same device requires me to re-pair every time.

Your solution is a kind of workaround. Android seems to have changed this behavior in ICS. So the real Google solution is to fix ICS to allow spp devices to connect and connect without pairing.

But I saw code to solve a similar problem:

 BluetoothSocket mSocket = null; mBluetoothAdapter.cancelDiscovery(); Method method; try { method = mBluetoothDevice.getClass() .getMethod("createRfcommSocket", new Class[] { int.class}); mSocket = (BluetoothSocket) method.invoke(mBluetoothDevice,1); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } mSocket.connect(); 
+1
source

Using this code:

 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); mBluetoothAdapter.cancelDiscovery(); Method m; try { m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class }); btSocket = (BluetoothSocket) m.invoke(device, 1); } catch (SecurityException e1) { e1.printStackTrace(); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } 

and adding the following to our Manifest works app

<uses-sdk android:minSdkVersion="13" android:targetSdkVersion="16"/>

0
source

All Articles