Example usage for android.bluetooth BluetoothGattCharacteristic addDescriptor

List of usage examples for android.bluetooth BluetoothGattCharacteristic addDescriptor

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattCharacteristic addDescriptor.

Prototype

public boolean addDescriptor(BluetoothGattDescriptor descriptor) 

Source Link

Document

Adds a descriptor to this characteristic.

Usage

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void addServices(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "addServices");
    if (!isOpenGattServer) {
        mBluetoothGattServer = mBluetoothManager.openGattServer(mContext, mGattServerCallback);
        isOpenGattServer = true;//ww  w. j  av  a  2 s .  c o m
    }
    addServiceCC = callbackContext;
    JSONArray services = Tools.getArray(json, Tools.SERVICES);
    gattServerSum = services.length();
    for (int i = 0; i < services.length(); i++) {
        String uniqueID = Tools.getData(services, i, Tools.UINQUE_ID);
        int serviceType = -1;
        if (Tools.getData(services, i, Tools.SERVICE_TYPE).equals("0")) {
            serviceType = BluetoothGattService.SERVICE_TYPE_PRIMARY;
        } else {
            serviceType = BluetoothGattService.SERVICE_TYPE_SECONDARY;
        }
        UUID serviceUUID = UUID.fromString(Tools.getData(services, i, Tools.SERVICE_UUID));
        BluetoothGattService service = new BluetoothGattService(serviceUUID, serviceType);
        JSONArray characteristics = Tools.getArray(services, i, Tools.CHARACTERISTICS);
        for (int j = 0; j < characteristics.length(); j++) {
            byte[] characteristicValue = Tools
                    .decodeBase64(Tools.getData(characteristics, Tools.CHARACTERISTIC_VALUE));
            UUID characteristicUUID = UUID
                    .fromString(Tools.getData(characteristics, Tools.CHARACTERISTIC_UUID));
            int characteristicProperty = Tools
                    .encodeProperty(Tools.getArray(characteristics, Tools.CHARACTERISTIC_PROPERTY));
            int characteristicPermission = Tools
                    .encodePermission(Tools.getArray(characteristics, Tools.CHARACTERISTIC_PERMISSION));
            BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUUID,
                    characteristicProperty, characteristicPermission);
            characteristic.setValue(characteristicValue);
            JSONArray descriptors = Tools.getArray(characteristics, j, Tools.DESCRIPTORS);
            for (int k = 0; k < descriptors.length(); k++) {
                byte[] descriptorValue = Tools.decodeBase64(Tools.getData(descriptors, Tools.DESCRIPTOR_VALUE));
                UUID descriptorUUID = UUID.fromString(Tools.getData(descriptors, Tools.DESCRIPTOR_UUID));
                int descriptorPermission = Tools
                        .encodePermission(Tools.getArray(descriptors, Tools.DESCRIPTOR_PERMISSION));
                BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(descriptorUUID,
                        descriptorPermission);
                descriptor.setValue(descriptorValue);
                characteristic.addDescriptor(descriptor);
            }
            service.addCharacteristic(characteristic);
        }
        if (mBluetoothGattServer.addService(service)) {
            localServices.put(uniqueID, service);
        }
    }
}