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.bluetoothmodule.TreadmillService.java

private void Enable_SportDistance_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;/*  w w w  . java 2 s. c  om*/
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_SPORT_DISTANCE_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_SporCalories_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("TREADMILL_SERVICE service not found!");
        return;//from   ww w .j ava  2  s .c  o  m
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_SPORT_CALORIES_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_HeartRate_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(HR_SERVICE_UUID);
    if (TreadmillService == null) {
        showMessage("HR_SERVICE service not found!");
        return;/*  ww w  .  j a  v a2  s.c o m*/
    }
    Log.i("Chandler", "Hr_SERVICE_UUID");
    BluetoothGattCharacteristic Characteristic = TreadmillService.getCharacteristic(UUID_HR_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("Hr 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_DISPLAY_STATE_CHARACTERISTIC_Notification() {
    BluetoothGattService TreadmillService = mBluetoothGatt.getService(UUID_TREADMILL_SERVICE);
    if (TreadmillService == null) {
        showMessage("Rx service not found!");
        //broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;//  ww w .  j a  va2 s.  c o m
    }
    BluetoothGattCharacteristic Characteristic = TreadmillService
            .getCharacteristic(UUID_DISPLAY_STATE_CHARACTERISTIC);
    if (Characteristic == null) {
        showMessage("DISPLAY STATE charateristic not found!");
        //broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
        return;
    }
    Log.i("Chandler", "UUID_DISPLAY_STATE_CHARACTERISTIC");
    mBluetoothGatt.setCharacteristicNotification(Characteristic, true);
    BluetoothGattDescriptor descriptor = Characteristic.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.qi.airstat.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 w w  w .j  ava  2  s  .  co  m
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // 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.guangyao.bluetoothtest.service.BluetoothLeService.java

/**
 * @brief enableTXNotification// ww  w  .  ja v a  2s  .c  om
 */
@SuppressLint("InlinedApi")
public void enableTXNotification() {
    BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
    if (RxService == null) {
        return;
    }

    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
    if (TxChar == null) {
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(TxChar, true);
    BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);
}

From source file:com.evothings.BLE.java

private void writeDescriptor(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    gh.mOperations.add(new Runnable() {
        @Override/* w w  w .j  a va  2  s.  co  m*/
        public void run() {
            try {
                gh.mCurrentOpContext = callbackContext;
                BluetoothGattDescriptor d = gh.mDescriptors.get(args.getInt(1));
                d.setValue(args.getArrayBuffer(2));
                if (!gh.mGatt.writeDescriptor(d)) {
                    gh.mCurrentOpContext = null;
                    callbackContext.error("writeDescriptor");
                    gh.process();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                callbackContext.error(e.toString());
                gh.process();
            }
        }
    });
    gh.process();
}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

public void writeValue(String address, BluetoothGattDescriptor descriptor, byte[] value) {
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    if (bluetoothGatt == null) {
        Log.w(TAG, "No connection found for: " + address);
        sendGattBroadcast(address, BleEvents.WRITE_DESC_FAIL, null);
        return;//from   w ww.  j av a  2  s.c o m
    }

    if (!descriptor.setValue(value) || !bluetoothGatt.writeDescriptor(descriptor)) {
        sendGattBroadcast(address, BleEvents.WRITE_DESC_FAIL, descriptor.getCharacteristic());
    }
}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

private void createGATT(final BluetoothDevice device) {
    bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() {
        @Override//from ww w. j  a v  a  2s.  c  o m
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            isConnected = BluetoothProfile.STATE_CONNECTED == newState;
            if (isConnected) {
                Log.i(TAG, "GATT connected.");
                startForeground(442, getNotification(device));
                gatt.discoverServices();
            } else {
                Log.i(TAG, "GATT disconnected.");
                NotificationService_.intent(BluetoothService.this).stop();
                notificationManager.notify(442, getNotification(device));
                gatt.connect();
            }

            final Intent connectionStateChanged = new Intent(CONNECTION_STATE_CHANGED);
            connectionStateChanged.putExtra(CONNECTION_STATE, isConnected);
            sendBroadcast(connectionStateChanged);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.i(TAG, "Services discovered.");
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            discoverCharacteristics(gatt);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            Log.i(TAG, "Characteristic written: " + status);

            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final byte command = characteristic.getValue()[0];
            switch (command) {
            case WRITE_TIME:
                Log.i(TAG, "Time written.");
                final BluetoothGattCharacteristic batteryCharacteristic = readableCharacteristics
                        .get(Characteristic.BATTERY.getUuid());
                gatt.setCharacteristicNotification(batteryCharacteristic, true);
                for (BluetoothGattDescriptor descriptor : batteryCharacteristic.getDescriptors()) {
                    if (descriptor.getUuid().toString().startsWith("00002904")) {
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        gatt.writeDescriptor(descriptor);
                    }
                }
                break;
            case WRITE_NOTIFICATION:
                Log.i(TAG, "Notification sent.");
                if (notificationsQueue.isEmpty()) {
                    Log.i(TAG, "Reading characteristics...");
                    readNextCharacteristics(gatt);
                } else {
                    Log.i(TAG, "writing next notification...");
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }
                break;
            default:
                Log.w(TAG, "No such ALERT IN command: " + command);
                break;
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            readCharacteristic(gatt, Characteristic.MANUFACTURER);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                final BluetoothGattCharacteristic gattCharacteristic, int status) {
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            switch (characteristic) {
            case MANUFACTURER:
                manufacturerInfo.manufacturer = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.FW_REVISION);
                break;
            case FW_REVISION:
                manufacturerInfo.firmwareRevision = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.MODE);
                break;
            default:
                Log.v(TAG, "Characteristic read: " + characteristic.name());
                if (characteristic == Characteristic.MODE) {
                    final Mode newMode = Mode.bySymbol(gattCharacteristic.getValue()[0]);
                    if (mode != newMode) {
                        onModeChanged(newMode);
                    }
                } else {
                    onBluetoothDataReceived(characteristic, gattCharacteristic.getValue());
                }

                if (notificationsQueue.isEmpty()) {
                    readNextCharacteristics(gatt);
                } else {
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }

                break;
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                BluetoothGattCharacteristic gattCharacteristic) {
            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            Log.d(TAG, "Characteristic changed: " + characteristic);

            if (characteristic == Characteristic.BATTERY) {
                onBluetoothDataReceived(Characteristic.BATTERY, gattCharacteristic.getValue());
            }
        }
    });
}

From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.BluetoothCustomManager.java

@SuppressLint("NewApi")
@Override/*from ww  w .  j a  v  a2s . c  om*/
public void writeDescriptor(String descriptorUid, BluetoothGatt gatt, byte[] value, String serviceUid,
        String characUid) {

    if (gatt != null && descriptorUid != null) {

        gattThreadPool.execute(new GattTask(gatt, descriptorUid, value, serviceUid, characUid) {
            @Override
            public void run() {

                if (getGatt() != null
                        && getGatt().getService(UUID.fromString(getDescriptorServiceUid())) != null
                        && getGatt().getService(UUID.fromString(getDescriptorServiceUid()))
                                .getCharacteristic(UUID.fromString(getDescriptorCharacUid())) != null) {

                    BluetoothGattDescriptor descriptor = getGatt()
                            .getService(UUID.fromString(getDescriptorServiceUid()))
                            .getCharacteristic(UUID.fromString(getDescriptorCharacUid()))
                            .getDescriptor(UUID.fromString(getUid()));

                    descriptor.setValue(getValue());

                    getGatt().writeDescriptor(descriptor);
                }

                eventManager.reset();
                try {
                    eventManager.waitOne(BT_TIMEOUT);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        gattThreadPool.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    } else
        Log.e(TAG, "Error int writeCharacteristic() input argument NULL");
}