Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bluetooth socket.connect() fails

I'm trying to make a connection between an android device and a RFID reader bluetooth. To do this i use the code of bluetooth chat (bluetooth chat example). But when i do mmSocket.connect(); at line 329 of the bluetooth chat example, the connection generate every time a java.io.IOException. I tried also this method to get the socket:

tmp = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mDevice, 1);

but nothing. I tried with 3 difference devices. The first, a Samung S2 running android 4.4.2 give me this error:

failed:read failed, socket might closed, read ret: -1

With a tablet running android 4.0.3 give me this error:

IOException: Connection refused

The curiosity is that if i try to connect my phone with my tablet i fail. But if i run in 2 devices this application, and with one i try to connect the second, while the second is searching some devices to connect, the connection is succesfull. But only if the second device is running this application and searching some devices to connect. I tried also to unpair but nothing. Finally i wanted to say that if i try to connect the 2 devices or one device with my rfid bluetooth reader throught settings the connection is succesfull. And last i wanted to say that when i'm trying to connect the 2 devices or the device with the reader rfid, if the devices isn't paired, compare a dialog that ask me to pair the 2 device but after this the connection failed.

like image 307
user3805317 Avatar asked Jul 04 '14 11:07

user3805317


3 Answers

Your code assumes secure BT connection, and attempts in two different ways (which is good). The first test you need to attempt is insecure BT connection using these methods:

BluetoothSocket sock;

// open API
sock = device.createInsecureRfcommSocketToServiceRecord(serviceUuid);

// if failed: hidden API
createMethod = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket)createMethod.invoke(device, 1);


If that does not fix the problem you should prob. move to checking the correctness of the uuid you pass to the creation method. You are probably (?) using the default SPP uuid:

UUID DEFAULT_SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

This works on many devices, but not on all.


Try to interrogate your peer for the list of uuids it supports:

// for apiVer >= 15
supportedUuids = device.getUuids();


// for apiVer < 15
try {
   Class cl = Class.forName("android.bluetooth.BluetoothDevice");
   Class[] params = {};
   Method method = cl.getMethod("getUuids", params);
   Object[] args = {};
   supportedUuids = (ParcelUuid[])method.invoke(device, args);
}
catch (Exception e) {
    // no op
    Log.e("uuids", "Activation of getUuids() via reflection failed: " + e);
}

And, if not empty, use the first in array as parameter to the creation method.

You can also use BTWiz, written by yours truly, that does all that for you in a transparent fall-through model, and also supports async Bluetooth IO.

like image 154
Gilad Haimov Avatar answered Nov 12 '22 22:11

Gilad Haimov


I had the same problem. It's easy to resolve. You have to configure your RFID reader to SPP protocol with his configure program and after this he will work.

like image 36
Samjack Avatar answered Nov 12 '22 22:11

Samjack


I had that problem too, I tried everything that was said in this post, but nothing worked for me. I finally got to fix it. My wrong code used a class variable BluetoothAdapter to check if bluetooth was on, like this :

public class PrintClass extends Activity  {
private XXXX myClassVar;
private BluetoothAdapter bluetooth; // created and checked in other method

... other methods in my class....

}

I got bt socket connect fails all the time. But I used a private method to activate Bluetooth, and the socket problem disappeared.

    public class PrintClass extends Activity  {

private XXXX myClassVar;

private void turnBlueToothOn(){

     try {
        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
        if(!bluetooth.isEnabled()){
            bluetooth.enable();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
....other methods in my class....
}

Why?? I have no idea, just worked for me!!!!

like image 22
ldt Avatar answered Nov 12 '22 21:11

ldt