Question

i'm writting an app which sends byte codes from a tablet to a µ-controler. Everything worked fine on the Lenovo A1 (Androi 2.3) and Samsung Galaxy Tab 7 Plus N (Android 3.2). Now i'm having issues with the new Samsung Galaxy Tab 2 (Android 4.0).

I'm able to pair with the Bluetooth antenna (which is connected to the µ-controller and communicates over the serial protocol). When I start the app I'm asked again to enter the password and to pair. After I enter the pairing password my main layout is visible but a connection isn't established.

The 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)

Further more is the LogCat receiving a thousand times the message:

V/BluetoothSocket.cpp(3189): availableNative

So as I searched in the web i found a few guys with a similar problem but no solution. Does someone know something about this issue?

Maybe it is a compability problem between the antenna and android 4.0. I don't think that the error is in my code because as I said the same code is running on older android versions perfectly.

Was it helpful?

Solution

Ok, I found out what the problem is. I'm not sure if it is just a Samsung problem or an Android ICS problem.

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

clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

Well it seems that it doesn't work out with my antenna and Tablet setup, so I tried:

clientSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

This does work. Ther first option forced the system to unpair the antenna and later ask for pairing again.

OTHER TIPS

Actually creating an insecure socket is the same as connecting two unpaired devices. This is clearly not the best way to handle it.

I have found that Android tries to repair to the device, and then it rejects the pairing response. After this bizzare behavior, it will accept the next connection attempt!

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

Still waiting for a response...

Thanks to @fuentessifuentes answer i wrote this method including backwards 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 it helps somone, out of this issu.

I believe I am seeing the same problem. I am using an spp terminal app from Google play that worked perfectly after pairing the device with my stock droid x. But now with my galaxy s3 with the same app and same device requires me to re-pair every time.

Your solution is kind of a workaround. It seems android changed this behavior in ICS. So the real fix is for Google to fix ICS to allow spp devices to pair and connect without 're pairing.

But, I did see some code to address a similar issue:

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();

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 app Manifest worked

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top