Android bluetooth socket.connect () not working

I am trying to establish a connection between an Android device and a Bluetooth RFID reader. For this, I use the bluetooth chat code ( example of bluetooth chat ). But when I do mmSocket.connect(); in line 329 of the bluetooth chat example, a connection is generated each time a java.io.IOException . I also tried 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. Firstly, running Android 4.4.2 Samung S2 gives 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 to the tablet, I will fail. But if I launched this application in 2 devices, and with one I try to connect the second, and the second is looking for some devices to connect, the connection is successful. But only if the second device launches this application and is looking for some devices to connect. I also tried to figure it out, but nothing. Finally, I wanted to say that if I try to connect 2 devices or one device using rfid bluetooth reader through the settings, the connection will be successful. And the last thing I wanted to say is that when I try to connect 2 devices or a device to the rfid reader, if the devices are not paired, compare the dialog box that asks me to connect device 2, but after that the connection failed,

+6
source share
4 answers

I had the same problem. This is easy to resolve. You must configure your RFID reader to SPP using your setup program, after which it will work.

0
source

Your code assumes a secure BT connection and attempts in two different ways (which is good). The first test you need to run is an unsafe BT connection using the following 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 this does not fix the problem, you must solve the problem. go to validation from uuid you go to the creation method. You probably (?) Use default SPP uuid:

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

This works on many devices, but not all.


Try to ask your peer for the list of uuids that he 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 the array as a parameter to the create method.

You can also use BTWiz , written by your truly, which does everything for you in a transparent failed model and also supports asynchronous Bluetooth IO.

+4
source

I also had this problem, I tried everything that was said in this post, but nothing worked for me. I finally decided to fix it. In my wrong code, a BluetoothAdapter class variable was used to check if bluetooth is enabled, for example:

 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 connection failed 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 !!!!

+1
source

Sometimes a command connection in a Bluetooth client on a Bluetooth server does not work. It can be an error in the driver or an error in the Bluetooth module, or an error in the library implements Bluetooth in Java. For me, it only helps to turn off and then turn on Bluetooth, then the connection will start working fine.

0
source

Source: https://habr.com/ru/post/971763/


All Articles