Search in sources :

Example 6 with BluetoothGattDescriptor

use of android.bluetooth.BluetoothGattDescriptor in project Gadgetbridge by Freeyourgadget.

the class PebbleGATTServer method initialize.

boolean initialize() {
    BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothGattServer = bluetoothManager.openGattServer(mContext, this);
    if (mBluetoothGattServer == null) {
        return false;
    }
    BluetoothGattService pebbleGATTService = new BluetoothGattService(SERVER_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    pebbleGATTService.addCharacteristic(new BluetoothGattCharacteristic(READ_CHARACTERISTICS, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ));
    writeCharacteristics = new BluetoothGattCharacteristic(WRITE_CHARACTERISTICS, BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE | BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_WRITE);
    writeCharacteristics.addDescriptor(new BluetoothGattDescriptor(CHARACTERISTICS_CONFIGURATION_DESCRIPTOR, BluetoothGattDescriptor.PERMISSION_WRITE));
    pebbleGATTService.addCharacteristic(writeCharacteristics);
    mBluetoothGattServer.addService(pebbleGATTService);
    return true;
}
Also used : BluetoothManager(android.bluetooth.BluetoothManager) BluetoothGattService(android.bluetooth.BluetoothGattService) BluetoothGattDescriptor(android.bluetooth.BluetoothGattDescriptor) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 7 with BluetoothGattDescriptor

use of android.bluetooth.BluetoothGattDescriptor in project Gadgetbridge by Freeyourgadget.

the class PebbleGATTClient method setMTU.

private void setMTU(BluetoothGatt gatt) {
    LOG.info("setting MTU");
    BluetoothGattCharacteristic characteristic = gatt.getService(SERVICE_UUID).getCharacteristic(MTU_CHARACTERISTIC);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_CONFIGURATION_DESCRIPTOR);
    // unknown
    descriptor.setValue(new byte[] { 0x0b, 0x01 });
    gatt.writeCharacteristic(characteristic);
}
Also used : BluetoothGattDescriptor(android.bluetooth.BluetoothGattDescriptor) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 8 with BluetoothGattDescriptor

use of android.bluetooth.BluetoothGattDescriptor in project Gadgetbridge by Freeyourgadget.

the class PebbleGATTClient method subscribeToMTU.

private void subscribeToMTU(BluetoothGatt gatt) {
    LOG.info("subscribing to mtu characteristic");
    BluetoothGattDescriptor descriptor = gatt.getService(SERVICE_UUID).getCharacteristic(MTU_CHARACTERISTIC).getDescriptor(CHARACTERISTIC_CONFIGURATION_DESCRIPTOR);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(descriptor);
    gatt.setCharacteristicNotification(gatt.getService(SERVICE_UUID).getCharacteristic(MTU_CHARACTERISTIC), true);
}
Also used : BluetoothGattDescriptor(android.bluetooth.BluetoothGattDescriptor)

Example 9 with BluetoothGattDescriptor

use of android.bluetooth.BluetoothGattDescriptor in project Gadgetbridge by Freeyourgadget.

the class PebbleGATTClient method subscribeToConnectivity.

private void subscribeToConnectivity(BluetoothGatt gatt) {
    LOG.info("subscribing to connectivity characteristic");
    BluetoothGattDescriptor descriptor = gatt.getService(SERVICE_UUID).getCharacteristic(CONNECTIVITY_CHARACTERISTIC).getDescriptor(CHARACTERISTIC_CONFIGURATION_DESCRIPTOR);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(descriptor);
    gatt.setCharacteristicNotification(gatt.getService(SERVICE_UUID).getCharacteristic(CONNECTIVITY_CHARACTERISTIC), true);
}
Also used : BluetoothGattDescriptor(android.bluetooth.BluetoothGattDescriptor)

Example 10 with BluetoothGattDescriptor

use of android.bluetooth.BluetoothGattDescriptor in project Gadgetbridge by Freeyourgadget.

the class NotifyAction method run.

@Override
public boolean run(BluetoothGatt gatt) {
    boolean result = gatt.setCharacteristicNotification(getCharacteristic(), enableFlag);
    if (result) {
        BluetoothGattDescriptor notifyDescriptor = getCharacteristic().getDescriptor(UUID_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION);
        if (notifyDescriptor != null) {
            int properties = getCharacteristic().getProperties();
            if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                LOG.debug("use NOTIFICATION");
                notifyDescriptor.setValue(enableFlag ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
                result = gatt.writeDescriptor(notifyDescriptor);
            } else if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
                LOG.debug("use INDICATION");
                notifyDescriptor.setValue(enableFlag ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
                result = gatt.writeDescriptor(notifyDescriptor);
                hasWrittenDescriptor = true;
            } else {
                hasWrittenDescriptor = false;
            }
        } else {
            LOG.warn("Descriptor CLIENT_CHARACTERISTIC_CONFIGURATION for characteristic " + getCharacteristic().getUuid() + " is null");
            hasWrittenDescriptor = false;
        }
    } else {
        hasWrittenDescriptor = false;
        LOG.error("Unable to enable notification for " + getCharacteristic().getUuid());
    }
    return result;
}
Also used : BluetoothGattDescriptor(android.bluetooth.BluetoothGattDescriptor)

Aggregations

BluetoothGattDescriptor (android.bluetooth.BluetoothGattDescriptor)10 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)5 BluetoothGattService (android.bluetooth.BluetoothGattService)3 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 BluetoothDevice (android.bluetooth.BluetoothDevice)1 BluetoothGatt (android.bluetooth.BluetoothGatt)1 BluetoothManager (android.bluetooth.BluetoothManager)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1