Example usage for android.bluetooth BluetoothGatt writeDescriptor

List of usage examples for android.bluetooth BluetoothGatt writeDescriptor

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGatt writeDescriptor.

Prototype

public boolean writeDescriptor(BluetoothGattDescriptor descriptor) 

Source Link

Document

Write the value of a given descriptor to the associated remote device.

Usage

From source file:Main.java

public static boolean disableNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    BluetoothGattDescriptor descriptor = configDescriptor(characteristic);
    descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    return gatt.writeDescriptor(descriptor) && gatt.setCharacteristicNotification(characteristic, false);
}

From source file:Main.java

public static boolean enableNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    BluetoothGattDescriptor descriptor = configDescriptor(characteristic);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    return gatt.setCharacteristicNotification(characteristic, true) && gatt.writeDescriptor(descriptor);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
static boolean setCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
        boolean isEnabled) {
    if (characteristic != null) {
        gatt.setCharacteristicNotification(characteristic, isEnabled);
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DEVICE_CONFIG_CHARACTERISTIC);
        if (descriptor != null) {
            descriptor.setValue(//from   ww w . jav a  2  s.  co m
                    isEnabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
            return gatt.writeDescriptor(descriptor);
        } else
            return false;
    } else
        return false;
}

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 a  v 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.megster.cordova.rfduino.Peripheral.java

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(TAG, "gatt " + gatt);
    Log.d(TAG, "status " + status);
    super.onServicesDiscovered(gatt, status);

    BluetoothGattService service = gatt.getService(RFDUINO_SERVICE_UUID);
    Log.d(TAG, "service " + service);

    BluetoothGattCharacteristic receiveCharacteristic = service.getCharacteristic(RECEIVE_CHARACTERISTIC_UUID);
    sendCharacteristic = service.getCharacteristic(SEND_CHARACTERISTIC_UUID);
    disconnectCharacteristic = service.getCharacteristic(DISCONNECT_CHARACTERISTIC_UUID);

    if (receiveCharacteristic != null) {
        gatt.setCharacteristicNotification(receiveCharacteristic, true);

        BluetoothGattDescriptor receiveConfigDescriptor = receiveCharacteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID);
        if (receiveConfigDescriptor != null) {
            receiveConfigDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(receiveConfigDescriptor);
        } else {//from w  w  w  .j av  a2  s . c om
            LOG.e(TAG, "Receive Characteristic can not be configured.");
        }
    } else {
        LOG.e(TAG, "Receive Characteristic is missing.");
    }

    // call the success callback for connect
    PluginResult result = new PluginResult(PluginResult.Status.OK);
    result.setKeepCallback(true);
    connectCallback.sendPluginResult(result);
}

From source file:ble.AndroidBle.java

@Override
public boolean characteristicNotification(String address, BleGattCharacteristic characteristic) {
    BleRequest request = mService.getCurrentRequest();
    BluetoothGatt gatt = mBluetoothGatts.get(address);
    if (gatt == null || characteristic == null) {
        return false;
    }/*from www .j  a v  a2s  . co m*/

    boolean enable = true;
    if (request.type == RequestType.CHARACTERISTIC_STOP_NOTIFICATION) {
        enable = false;
    }
    BluetoothGattCharacteristic c = characteristic.getGattCharacteristicA();
    if (!gatt.setCharacteristicNotification(c, enable)) {
        return false;
    }

    BluetoothGattDescriptor descriptor = c.getDescriptor(BleService.DESC_CCC);
    if (descriptor == null) {
        return false;
    }

    byte[] val_set = null;
    if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION) {
        val_set = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
    } else if (request.type == RequestType.CHARACTERISTIC_INDICATION) {
        val_set = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
    } else {
        val_set = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
    }
    if (!descriptor.setValue(val_set)) {
        return false;
    }

    return gatt.writeDescriptor(descriptor);
}

From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java

/**
 * Enables or disables the notifications for given characteristic. This method is SYNCHRONOUS and wait until the
 * {@link BluetoothGattCallback#onDescriptorWrite(BluetoothGatt, BluetoothGattDescriptor, int)} will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If
 * connection state will change, or an error will occur, an exception will be thrown.
 * //from  w  ww.jav  a  2 s .  co m
 * @param gatt
 *            the GATT device
 * @param characteristic
 *            the characteristic to enable or disable notifications for
 * @param enable
 *            <code>true</code> to enable notifications, <code>false</code> to disable them
 * @throws DfuException
 * @throws UploadAbortedException
 */
private void setCharacteristicNotification(final BluetoothGatt gatt,
        final BluetoothGattCharacteristic characteristic, final boolean enable)
        throws DeviceDisconnectedException, DfuException, UploadAbortedException {
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState);
    mErrorState = 0;

    if (mNotificationsEnabled == enable)
        return;

    logi((enable ? "Enabling " : "Disabling") + " notifications...");

    // enable notifications locally
    gatt.setCharacteristicNotification(characteristic, enable);

    // enable notifications on the device
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
            : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(descriptor);

    // We have to wait until device gets disconnected or an error occur
    try {
        synchronized (mLock) {
            while ((mNotificationsEnabled != enable && mConnectionState == STATE_CONNECTED_AND_READY
                    && mErrorState == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }
    if (mAborted)
        throw new UploadAbortedException();
    if (mErrorState != 0)
        throw new DfuException("Unable to set notifications state", mErrorState);
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState);
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * Enables or disables notification on a give characteristic.
 *
 * @param enabled If true, enable notification.  False otherwise.
 *//*from ww w  .  j a  va2s.c om*/
public boolean setCharacteristicNotification(String address, String serviceUuid, String characteristicUuid,
        boolean enabled) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (mBluetoothAdapter == null || bluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return false;
    }
    BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid));
    if (service == null) {
        Log.w(TAG, "Service not found.");
        return false;
    }
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid));
    if (characteristic == null) {
        Log.w(TAG, "characteristic not found.");
        return false;
    }

    bluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    final int charaProp = characteristic.getProperties();
    if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor != null) {
            Log.d(TAG, ">>>> ENABLE_NOTIFICATION_VALUE : " + characteristic.getUuid().toString());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);

            return true;
        } else {
            return false;
        }
    } else if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        if (descriptor != null) {
            Log.d(TAG, ">>>> ENABLE_INDICATION_VALUE : " + characteristic.getUuid().toString());
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);

            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

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

private void createGATT(final BluetoothDevice device) {
    bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() {
        @Override/*from  w  ww  .  j ava2  s.co  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());
            }
        }
    });
}