Example usage for android.bluetooth BluetoothGattService addCharacteristic

List of usage examples for android.bluetooth BluetoothGattService addCharacteristic

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGattService addCharacteristic.

Prototype

public boolean addCharacteristic(BluetoothGattCharacteristic characteristic) 

Source Link

Document

Add a characteristic to this service.

Usage

From source file:org.physical_web.physicalweb.FatBeaconBroadcastService.java

private void initGattServer() {
    mGattServer = mBluetoothManager.openGattServer(this, mGattServerCallback);
    BluetoothGattService service = new BluetoothGattService(UUID.fromString(SERVICE_UUID),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic webpage = new BluetoothGattCharacteristic(CHARACTERISTIC_WEBPAGE_UUID,
            BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
    service.addCharacteristic(webpage);
    mGattServer.addService(service);// w  ww.j a va2 s  . c o m
}

From source file:io.v.android.libs.discovery.ble.BlePlugin.java

private BluetoothGattService convertToService(Advertisement adv) throws IOException {
    Map<UUID, byte[]> attributes = BleAdvertisementConverter.vAdvertismentToBleAttr(adv);
    BluetoothGattService service = new BluetoothGattService(UUIDUtil.UuidToUUID(adv.getServiceUuid()),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    for (Map.Entry<UUID, byte[]> entry : attributes.entrySet()) {
        BluetoothGattCharacteristic ch = new BluetoothGattCharacteristic(entry.getKey(), 0,
                BluetoothGattCharacteristic.PERMISSION_READ);
        ch.setValue(entry.getValue());//from  w  ww .j  a va  2 s  . c  o m
        service.addCharacteristic(ch);
    }
    return service;
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

public void addService(final String uuid, Map<String, byte[]> characteristics) {
    BluetoothGattService service = new BluetoothGattService(UUID.fromString(uuid),
            BluetoothGattService.SERVICE_TYPE_PRIMARY);
    for (Map.Entry<String, byte[]> entry : characteristics.entrySet()) {
        BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
                UUID.fromString(entry.getKey()), BluetoothGattCharacteristic.PROPERTY_READ,
                BluetoothGattCharacteristic.PERMISSION_READ);
        characteristic.setValue(entry.getValue());
        service.addCharacteristic(characteristic);
    }// w  ww. j av  a2 s . co m

    synchronized (this) {
        if (mServices.put(uuid, service) != null) {
            throw new IllegalStateException("already being advertised: " + uuid);
        }
        if (mEnabled) {
            startAdvertising(service);
        }
    }
}

From source file:edu.umich.eecs.lab11.camera.CameraFragment.java

private void initServer() {
    BluetoothGattService service = new BluetoothGattService(DeviceProfile.SERVICE_UUID,
            BluetoothGattService.SERVICE_TYPE_PRIMARY);

    BluetoothGattCharacteristic offsetCharacteristic = new BluetoothGattCharacteristic(
            DeviceProfile.CHARACTERISTIC_UUID,
            //Read+write permissions
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
            BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

    service.addCharacteristic(offsetCharacteristic);
    mGattServer.addService(service);// w  w  w .j  a  va 2 s  .  c  o  m
}

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;//from  w ww.  java  2s  .com
    }
    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);
        }
    }
}