如何在Android 4.3中启用多个BluetoothGattDescriptor?

时间:2014-12-03 02:09:33

标签: android bluetooth-lowenergy notify accelerator

我正在编写一些代码来获取TI Keyfob(cc2540)加速器数据。 应启用3个CharacteristicsNotifications。 我逐个设置它们并启用了BluetoothGattDescriptor,但在代码运行后,只有第一个BluetoothGattDescriptor可以发送通知。

我的设备是带有Android 4.3的三星S3。

以下是我的BluetoothService.java中的一些代码:

static final byte[] DISABLE = {0x00};
static final byte[] ENABLE = {0x01};

public void enableKeyfobAccelService(boolean enable) {
    BluetoothGattService _service = mBluetoothGatt.getService(UUID_KEYFOB_ACCEL_SERVICE);
    if (_service == null) {
        Log.d(TAG, "Keyfob Accel service not found!");
        return;
    }

    Log.d(TAG, "Keyfob Accel service found!");

    // write value to Characteristic ACCEL_ENABLER "0000ffa1-0000-1000-8000-00805f9b34fb"
    BluetoothGattCharacteristic _nc = _service.getCharacteristic(UUID_KEYFOB_ACCEL_ENABLER);
    if (_nc != null) {
        if (enable)
            _nc.setValue(ENABLE);
        else
            _nc.setValue(DISABLE);
        mBluetoothGatt.writeCharacteristic(_nc);
    }

    accel_enabled = enable;
}


// Implements callback methods for GATT events that the app cares about. For
// example,
// connection change and services discovered.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                        int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:"
                    + mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {

            Log.v(TAG, "onCharacteristicWrite value : " + byteArrayToHex(characteristic.getValue()));

            if (UUID_KEYFOB_ACCEL_ENABLER.equals(characteristic.getUuid())) {

                BluetoothGattCharacteristic _nc = characteristic.getService().getCharacteristic(UUID_KEYFOB_ACCEL_X);

                if( _nc != null ) {
                    setCharacteristicNotification(_nc, accel_enabled);
                    Log.d(TAG, "Keyfob Accel X ready! " + accel_enabled);
                }
                else {
                    Log.d(TAG, "Keyfob Accel UUID_KEYFOB_ACCEL_X is null! ");
                }

                _nc = characteristic.getService().getCharacteristic(UUID_KEYFOB_ACCEL_Y);

                if( _nc != null ) {
                    setCharacteristicNotification(_nc, accel_enabled);
                    Log.d(TAG, "Keyfob Accel Y ready! " + accel_enabled);
                }
                else {
                    Log.d(TAG, "Keyfob Accel UUID_KEYFOB_ACCEL_Y is null! ");
                }

                _nc = characteristic.getService().getCharacteristic(UUID_KEYFOB_ACCEL_Z);

                if( _nc != null ) {
                    setCharacteristicNotification(_nc, accel_enabled);
                    Log.d(TAG, "Keyfob Accel Z ready! " + accel_enabled);
                }
                else {
                    Log.d(TAG, "Keyfob Accel UUID_KEYFOB_ACCEL_Z is null! ");
                }
            }
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
};

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    if (characteristic == null) {
        Log.w(TAG, "Bluetooth characteristic not initialized");
        return;
    }

    boolean success = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    Log.v(TAG, "setCharacteristicNotification = " + success);

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    if (descriptor != null) {
        byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
        descriptor.setValue(val);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

在我接受UUID_KEYFOB_ACCEL_ENABLER之后,我可以读取日志以了解所有三个mBluetoothGatt.writeDescriptor(描述符)都已运行。但是onCharacteristicChanged()只能从UUID_KEYFOB_ACCEL_X中获取更改的值。

很奇怪,请求你的帮助。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题的原因:对于BLE,应该在两次写入之间等待一段时间:

_nc = characteristic.getService().getCharacteristic(UUID_KEYFOB_ACCEL_Y);

if( _nc != null ) {
   setCharacteristicNotification(_nc, accel_enabled);
   Log.d(TAG, "Keyfob Accel Y ready! " + accel_enabled);
}
else {
   Log.d(TAG, "Keyfob Accel UUID_KEYFOB_ACCEL_Y is null! ");
}

// MUST add wait some times
try {
        Thread.sleep(100);
    } catch (Exception e) {
        // ignore
    }

_nc = characteristic.getService().getCharacteristic(UUID_KEYFOB_ACCEL_Z);

if( _nc != null ) {
    setCharacteristicNotification(_nc, accel_enabled);
    Log.d(TAG, "Keyfob Accel Z ready! " + accel_enabled);
}
else {
    Log.d(TAG, "Keyfob Accel UUID_KEYFOB_ACCEL_Z is null! ");
}