Java Code Examples for android.bluetooth.BluetoothGattService#getCharacteristic()
The following examples show how to use
android.bluetooth.BluetoothGattService#getCharacteristic() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseMyo.java From myolib with Apache License 2.0 | 7 votes |
private void internalSend(MyoMsg msg) { BluetoothGattService gattService = mBluetoothGatt.getService(msg.getServiceUUID()); if (gattService == null) { Logy.w(TAG, "BluetoothGattService unavailable!: " + msg.toString()); return; } BluetoothGattCharacteristic gattChar = gattService.getCharacteristic(msg.getCharacteristicUUID()); if (gattChar == null) { Logy.w(TAG, "BluetoothGattCharacteristic unavailable!: " + msg.toString()); return; } mDispatchTime = System.currentTimeMillis(); if (msg.getDescriptorUUID() != null) { BluetoothGattDescriptor gattDesc = gattChar.getDescriptor(msg.getDescriptorUUID()); if (gattDesc == null) { Logy.w(TAG, "BluetoothGattDescriptor unavailable!: " + msg.toString()); return; } mMsgCallbackMap.put(msg.getIdentifier(), msg); if (msg instanceof WriteMsg) { gattDesc.setValue(((WriteMsg) msg).getData()); mBluetoothGatt.writeDescriptor(gattDesc); } else { mBluetoothGatt.readDescriptor(gattDesc); } } else { mMsgCallbackMap.put(msg.getIdentifier(), msg); if (msg instanceof WriteMsg) { gattChar.setValue(((WriteMsg) msg).getData()); mBluetoothGatt.writeCharacteristic(gattChar); } else { mBluetoothGatt.readCharacteristic(gattChar); } } Logy.v(TAG, "Processed: " + msg.getIdentifier()); }
Example 2
Source File: MultipleBleService.java From BleLib with Apache License 2.0 | 6 votes |
/** * Enables or disables notification on a give characteristic. * * @param address The address to read from. * @param serviceUUID remote device service uuid * @param characteristicUUID remote device characteristic uuid * @param enabled if notify is true */ public void setCharacteristicNotification(String address, String serviceUUID, String characteristicUUID, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGattMap.get(address) == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } BluetoothGattService service = mBluetoothGattMap.get(address).getService(UUID.fromString(serviceUUID)); BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID)); mBluetoothGattMap.get(address).setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor( UUID.fromString(GattAttributes.DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION)); descriptor.setValue(enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); mBluetoothGattMap.get(address).writeDescriptor(descriptor); }
Example 3
Source File: BTLEDeviceManager.java From BLEService with BSD 3-Clause "New" or "Revised" License | 6 votes |
public boolean writeDescriptor(String address, UUID serviceUUID, UUID characteristicUUID, UUID descriptorUUID, byte[] value) throws DeviceManagerException, DeviceNameNotFoundException { BTDeviceInfo deviceInfo = getDeviceInfo(address); if (deviceInfo == null) { throw new DeviceNameNotFoundException(String.format("%s %s", mContext.getString(R.string.device_not_found), address)); } BluetoothGattService service = deviceInfo.getGatt().getService(serviceUUID); if (service == null) { throw new DeviceManagerException(String.format("%s %s %s", mContext.getString(R.string.service_not_found), address, serviceUUID)); } BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID); if (characteristic == null) { throw new DeviceManagerException(String.format("%s %s %s", mContext.getString(R.string.characteristic_not_found), address, characteristicUUID)); } BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUUID); if (descriptor == null) { throw new DeviceManagerException(String.format("%s %s %s %s", mContext.getString(R.string.descriptor_not_found), address, characteristicUUID, descriptorUUID)); } return writeDescriptor(deviceInfo, characteristic, descriptor, value); }
Example 4
Source File: SerialSocket.java From SimpleBluetoothLeTerminal with MIT License | 5 votes |
@Override boolean connectCharacteristics(BluetoothGattService gattService) { Log.d(TAG, "service cc254x uart"); readCharacteristic = gattService.getCharacteristic(BLUETOOTH_LE_CC254X_CHAR_RW); writeCharacteristic = gattService.getCharacteristic(BLUETOOTH_LE_CC254X_CHAR_RW); return true; }
Example 5
Source File: BlePeripheral.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 5 votes |
public void readDescriptor(@NonNull BluetoothGattService service, UUID characteristicUUID, UUID descriptorUUID, CompletionHandler completionHandler) { final BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID); if (characteristic != null) { readDescriptor(characteristic, descriptorUUID, completionHandler); } else { Log.w(TAG, "read: characteristic not found: " + characteristicUUID.toString()); finishExecutingCommand(BluetoothGatt.GATT_READ_NOT_PERMITTED); } }
Example 6
Source File: GlucoseManager.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(GLS_SERVICE_UUID); if (service != null) { glucoseMeasurementCharacteristic = service.getCharacteristic(GM_CHARACTERISTIC); glucoseMeasurementContextCharacteristic = service.getCharacteristic(GM_CONTEXT_CHARACTERISTIC); recordAccessControlPointCharacteristic = service.getCharacteristic(RACP_CHARACTERISTIC); } return glucoseMeasurementCharacteristic != null && recordAccessControlPointCharacteristic != null; }
Example 7
Source File: OWDevice.java From android-ponewheel with MIT License | 5 votes |
public void setCharacteristicValue(BluetoothGattService gattService, BluetoothGatt gatt, String k, int v) { DeviceCharacteristic dc = getDeviceCharacteristicByKey(k); if (dc != null) { BluetoothGattCharacteristic lc = null; lc = gattService.getCharacteristic(UUID.fromString(dc.uuid.get())); if (lc != null) { ByteBuffer var2 = ByteBuffer.allocate(2); var2.putShort((short) v); lc.setValue(var2.array()); lc.setWriteType(2); gatt.writeCharacteristic(lc); EventBus.getDefault().post(new DeviceStatusEvent("SET " + k + " TO " + v)); } } }
Example 8
Source File: BaseCharacteristicOperation.java From tap-android-sdk with Apache License 2.0 | 5 votes |
protected BluetoothGattCharacteristic extractCharacteristic(BluetoothGatt gatt) { BluetoothGattService s = gatt.getService(service); if (service == null) { postOnError(ErrorStrings.NO_SERVICE); return null; } try { return s.getCharacteristic(characteristic); } catch (NullPointerException e) { return null; } }
Example 9
Source File: BaseBleService.java From bleYan with GNU General Public License v2.0 | 5 votes |
/** * enable notify or disable notify */ public void updateCharacteristicNotification(UUID serviceUUID, UUID CharacteristicUUID, UUID descriptorUUID, boolean enable) { final BluetoothGattService service = mGatt == null ? null : mGatt.getService(serviceUUID); if (service != null) { final BluetoothGattCharacteristic readData = service.getCharacteristic(CharacteristicUUID); mGatt.setCharacteristicNotification(readData, enable); final BluetoothGattDescriptor config = readData.getDescriptor(descriptorUUID); if(config != null) { config.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); write(config); } } }
Example 10
Source File: HRManager.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { super.isOptionalServiceSupported(gatt); final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID); if (service != null) { bodySensorLocationCharacteristic = service.getCharacteristic(BODY_SENSOR_LOCATION_CHARACTERISTIC_UUID); } return bodySensorLocationCharacteristic != null; }
Example 11
Source File: BLEService.java From microbit with Apache License 2.0 | 5 votes |
/** * write repeatedly to (4) to register for the events your app wants to see from the micro:bit. * e.g. write <1,1> to register for a 'DOWN' event on ButtonA. * Any events matching this will then start to be delivered via the MicroBit Event characteristic. * * @param eventService Bluetooth GATT service. * @param enable Enable or disable. */ private void register_AppRequirement(BluetoothGattService eventService, boolean enable) { if(!enable) { return; } BluetoothGattCharacteristic app_requirements = eventService.getCharacteristic(CharacteristicUUIDs.ES_CLIENT_REQUIREMENTS); if(app_requirements != null) { logi("register_AppRequirement() :: found Constants.ES_CLIENT_REQUIREMENTS "); /* Registering for everything at the moment <1,0> which means give me all the events from ButtonA. <2,0> which means give me all the events from ButtonB. <0,0> which means give me all the events from everything. writeCharacteristic(Constants.EVENT_SERVICE.toString(), Constants.ES_CLIENT_REQUIREMENTS.toString(), 0, BluetoothGattCharacteristic.FORMAT_UINT32); */ writeCharacteristic(GattServiceUUIDs.EVENT_SERVICE.toString(), CharacteristicUUIDs.ES_CLIENT_REQUIREMENTS.toString(), EventCategories.SAMSUNG_REMOTE_CONTROL_ID, GattFormats.FORMAT_UINT32); writeCharacteristic(GattServiceUUIDs.EVENT_SERVICE.toString(), CharacteristicUUIDs.ES_CLIENT_REQUIREMENTS.toString(), EventCategories.SAMSUNG_CAMERA_ID, GattFormats.FORMAT_UINT32); writeCharacteristic(GattServiceUUIDs.EVENT_SERVICE.toString(), CharacteristicUUIDs.ES_CLIENT_REQUIREMENTS.toString(), EventCategories.SAMSUNG_ALERTS_ID, GattFormats.FORMAT_UINT32); writeCharacteristic(GattServiceUUIDs.EVENT_SERVICE.toString(), CharacteristicUUIDs.ES_CLIENT_REQUIREMENTS.toString(), EventCategories.SAMSUNG_SIGNAL_STRENGTH_ID, GattFormats.FORMAT_UINT32); writeCharacteristic(GattServiceUUIDs.EVENT_SERVICE.toString(), CharacteristicUUIDs.ES_CLIENT_REQUIREMENTS.toString(), EventCategories.SAMSUNG_DEVICE_INFO_ID, GattFormats.FORMAT_UINT32); //writeCharacteristic(GattServiceUUIDs.EVENT_SERVICE.toString(), CharacteristicUUIDs // .ES_CLIENT_REQUIREMENTS.toString(), EventCategories.SAMSUNG_TELEPHONY_ID, // GattFormats.FORMAT_UINT32); } }
Example 12
Source File: ConnectionImpl.java From easyble-x with Apache License 2.0 | 5 votes |
@Nullable @Override public BluetoothGattCharacteristic getCharacteristic(UUID service, UUID characteristic) { if (service != null && characteristic != null && bluetoothGatt != null) { BluetoothGattService gattService = bluetoothGatt.getService(service); if (gattService != null) { return gattService.getCharacteristic(characteristic); } } return null; }
Example 13
Source File: ProximityManager.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { super.isOptionalServiceSupported(gatt); final BluetoothGattService iaService = gatt.getService(IMMEDIATE_ALERT_SERVICE_UUID); if (iaService != null) { alertLevelCharacteristic = iaService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID); } return alertLevelCharacteristic != null; }
Example 14
Source File: BleSensor.java From BLE-Heart-rate-variability-demo with MIT License | 5 votes |
private BluetoothGattCharacteristic getCharacteristic(BluetoothGatt bluetoothGatt, String uuid) { final UUID serviceUuid = UUID.fromString(getServiceUUID()); final UUID characteristicUuid = UUID.fromString(uuid); final BluetoothGattService service = bluetoothGatt.getService(serviceUuid); return service.getCharacteristic(characteristicUuid); }
Example 15
Source File: BleService.java From BleLib with Apache License 2.0 | 5 votes |
/** * Request a read on a given {@code BluetoothGattCharacteristic}, specific service UUID * and characteristic UUID. The read result is reported asynchronously through the * {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, * android.bluetooth.BluetoothGattCharacteristic, int)} callback. * * @param serviceUUID remote device service uuid * @param characteristicUUID remote device characteristic uuid */ public void readCharacteristic(String serviceUUID, String characteristicUUID) { if (mBluetoothGatt != null) { BluetoothGattService service = mBluetoothGatt.getService(UUID.fromString(serviceUUID)); BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID)); mBluetoothGatt.readCharacteristic(characteristic); } }
Example 16
Source File: GattPlugin.java From bitgatt with Mozilla Public License 2.0 | 4 votes |
private void addLocalGattServerCharacteristicDescriptor(DumperContext dumpContext, Iterator<String> args) throws InterruptedException { int index = 0; String localServiceUuid = null; String characteristicUuid = null; String descriptorUuid = null; int permissions = -1; while (args.hasNext()) { if (index == 0) { localServiceUuid = args.next(); } else if (index == 1) { characteristicUuid = args.next(); } else if (index == 2) { descriptorUuid = args.next(); } else if (index == 3) { permissions = Integer.parseInt(args.next()); } index++; } if (localServiceUuid == null) { logError(dumpContext, new IllegalArgumentException("No local server service uuid provided")); return; } else if (characteristicUuid == null) { logError(dumpContext, new IllegalArgumentException("No characteristic uuid provided")); return; } else if (descriptorUuid == null) { logError(dumpContext, new IllegalArgumentException("No characteristic descriptor uuid provided")); return; } else if (permissions == -1) { logError(dumpContext, new IllegalArgumentException("No characteristic permissions provided")); return; } GattServerConnection conn = fitbitGatt.getServer(); if (conn == null) { logError(dumpContext, new IllegalArgumentException("No valid connection for provided mac")); return; } BluetoothGattService localService = conn.getServer().getService(UUID.fromString(localServiceUuid)); if (localService == null) { logError(dumpContext, new IllegalStateException("No local service for the uuid" + localServiceUuid + "found")); return; } BluetoothGattCharacteristic localCharacteristic = localService.getCharacteristic(UUID.fromString(characteristicUuid)); if (!localService.addCharacteristic(localCharacteristic)) { logError(dumpContext, new IllegalStateException("Couldn't get characteristic from service")); return; } BluetoothGattDescriptor localDescriptor = new BluetoothGattDescriptor(UUID.fromString(descriptorUuid), permissions); if (!localCharacteristic.addDescriptor(localDescriptor)) { logError(dumpContext, new IllegalStateException("Couldn't add descriptor " + descriptorUuid + " to the local characteristic " + characteristicUuid + " on the service " + localServiceUuid)); return; } // don't worry, this instance can only be registered once, but we do want for it to have // a fresh dumper context serverConnectionListener.setContext(dumpContext); conn.registerConnectionEventListener(serverConnectionListener); CountDownLatch cdl = new CountDownLatch(1); AddGattServerServiceCharacteristicDescriptorTransaction tx = new AddGattServerServiceCharacteristicDescriptorTransaction(conn, GattState.ADD_SERVICE_CHARACTERISTIC_DESCRIPTOR_SUCCESS, localService, localCharacteristic, localDescriptor); conn.runTx(tx, result -> { logSuccessOrFailure(result, dumpContext, "Successfully added " + localDescriptor.getUuid().toString() + " to " + localCharacteristic.getUuid().toString() + " on " + localService.getUuid().toString(), "Failed to add " + localDescriptor.getUuid().toString() + " to " + localCharacteristic.getUuid().toString() + " on " + localService.getUuid().toString()); cdl.countDown(); }); cdl.await(); }
Example 17
Source File: DfuBaseService.java From microbit with Apache License 2.0 | 4 votes |
private int flashingWithPairCode(Intent intent) { mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS); mDeviceName = intent.getStringExtra(EXTRA_DEVICE_NAME); sendLogBroadcast(LOG_LEVEL_VERBOSE, "Connecting to DFU target 2..."); if (!makeGattConnection(mDeviceAddress)) return 5; logi("Phase2 s"); updateProgressNotification(PROGRESS_VALIDATING); //For Stats purpose only { BluetoothGattService deviceService = gatt.getService(DEVICE_INFORMATION_SERVICE_UUID); if (deviceService != null) { BluetoothGattCharacteristic firmwareCharacteristic = deviceService.getCharacteristic(FIRMWARE_REVISION_UUID); if (firmwareCharacteristic != null) { String firmware = null; firmware = readCharacteristicNoFailure(gatt, firmwareCharacteristic); logi("Micro:bit firmware version String = " + firmware); sendStatsMicroBitFirmware(firmware); } else { logi("Error Cannot find FIRMWARE_REVISION_UUID"); } } else { logi("Error Cannot find DEVICE_INFORMATION_SERVICE_UUID"); } }//For Stats purpose only Ends int rc = 1; BluetoothGattService fps = gatt.getService(MICROBIT_FLASH_SERVICE_UUID); if (fps == null) { logi("Error Cannot find MICROBIT_FLASH_SERVICE_UUID"); sendLogBroadcast(LOG_LEVEL_WARNING, "Upload aborted"); terminateConnection(gatt, PROGRESS_SERVICE_NOT_FOUND); return 6; } final BluetoothGattCharacteristic sfpc1 = fps.getCharacteristic(MICROBIT_FLASH_SERVICE_CONTROL_CHARACTERISTIC_UUID); if (sfpc1 == null) { logi("Error Cannot find MICROBIT_FLASH_SERVICE_CONTROL_CHARACTERISTIC_UUID"); sendLogBroadcast(LOG_LEVEL_WARNING, "Upload aborted"); terminateConnection(gatt, PROGRESS_SERVICE_NOT_FOUND); return 6; } sfpc1.setValue(1, BluetoothGattCharacteristic.FORMAT_UINT8, 0); try { logi("Writing Flash Command ...."); writeCharacteristic(gatt, sfpc1); rc = 0; } catch (Exception e) { e.printStackTrace(); Log.e(TAG, e.toString()); } if (rc == 0) { sendProgressBroadcast(PROGRESS_WAITING_REBOOT); //Wait for the device to reboot. waitUntilDisconnected(); waitUntilConnected(); logi("Refreshing the cache before discoverServices() for Android version " + Build.VERSION.SDK_INT); refreshDeviceCache(gatt, true); do { logi("Calling phase 3"); mError = 0; intent = phase3(intent); resultReceiver = null; gatt.disconnect(); waitUntilDisconnected(); if (mConnectionState != STATE_CLOSED) { close(gatt); } gatt = null; logi("End phase 3"); } while (intent != null); } logi("Phase2 e"); return rc; }
Example 18
Source File: BlePeripheral.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 4 votes |
public @Nullable BluetoothGattCharacteristic getCharacteristic(@NonNull UUID characteristicUUID, @NonNull UUID serviceUUID) { // This function requires that service discovery has been completed for the given device. BluetoothGattService service = getService(serviceUUID); return service == null ? null : service.getCharacteristic(characteristicUUID); }
Example 19
Source File: ConnectionImpl.java From easyble-x with Apache License 2.0 | 4 votes |
private void executeRequest(GenericRequest request) { currentRequest = request; connHandler.sendMessageDelayed(Message.obtain(connHandler, MSG_REQUEST_TIMEOUT, request), configuration.requestTimeoutMillis); if (bluetoothAdapter.isEnabled()) { if (bluetoothGatt != null) { switch (request.type) { case READ_RSSI: if (!bluetoothGatt.readRemoteRssi()) { handleFailedCallback(request, REQUEST_FAIL_TYPE_REQUEST_FAILED, true); } break; case CHANGE_MTU: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (!bluetoothGatt.requestMtu((int) request.value)) { handleFailedCallback(request, REQUEST_FAIL_TYPE_REQUEST_FAILED, true); } } break; case READ_PHY: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { bluetoothGatt.readPhy(); } break; case SET_PREFERRED_PHY: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int[] options = (int[]) request.value; bluetoothGatt.setPreferredPhy(options[0], options[1], options[2]); } break; default: BluetoothGattService gattService = bluetoothGatt.getService(request.service); if (gattService != null) { BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(request.characteristic); if (characteristic != null) { switch (request.type) { case SET_NOTIFICATION: case SET_INDICATION: executeIndicationOrNotification(request, characteristic); break; case READ_CHARACTERISTIC: executeReadCharacteristic(request, characteristic); break; case READ_DESCRIPTOR: executeReadDescriptor(request, characteristic); break; case WRITE_CHARACTERISTIC: executeWriteCharacteristic(request, characteristic); break; } } else { handleFailedCallback(request, REQUEST_FAIL_TYPE_CHARACTERISTIC_NOT_EXIST, true); } } else { handleFailedCallback(request, REQUEST_FAIL_TYPE_SERVICE_NOT_EXIST, true); } break; } } else { handleFailedCallback(request, REQUEST_FAIL_TYPE_GATT_IS_NULL, true); } } else { handleFailedCallback(request, REQUEST_FAIL_TYPE_BLUETOOTH_ADAPTER_DISABLED, true); } }
Example 20
Source File: MultipleBleService.java From BleLib with Apache License 2.0 | 3 votes |
/** * Request a read on a given {@code BluetoothGattCharacteristic}, specific service UUID * and characteristic UUID. The read result is reported asynchronously through the * {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, * android.bluetooth.BluetoothGattCharacteristic, int)} callback. * * @param address The address to read from. * @param serviceUUID remote device service uuid * @param characteristicUUID remote device characteristic uuid */ public void readCharacteristic(String address, String serviceUUID, String characteristicUUID) { if (mBluetoothGattMap.get(address) != null) { BluetoothGattService service = mBluetoothGattMap.get(address).getService(UUID.fromString(serviceUUID)); BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID)); mBluetoothGattMap.get(address).readCharacteristic(characteristic); } }