Bluetooth connection failure “java.io.ioexception: read failure, socket may be closed or timeout, read RET: – 1”

I'm trying to connect to the device through an application installed in nexus 5 I want to make an application like rainbow contacts in Android In my app, my goal is to connect to another device via Bluetooth and transfer a group of contacts or files

I can use the pair device dialog box, but when I try to pair, an error will appear

//to create socket
if (secure) {
            bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
        } else {
            bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
        }
//connection establishment
try {

                bluetoothSocket.connect();
                success = true;
                break;
            } catch (IOException e) {
                //try the fallback
                try {
                    Class<?> clazz = tmp.getRemoteDevice().getClass();
                    Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
                    Method m = clazz.getmethod("createRfcommSocket",paramTypes);
                    Object[] params = new Object[] {Integer.valueOf(1)};
                    bluetoothSocket  = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(),params);
                    Thread.sleep(500);
                    bluetoothSocket.connect();
                    success = true;
                    break;
                } catch (FallbackException e1) {
                    Log.w("BT","Could not initialize FallbackBluetoothSocket classes.",e);
                } catch (InterruptedException e1) {
                    Log.w("BT",e1.getMessage(),e1);
                } catch (IOException e1) {
                    Log.w("BT","Fallback Failed. Cancelling.",e1);
                }
            }

I was wrong

09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication I/BT﹕ Attempting to connect to Protocol: 00001101-0000-1000-8000-00805f9b34fb
09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:57.247  27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(),SocketState: INIT,mPfd: {ParcelFileDescriptor: FileDescriptor[56]}
09-06 13:44:58.667  27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:58.667  27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(),mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication W/BT﹕ Fallback Failed. Cancelling.
    java.io.IOException: read Failed,socket might closed or timeout,read ret: -1
            at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
            at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
            at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
            at com.example.gauravdubey.myapplication.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:198)
            at com.example.gauravdubey.myapplication.BluetoothConnector.connect(BluetoothConnector.java:62)
            at com.example.gauravdubey.myapplication.ConnectThread.run(ConnectThread.java:101)
            at com.example.gauravdubey.myapplication.MainActivity$1.onItemClick(MainActivity.java:288)
            at android.widget.AdapterView.performItemClick(AdapterView.java:299)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
            at android.widget.AbsListView$3.run(AbsListView.java:3645)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication V/connectThread﹕ Could not connect to device: B0:D0:9C:8B:A4:47
09-06 13:45:03.267  27860-27860/com.example.gauravdubey.myapplication I/Choreographer﹕ Skipped 361 frames!  The application may be doing too much work on its main thread.

So what did I do wrong? Any help would be appreciated

Solution

In Bluetooth connector In the constructor of Java, a list of uuidcandidates will be passed

public BluetoothConnector(BluetoothDevice device,boolean secure,BluetoothAdapter adapter,List<UUID> uuidCandidates) 
          {
        this.device = device;
        this.secure = secure;
        this.adapter = adapter;
        this.uuidCandidates = uuidCandidates;

        if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
            this.uuidCandidates = new ArrayList<UUID>();
            this.uuidCandidates.add(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
        }
    }

Did you pass null?

If so, try calling device. For the Bluetooth device to be connected Fetchuuidswithsdp() and receive Bluetooth device in the receiver ACTION_ UUID intent Because this will get the list of UUIDs supported by the device

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>