Example usage for android.bluetooth BluetoothGattService BluetoothGattService

List of usage examples for android.bluetooth BluetoothGattService BluetoothGattService

Introduction

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

Prototype

public BluetoothGattService(UUID uuid, int serviceType) 

Source Link

Document

Create a new BluetoothGattService.

Usage

From source file:com.example.emulator.EmulatorFragment.java

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public EmulatorFragment() {
    mBatteryLevelCharacteristic = new BluetoothGattCharacteristic(BATTERY_LEVEL_UUID,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_READ);

    mBatteryLevelCharacteristic.addDescriptor(Peripheral.getClientCharacteristicConfigurationDescriptor());

    mBatteryLevelCharacteristic/*from ww w . j  a v  a2  s  . c o  m*/
            .addDescriptor(Peripheral.getCharacteristicUserDescriptionDescriptor(BATTERY_LEVEL_DESCRIPTION));

    mBatteryService = new BluetoothGattService(BATTERY_SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    mBatteryService.addCharacteristic(mBatteryLevelCharacteristic);
}

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);
    }/*from   w ww  .  j ava  2s .co  m*/

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

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());//  www . jav  a2  s .c o  m
        service.addCharacteristic(ch);
    }
    return service;
}

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);//from   w w w  .j  a  va 2 s .c o m
    mGattServer.addService(service);
}

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  ww w  . j  a v a 2s  .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);
        }
    }
}

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  ava  2  s  .  c o m*/
}