IOException во время вызова connect () для устройства linux - Android Bluetooth - PullRequest
0 голосов
/ 23 февраля 2019

Я читал о и, к сожалению, не могу решить мою проблему.Я пытаюсь просто отправить строку на устройство, но не могу пройти этап подключения.Я попытался сослаться на Google Doc Bluetooth info Вместе с примером приложения чата, но безрезультатно.Также упоминалась пара вопросов переполнения стека 1 , 2 .Ниже приведен фрагмент кода подключения и вывод hcidump, поскольку я использую Linux.Любая помощь будет оценена.

Тема Android Connect:

    private class ConnectThread extends Thread {
    private final BluetoothDevice mmDevice;
    private BluetoothSocket mmSocket;

    public ConnectThread(BluetoothDevice device) {
        Log.d(TAG, "ConnectThread");
        BluetoothSocket temp = null;
        Log.d(TAG, "Device returned :" + device);
        mmDevice = device;
        Log.d(TAG, "Device returned: " + device);
        try {
            //Use device as we are connecting to device
            temp = device.createInsecureRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
        } catch (IOException e) {
            Log.e(TAG, "Error create failed.", e);
        }

        mmSocket = temp;
        mState = STATE_CONNECTING;
    }

    public void run() {
        Log.d(TAG, "Begin connect thread" + this);
        mBluetoothAdapter.cancelDiscovery();

        try {
            mmSocket.connect();
            //BREAKS ABOVE ON CONNECT as this is a blocking call that throws IO or returns
            //connection
        } catch (IOException connectException) {
            Log.e(TAG, "Could not connect to device " + mmDevice.getName(), connectException);
            try {
                //Try catch was removed as is not needed
                /*try {
                    Log.e("", "trying fallback...");

                    mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1);
                    Thread.sleep(500);
                    mmSocket.connect();

                    Log.e("", "Connected");
                } catch (Exception e) {
                    Log.e(TAG, "Error on backup connection", e);
                }*/
                mmSocket.close();
            } catch (IOException closeException) {
                Log.e(TAG, "Could not close client socket", closeException);
            }
            connectionLost();

            return;
        }

        connected(mmSocket, mmDevice); // Manges Coach socket in separate thread
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "Could not close the client socket", e);
        }
    }
}'

Дамп HCI из терминала Linux:

HCI Event: Connect Request (0x04) plen 10
bdaddr XXX class 0x5a020c type ACL
HCI Event: Command Status (0x0f) plen 4
Accept Connection Request (0x01|0x0009) status 0x00 ncmd 1
HCI Event: Connect Complete (0x03) plen 11
status 0x00 handle 256 bdaddr XXX type ACL encrypt 0x00
HCI Event: Command Status (0x0f) plen 4
Read Remote Supported Features (0x01|0x001b) status 0x00 ncmd 1
HCI Event: Read Remote Supported Features (0x0b) plen 11
status 0x00 handle 256
Features: 0xff 0xfe 0x8f 0xfe 0xd8 0x3f 0x5b 0x87
HCI Event: Command Status (0x0f) plen 4
Read Remote Extended Features (0x01|0x001c) status 0x00 ncmd 1
HCI Event: Read Remote Extended Features (0x23) plen 13
status 0x00 handle 256 page 1 max 1
Features: 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00
HCI Event: Command Status (0x0f) plen 4
Remote Name Request (0x01|0x0019) status 0x00 ncmd 1
HCI Event: Remote Name Req Complete (0x07) plen 255
status 0x00 bdaddr XXX name 'XT1030'
HCI Event: Command Complete (0x0e) plen 10
Link Key Request Reply (0x01|0x000b) ncmd 1
status 0x00 bdaddr XXX
HCI Event: Encrypt Change (0x08) plen 4
status 0x00 handle 256 encrypt 0x01
HCI Event: Command Complete (0x0e) plen 7
Read Encryption Key Size (0x05|0x0008) ncmd 1
HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 256 reason 0x13
Reason: Remote User Terminated Connection

Ошибка, выданная кодом:

java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:518)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:529)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
    at ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:518)
    at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:495)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:329)
    at com.example.myapplication.logic.bluetooth.Setup.BluetoothUtil$ConnectThread.run(BluetoothUtil.java:325)

РЕДАКТИРОВАТЬ

Проблема заключалась в том, что устройство подключалось к устройству BLE, не являющемуся классическим Bluetooth.Приведенный выше код является правильным и не требует перехвата попытки с резервным подключением.Смотрите комментарии выше.

...