BLE HID over GATT не вызывается - PullRequest
       27

BLE HID over GATT не вызывается

0 голосов
/ 24 сентября 2018

Я работаю с приложением Android, которое подключается к устройству BLE, которое реализует протокол связи HID через GATT.Что мне нужно сделать, это получить уведомление от устройства при нажатии кнопки, включить некоторые светодиоды или прочитать некоторые параметры.Проблема в том, что обратные вызовы не вызываются, и я не могу читать или записывать данные на устройство, а также onCharacteristicChanged никогда не вызывается.

Вот мой обратный вызов:

    private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            gatt.discoverServices();
            Log.d(TAG, "CONNECTED");
        } else {

        }
    }
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        //super.onServicesDiscovered(gatt, status);
        Log.d("OnServDiscovered", "REACHEd");
        if (status == BluetoothGatt.GATT_SUCCESS) {
            BluetoothGattService service = gatt.getService(HID_UUID);
            if (service != null) {
                Log.d(TAG, "Service not null");
                final BluetoothGattCharacteristic characteristic = service.getCharacteristic(HID_REPORT_UUID);
                if (characteristic != null) {
                    Log.d(TAG, "Characteristic not null");
                    setCharacteristicNotification(characteristic, true);
                    characteristic.setValue(new byte[]{(byte) 0x01, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0x1F, 0x00, 0x00, 0x00});
                    Log.d(TAG, "Write status:" + gatt.writeCharacteristic(characteristic));
                }
            }
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
        Log.d("onCharacteristicChanged", characteristic.getStringValue(0));
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);

        Log.d(TAG, "OnCharacteristicWrite reached");
    }

    @Override
    public void onDescriptorWrite(final BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);
        Log.d("ON DESC WRITE", "REACHED. CODE:" + String.valueOf(status));

    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        Log.d("ONCHARREAD", "REACHED!!!!");
    }
};

Состояние чтения / записи из/ характеристика или дескрипторы верны, но обратные вызовы никогда не запускаются.Служба, Характеристика, Дескриптор не равны NULL, но у меня есть ошибка в Logcat.

09-24 09:55:20.800 22527-22540/com.example.pcostache.tstyt 
D/MainActivity: CONNECTED
09-24 09:55:20.812 22527-22540/com.example.pcostache.tstyt 
D/BluetoothGatt: onSearchComplete() = Device=00:08:D3:F2:00:XX Status=0
09-24 09:55:20.812 22527-22540/com.example.pcostache.tstyt 
E/BluetoothGatt: Broken GATT database: can't find included service.
09-24 09:55:20.813 22527-22540/com.example.pcostache.tstyt 
D/OnServDiscovered: REACHEd
09-24 09:55:20.813 22527-22540/com.example.pcostache.tstyt D/SERV: NOT 
NULL
09-24 09:55:20.813 22527-22540/com.example.pcostache.tstyt 
D/Characteristic: NOT NULL
09-24 09:55:20.817 22527-22540/com.example.pcostache.tstyt D/STAT: 
SUCCES

Сломанная база данных GATT взята из этого куска кода из Android:

                    // Fix references to included services, as they doesn't point to right objects.
                for (BluetoothGattService fixedService : mServices) {
                    ArrayList<BluetoothGattService> includedServices =
                            new ArrayList(fixedService.getIncludedServices());
                    fixedService.getIncludedServices().clear();

                    for (BluetoothGattService brokenRef : includedServices) {
                        BluetoothGattService includedService = getService(mDevice,
                                brokenRef.getUuid(), brokenRef.getInstanceId());
                        if (includedService != null) {
                            fixedService.addIncludedService(includedService);
                        } else {
                            Log.e(TAG, "Broken GATT database: can't find included service.");
                        }
                    }
                }

После длительного периодаИз исследований я действительно не могу понять, что здесь происходит, и любая помощь или предложение будут замечательными.Спасибо!

...