Example usage for android.bluetooth BluetoothGatt getService

List of usage examples for android.bluetooth BluetoothGatt getService

Introduction

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

Prototype

public BluetoothGattService getService(UUID uuid) 

Source Link

Document

Returns a BluetoothGattService , if the requested UUID is supported by the remote device.

Usage

From source file:Main.java

public static BluetoothGattService getService(BluetoothGatt gatt, String serviceUUID) {
    return gatt.getService(UUID.fromString(serviceUUID));
}

From source file:Main.java

public static BluetoothGattCharacteristic findBluetoothGattCharacteristic(BluetoothGatt gatt, UUID serviceUuid,
        UUID characteristicUuid) {
    if (gatt == null) {
        return null;
    }//from  w w  w. j av a 2s .c  o m
    BluetoothGattService service = gatt.getService(serviceUuid);
    if (service == null) {
        return null;
    }

    return service.getCharacteristic(characteristicUuid);
}

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 .ja v a2  s  .c  o m*/
            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 BleGattService getService(String address, UUID uuid) {
    BluetoothGatt gatt = mBluetoothGatts.get(address);
    if (gatt == null) {
        return null;
    }/*from w  w  w  .  j  ava  2  s  .  c  o m*/

    BluetoothGattService service = gatt.getService(uuid);
    if (service == null) {
        return null;
    } else {
        return new BleGattService(service);
    }
}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

BluetoothGattService getService(String address, UUID serviceId) {
    if (DEBUG)/*  w  w w .ja v  a 2s.  c  om*/
        Log.d(TAG, "lookup for service: " + serviceId);
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    return bluetoothGatt == null ? null : bluetoothGatt.getService(serviceId);
}

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

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.i(TAG, "Services Discovered");
    if (status != BluetoothGatt.GATT_SUCCESS) {
        Log.e(TAG, "Service discovery failed!");
        return;// w  w  w. j  a  va  2  s .c  o m
    }

    try {
        mHtml = new FileOutputStream(getHtmlFile());
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found", e);
        return;
    }

    characteristic = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_WEBPAGE_UUID);
    gatt.readCharacteristic(characteristic);
}

From source file:de.blinkt.openvpn.bluetooth.service.UartService.java

/**
 * When the device is bonded and has the Generic Attribute service and the Service Changed characteristic this method enables indications on this characteristic.
 * In case one of the requirements is not fulfilled this method returns <code>false</code>.
 *
 * @return <code>true</code> when the request has been sent, <code>false</code> when the device is not bonded, does not have the Generic Attribute service, the GA service does not have
 * the Service Changed characteristic or this characteristic does not have the CCCD.
 *//*from  w w w  .j  a  v a  2s.  co  m*/
public boolean ensureServiceChangedEnabled() {
    final BluetoothGatt gatt = mBluetoothGatt;
    if (gatt == null)
        return false;

    // The Service Changed indications have sense only on bonded devices
    final BluetoothDevice device = gatt.getDevice();
    if (device.getBondState() != BluetoothDevice.BOND_BONDED)
        return false;

    final BluetoothGattService gaService = gatt.getService(GENERIC_ATTRIBUTE_SERVICE);
    if (gaService == null)
        return false;

    final BluetoothGattCharacteristic scCharacteristic = gaService
            .getCharacteristic(SERVICE_CHANGED_CHARACTERISTIC);
    return scCharacteristic != null;
}

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

/**
 * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
 * asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
 * callback./*  w  ww .j a v  a2  s.  com*/
 *
 */
public boolean readCharacteristic(String address, String serviceUuid, String characteristicUuid) {
    Log.d(TAG, "readCharacteristic 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;
    }
    boolean result = bluetoothGatt.readCharacteristic(characteristic);
    Log.d(TAG, "Read charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}

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

public boolean writeRemoteCharacteristic(String address, String serviceUuid, String characteristicUuid,
        byte[] value) {
    Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = "
            + characteristicUuid);//from w  ww  .j a v a2 s  .  co m
    if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid)
            || StringUtils.isEmptyString(characteristicUuid)) {
        Log.w(TAG, "Unknown parameter");
        return false;
    }
    if (value == null) {
        Log.w(TAG, "value is empty");
        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;
    }

    characteristic.setValue(value);
    boolean result = bluetoothGatt.writeCharacteristic(characteristic);
    Log.d(TAG, "Write charac uuid = " + characteristic.getUuid().toString() + ", result = " + result);

    return result;
}

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   w w  w.j  a  v  a2s .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;
    }
}