Example usage for android.bluetooth BluetoothGattDescriptor setValue

List of usage examples for android.bluetooth BluetoothGattDescriptor setValue

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattDescriptor setValue.

Prototype

public boolean setValue(byte[] value) 

Source Link

Document

Updates the locally stored value of this descriptor.

Usage

From source file:com.example.android.bluetoothlegatt.BluetoothLeService.java

/**
 * Enables or disables notification on a give characteristic.
 *
 * @param characteristic Characteristic to act on.
 * @param enabled If true, enable notification.  False otherwise.
 *///from  www. j a  va  2 s.  co  m
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enabled)) {
        Log.e(TAG, "setCharacteristicNotification fail! enabled=" + enabled);
    }

    if ((UUID.fromString("0000fff4-0000-1000-8000-00805f9b34fb")).equals(characteristic.getUuid())) {
        //mBluetoothGatt.writeCharacteristic(characteristic);
        List<BluetoothGattDescriptor> descs = characteristic.getDescriptors();
        for (int i = 0; i < descs.size(); i++) {
            BluetoothGattDescriptor desc = descs.get(i);
            desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            Log.d(TAG, "writeDescriptor notify, uuid=" + desc.getUuid().toString());
            mBluetoothGatt.writeDescriptor(desc);
        }
        //BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        //descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        //mBluetoothGatt.writeDescriptor(descriptor);
    }

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

From source file:com.ble.BLService.java

/**
 * Enables or disables notification on a give characteristic.
 * //  w ww.j  a v a2 s.c  o  m
 * @param characteristic
 *            Characteristic to act on.
 * @param enabled
 *            If true, enable notification. False otherwise.
 */
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    if (UUID_STM32_ACCELEROMETER_PARAMETER.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(BLGattAttributes.STM32_ACCELEROMETER_PARAMETER));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }

    if (UUID_STM32_WRITE_TO_DEVICE.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(BLGattAttributes.STM32_WRITE_TO_DEVICE));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }

}

From source file:com.ec.android.module.bluetooth40.BluetoothLeService.java

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;//from  w w  w. j av a2s .co  m
    }
    if (enabled == true) {
        mBluetoothGatt.setCharacteristicNotification(characteristic, true);
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
        if (descriptor != null) {
            //??
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    } else {
        mBluetoothGatt.setCharacteristicNotification(characteristic, false);
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
        if (descriptor != null) {
            //??
            descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }

}

From source file:de.blinkt.openvpn.bluetooth.service.UartService.java

public void setDescriptor(BluetoothGattService rxService, UUID uuid) {
    CommonTools.delayTime(150);//w ww  . ja  v a  2 s.c om
    BluetoothGattCharacteristic TxChar = rxService.getCharacteristic(uuid);
    if (TxChar == null) {
        showMessage("Tx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }

    //      final int rxProperties = TxChar.getProperties();
    //      boolean writeRequest = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0;
    //
    //      // Set the WRITE REQUEST type when the characteristic supports it. This will allow to send long write (also if the characteristic support it).
    //      // In case there is no WRITE REQUEST property, this manager will divide texts longer then 20 bytes into up to 20 bytes chunks.
    //      if (writeRequest)
    //         TxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

    mBluetoothGatt.setCharacteristicNotification(TxChar, true);

    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    //      mBluetoothGatt.readDescriptor(descriptor);
    mBluetoothGatt.writeDescriptor(descriptor);
    //??
    broadcastUpdate(FINDED_SERVICE);
}

From source file:com.cerevo.blueninja.blueninja_ble_example.MainActivity.java

/**
 * Characteristic?Notification?//from w w  w.  jav  a  2s .  c  o m
 */
private void disableBleNotification() {
    for (int i = 0; i < UUIDS_CHARACTERISTIC.length; i++) {
        BluetoothGattDescriptor desc = mCharacteristics[i]
                .getDescriptor(UUID.fromString(UUID_CLIENT_CHARACTERISTIC_CONFIG));
        desc.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
        mGatt.writeDescriptor(desc);
        if (!mGatt.setCharacteristicNotification(mCharacteristics[i], false)) {
            setStatus(AppState.BLE_NOTIF_REGISTER_FAILED);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    setStatus(AppState.BLE_NOTIF_REGISTERD);
}

From source file:com.cerevo.blueninja.blueninja_ble_example.MainActivity.java

/**
 * Characteristic?Notification?//  www .  j  av  a  2 s.c  o  m
 */
private void enableBleNotification() {
    for (int i = 0; i < UUIDS_CHARACTERISTIC.length; i++) {
        boolean reg = mGatt.setCharacteristicNotification(mCharacteristics[i], true);
        BluetoothGattDescriptor desc = mCharacteristics[i]
                .getDescriptor(UUID.fromString(UUID_CLIENT_CHARACTERISTIC_CONFIG));
        desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mGatt.writeDescriptor(desc);

        if (!reg) {
            setStatus(AppState.BLE_NOTIF_REGISTER_FAILED);
            return;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    setStatus(AppState.BLE_NOTIF_REGISTERD);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

/**
 * Enable TXNotification/*from w w w.j a  v  a  2 s.c  o m*/
 *
 * @return 
 */
private void enableTXNotification() {
    BluetoothGattService RxService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (RxService == null) {
        showMessage("Rx service not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    if (TxChar == null) {
        showMessage("Tx charateristic not found!");
        broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    Log.i("Chandler", "RX_SERVICE_UUID");
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);
    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_SPEED_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;/* w  w w.  j  ava  2 s  .  c  o m*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService.getCharacteristic(UUID_SPEED_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_INCLINE_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;//from  w  w w.j  av a2s  .com
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_INCLINE_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.example.bluetoothmodule.TreadmillService.java

private void Enable_SportTime_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;//from   ww  w .j  a  v  a 2s .c o  m
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_SPORT_TIME_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("SPEED charateristic not found!");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}