Here are the examples of the java api class android.bluetooth.BluetoothGatt taken from open source projects.
1. AndroidBle#writeCharacteristic()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean writeCharacteristic(String address, BleGattCharacteristic characteristic) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null) { return false; } Log.d("blelib", new String(Hex.encodeHex(characteristic.getGattCharacteristicA().getValue()))); return gatt.writeCharacteristic(characteristic.getGattCharacteristicA()); }
2. BleManager#writeCharacteristic()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
@Override public final boolean writeCharacteristic(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) == 0) return false; return gatt.writeCharacteristic(characteristic); }
3. BleManager#readCharacteristic()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
@Override public final boolean readCharacteristic(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false; return gatt.readCharacteristic(characteristic); }
4. BleManager#enableIndications()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
@Override public final boolean enableIndications(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0) return false; gatt.setCharacteristicNotification(characteristic, true); final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); return gatt.writeDescriptor(descriptor); } return false; }
5. BleManager#enableNotifications()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
@Override public final boolean enableNotifications(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) return false; gatt.setCharacteristicNotification(characteristic, true); final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); return gatt.writeDescriptor(descriptor); } return false; }
6. BleManager#writeCharacteristic()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
/** * Writes the characteristic value to the given characteristic. * * @param characteristic the characteristic to write to * @return true if request has been sent */ protected final boolean writeCharacteristic(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) == 0) return false; Logger.v(mLogSession, "Writing characteristic " + characteristic.getUuid() + " (" + getWriteType(characteristic.getWriteType()) + ")"); Logger.d(mLogSession, "gatt.writeCharacteristic(" + characteristic.getUuid() + ")"); return gatt.writeCharacteristic(characteristic); }
7. BleManager#readCharacteristic()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
/** * Sends the read request to the given characteristic. * * @param characteristic the characteristic to read * @return true if request has been sent */ protected final boolean readCharacteristic(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false; Logger.v(mLogSession, "Reading characteristic " + characteristic.getUuid()); Logger.d(mLogSession, "gatt.readCharacteristic(" + characteristic.getUuid() + ")"); return gatt.readCharacteristic(characteristic); }
8. BleManager#enableIndications()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
/** * Enables indications on given characteristic * * @return true is the request has been sent, false if one of the arguments was <code>null</code> or the characteristic does not have the CCCD. */ protected final boolean enableIndications(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0) return false; Logger.d(mLogSession, "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)"); gatt.setCharacteristicNotification(characteristic, true); final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); Logger.v(mLogSession, "Enabling indications for " + characteristic.getUuid()); Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x02-00)"); return gatt.writeDescriptor(descriptor); } return false; }
9. BleManager#enableNotifications()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
/** * Enables notifications on given characteristic * * @return true is the request has been sent, false if one of the arguments was <code>null</code> or the characteristic does not have the CCCD. */ protected final boolean enableNotifications(final BluetoothGattCharacteristic characteristic) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null || characteristic == null) return false; // Check characteristic property final int properties = characteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) return false; Logger.d(mLogSession, "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)"); gatt.setCharacteristicNotification(characteristic, true); final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); Logger.v(mLogSession, "Enabling notifications for " + characteristic.getUuid()); Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)"); return gatt.writeDescriptor(descriptor); } return false; }
10. AndroidBle#requestStopNotification()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean requestStopNotification(String address, BleGattCharacteristic characteristic) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; } mService.addBleRequest(new BleRequest(RequestType.CHARACTERISTIC_NOTIFICATION, gatt.getDevice().getAddress(), characteristic)); return true; }
11. AndroidBle#requestIndication()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean requestIndication(String address, BleGattCharacteristic characteristic) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; } mService.addBleRequest(new BleRequest(RequestType.CHARACTERISTIC_INDICATION, gatt.getDevice().getAddress(), characteristic)); return true; }
12. AndroidBle#requestWriteCharacteristic()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean requestWriteCharacteristic(String address, BleGattCharacteristic characteristic, String remark) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; } mService.addBleRequest(new BleRequest(RequestType.WRITE_CHARACTERISTIC, gatt.getDevice().getAddress(), characteristic, remark)); return true; }
13. AndroidBle#requestCharacteristicNotification()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean requestCharacteristicNotification(String address, BleGattCharacteristic characteristic) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; } mService.addBleRequest(new BleRequest(RequestType.CHARACTERISTIC_NOTIFICATION, gatt.getDevice().getAddress(), characteristic)); return true; }
14. AndroidBle#getService()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public BleGattService getService(String address, UUID uuid) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null) { return null; } BluetoothGattService service = gatt.getService(uuid); if (service == null) { return null; } else { return new BleGattService(service); } }
15. AndroidBle#discoverServices()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean discoverServices(String address) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null) { return false; } boolean ret = gatt.discoverServices(); if (!ret) { disconnect(address); } return ret; }
16. AndroidBle#requestReadCharacteristic()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean requestReadCharacteristic(String address, BleGattCharacteristic characteristic) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; } mService.addBleRequest(new BleRequest(RequestType.READ_CHARACTERISTIC, gatt.getDevice().getAddress(), characteristic)); return true; }
17. AndroidBle#getServices()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public ArrayList<BleGattService> getServices(String address) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null) { return null; } ArrayList<BleGattService> list = new ArrayList<BleGattService>(); List<BluetoothGattService> services = gatt.getServices(); for (BluetoothGattService s : services) { BleGattService service = new BleGattService(s); // service.setInfo(btQuery.getGattServiceInfo(s.getUuid())); list.add(service); } return list; }
18. AndroidBle#connect()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean connect(String address) { BluetoothDevice device = mBtAdapter.getRemoteDevice(address); BluetoothGatt gatt = device.connectGatt(mService, false, mGattCallback); if (gatt == null) { mBluetoothGatts.remove(address); return false; } else { // TODO: if state is 141, it can be connected again after about 15 // seconds mBluetoothGatts.put(address, gatt); return true; } }
19. BleManager#readBatteryLevel()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
/** * Reads the battery level from the device. * * @return true if request has been sent */ public final boolean readBatteryLevel() { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null) return false; final BluetoothGattService batteryService = gatt.getService(BATTERY_SERVICE); if (batteryService == null) return false; final BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC); if (batteryLevelCharacteristic == null) return false; // Check characteristic property final int properties = batteryLevelCharacteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_READ) == 0) { return setBatteryNotifications(true); } Logger.a(mLogSession, "Reading battery level..."); return readCharacteristic(batteryLevelCharacteristic); }
20. AndroidBle#characteristicNotification()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean characteristicNotification(String address, BleGattCharacteristic characteristic) { BleRequest request = mService.getCurrentRequest(); BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; } boolean enable = true; if (request.type == RequestType.CHARACTERISTIC_STOP_NOTIFICATION) { enable = false; } BluetoothGattCharacteristic c = characteristic.getGattCharacteristicA(); if (!gatt.setCharacteristicNotification(c, enable)) { return false; } BluetoothGattDescriptor descriptor = c.getDescriptor(BleService.DESC_CCC); if (descriptor == null) { return false; } byte[] val_set = null; if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION) { val_set = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE; } else if (request.type == RequestType.CHARACTERISTIC_INDICATION) { val_set = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE; } else { val_set = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE; } if (!descriptor.setValue(val_set)) { return false; } return gatt.writeDescriptor(descriptor); }
21. AndroidBle#readCharacteristic()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
public boolean readCharacteristic(String address, BleGattCharacteristic characteristic) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null) { return false; } return gatt.readCharacteristic(characteristic.getGattCharacteristicA()); }
22. BleManager#setBatteryNotifications()
Project: Android-nRF-Toolbox
File: BleManager.java
File: BleManager.java
/** * This method tries to enable notifications on the Battery Level characteristic. * * @param enable <code>true</code> to enable battery notifications, false to disable * @return true if request has been sent */ public boolean setBatteryNotifications(final boolean enable) { final BluetoothGatt gatt = mBluetoothGatt; if (gatt == null) { return false; } final BluetoothGattService batteryService = gatt.getService(BATTERY_SERVICE); if (batteryService == null) return false; final BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC); if (batteryLevelCharacteristic == null) return false; // Check characteristic property final int properties = batteryLevelCharacteristic.getProperties(); if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0) return false; gatt.setCharacteristicNotification(batteryLevelCharacteristic, enable); final BluetoothGattDescriptor descriptor = batteryLevelCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID); if (descriptor != null) { if (enable) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); Logger.a(mLogSession, "Enabling battery level notifications..."); Logger.v(mLogSession, "Enabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC); Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)"); } else { descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); Logger.a(mLogSession, "Disabling battery level notifications..."); Logger.v(mLogSession, "Disabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC); Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x00-00)"); } return gatt.writeDescriptor(descriptor); } return false; }
23. AndroidBle#requestConnect()
Project: GizwitsBLE
File: AndroidBle.java
File: AndroidBle.java
@Override public boolean requestConnect(String address) { BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt != null && gatt.getServices().size() == 0) { return false; } mService.addBleRequest(new BleRequest(RequestType.CONNECT_GATT, address)); return true; }