Android BLE的读写特性

时间:2018-01-31 12:29:57

标签: android bluetooth-lowenergy

我正在开发一个Android BLE应用程序,我正在使用此代码将消息写入设备并阅读响应。基本代码是android的例子。只是想知道为什么这与一些BLE芯片和其他没有合作。这是在BLE中使用写入和读取的正确方法吗?

if(BluetoothLeService.mBluetoothGatt.getService(BluetoothLeService.UUID_SERVICE) != null){

    BluetoothGattService GattService = BluetoothLeService.mBluetoothGatt.getService(BluetoothLeService.UUID_SERVICE);

    BluetoothGattCharacteristic characteristic = GattService.getCharacteristic(BluetoothLeService.UUID_CHARACTERISTICS);

    if(characteristic != null) {

        uuidOK = true;
        final int charaProp = characteristic.getProperties();

        if (((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) | (charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) {

            BluetoothLeService.writeCharacteristic(new byte[]{0, 10, 10});
        }

        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            // If there is an active notification on a characteristic, clear
            // it first so it doesn't update the data field on the user interface.
            if (mNotifyCharacteristic != null) {
                mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
                mNotifyCharacteristic = null;
            }
            mBluetoothLeService.readCharacteristic(characteristic);
        }

        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            mNotifyCharacteristic = characteristic;
            mBluetoothLeService.setCharacteristicNotification(characteristic, true);

        }

    }
}

0 个答案:

没有答案