Refine search
void swapScanResult(RxBleDeviceServices services) { data.clear(); for (BluetoothGattService service : services.getBluetoothGattServices()) { // Add service data.add(new AdapterItem(AdapterItem.SERVICE, getServiceType(service), service.getUuid())); final List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); for (BluetoothGattCharacteristic characteristic : characteristics) { data.add(new AdapterItem(AdapterItem.CHARACTERISTIC, describeProperties(characteristic), characteristic.getUuid())); } } notifyDataSetChanged(); }
public boolean writeCharacteristic(){ //check mBluetoothGatt is available if (mBluetoothGatt == null) { Log.e(TAG, "lost connection"); return false; } BluetoothGattService Service = mBluetoothGatt.getService(your Services); if (Service == null) { Log.e(TAG, "service not found!"); return false; } BluetoothGattCharacteristic charac = Service .getCharacteristic(your characteristic); if (charac == null) { Log.e(TAG, "char not found!"); return false; } byte[] value = new byte[1]; value[0] = (byte) (21 & 0xFF); charac.setValue(value); boolean status = mBluetoothGatt.writeCharacteristic(charac); return status; }
/** * Add a {@link BluetoothGattService} to the device. Calling this method is not required. * * @param uuid service UUID * @param characteristics characteristics that the service should report. Use {@link CharacteristicsBuilder} to create them. */ public DeviceBuilder addService(@NonNull UUID uuid, @NonNull List<BluetoothGattCharacteristic> characteristics) { BluetoothGattService bluetoothGattService = new BluetoothGattService(uuid, 0); for (BluetoothGattCharacteristic characteristic : characteristics) { bluetoothGattService.addCharacteristic(characteristic); } rxBleDeviceServices.getBluetoothGattServices().add(bluetoothGattService); return this; }
private void appendServiceHeader(StringBuilder descriptionBuilder, BluetoothGattService bluetoothGattService) { descriptionBuilder .append("\n") .append(createServiceType(bluetoothGattService)) .append(" - ") .append(createServiceName(bluetoothGattService)) .append(" (") .append(bluetoothGattService.getUuid().toString()) .append(")\n") .append("Instance ID: ").append(bluetoothGattService.getInstanceId()) .append('\n'); }
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Bundle bundle = new Bundle(); bundle.putString(PARCEL_CHARACTERISTIC_UUID, characteristic.getUuid().toString()); bundle.putString(PARCEL_SERVICE_UUID, characteristic.getService().getUuid().toString()); bundle.putByteArray(PARCEL_VALUE, characteristic.getValue()); Message msg = Message.obtain(activity_handler, NOTIFICATION_RECEIVED); msg.setData(bundle); msg.sendToTarget(); }
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 && (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false; if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid()); if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false; if (service == null) return false; BluetoothDevice device = service.getDevice(); if (device == null) return false; service.getType(), service.getInstanceId(), new ParcelUuid(service.getUuid()), characteristic.getInstanceId(), new ParcelUuid(characteristic.getUuid()), characteristic.getWriteType(), AUTHENTICATION_NONE,
/** * Checks available services, characteristics and descriptors */ private void checkAvailableServices() { for (BluetoothGattService service : mBluetoothGatt.getServices()) { Log.d(TAG, "onServicesDiscovered:" + service.getUuid()); for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) { Log.d(TAG, " char:" + characteristic.getUuid()); for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) { Log.d(TAG, " descriptor:" + descriptor.getUuid()); } } } }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(DFU_CONTROL_POINT_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mControlPointCharacteristic = characteristic; mPacketCharacteristic = dfuService.getCharacteristic(DFU_PACKET_UUID); return mPacketCharacteristic != null; }
private void addImmediateAlertService() { /* * This method must be called in UI thread. It works fine on Nexus devices but if called * from other thread (e.g. from onServiceAdded in gatt server callback) it hangs the app. */ final BluetoothGattCharacteristic alertLevel = new BluetoothGattCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID, BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE, BluetoothGattCharacteristic.PERMISSION_WRITE); alertLevel.setValue(NO_ALERT); final BluetoothGattService immediateAlertService = new BluetoothGattService(IMMEDIATE_ALERT_SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY); immediateAlertService.addCharacteristic(alertLevel); mBluetoothGattServer.addService(immediateAlertService); }
private int readServiceCharacteristics(BluetoothGattService service, int offset) { for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) { if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0 && !isBlacklisted(service, characteristic)) { if (offset == 0) { readBytes(characteristic.getUuid()); return -1; } offset -= 1; } } for (BluetoothGattService included : service.getIncludedServices()) { offset = readServiceCharacteristics(included, offset); if (offset == -1) { return offset; } } return offset; }
@Override public boolean test(BluetoothGattService bluetoothGattService) throws Exception { return bluetoothGattService.getUuid().equals(serviceUuid); } })
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (BluetoothGatt.GATT_SUCCESS == status) { notifyWithResult(characteristic); } else { UUID serviceId = characteristic.getService().getUuid(); UUID characteristicId = characteristic.getUuid(); notifyWithFail(serviceId, characteristicId, "onCharacteristicRead fail"); } }
public void readCharacteristic(String characteristicName) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } BluetoothGattService s = mBluetoothGatt.getService(UUID.fromString(kYourServiceUUIDString)); BluetoothGattCharacteristic c = s.getCharacteristic(UUID.fromString(characteristicName)); //put the characteristic into the read queue readCharacteristicQueue.add(c); //if there is only 1 item in the queue, then read it. If more than 1, we handle asynchronously in the callback above //GIVE PRECEDENCE to descriptor writes. They must all finish first. if((readCharacteristicQueue.size() == 1) && (descriptorWriteQueue.size() == 0)) mBluetoothGatt.readCharacteristic(c); }
/** * if present we build the debug service for be able to send/receive the debug information * @param debugService debug service * @return object that we can use for communicate with the debug console, * can be null if we didn't find all the needed characteristics */ private Debug buildDebugService(BluetoothGattService debugService){ List<BluetoothGattCharacteristic> charList = debugService.getCharacteristics(); BluetoothGattCharacteristic term=null,err=null; //search the term and err characteristics for(BluetoothGattCharacteristic temp : charList){ if(temp.getUuid().equals(BLENodeDefines.Services.Debug.DEBUG_TERM_UUID)) term=temp; else if(temp.getUuid().equals(BLENodeDefines.Services.Debug.DEBUG_STDERR_UUID)) err=temp; }//for //if both are present we build the object if(term!=null && err!=null) return new Debug(Node.this,term,err); else return null; }//buildDebugService
private boolean isBlacklisted(BluetoothGattService service, BluetoothGattCharacteristic characteristic) { // Reading this triggers a pairing request on Beurer BF710 if (service.getUuid().equals(BluetoothGattUuid.fromShortCode(0xffe0)) && characteristic.getUuid().equals(BluetoothGattUuid.fromShortCode(0xffe5))) { return true; } return false; }
/** * Setup Battery Service * * @return the service */ private static BluetoothGattService setUpBatteryService() { final BluetoothGattService service = new BluetoothGattService(SERVICE_BATTERY, BluetoothGattService.SERVICE_TYPE_PRIMARY); // Battery Level final BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic( CHARACTERISTIC_BATTERY_LEVEL, BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED); final BluetoothGattDescriptor clientCharacteristicConfigurationDescriptor = new BluetoothGattDescriptor( DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE); clientCharacteristicConfigurationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); characteristic.addDescriptor(clientCharacteristicConfigurationDescriptor); while (!service.addCharacteristic(characteristic)); return service; }
public void addService(BluetoothGattService svc) { services.put(new Handle(svc.getUuid(), svc.getInstanceId()), svc); List<BluetoothGattCharacteristic> characteristics = svc.getCharacteristics(); service_characteristics.put(new Handle(svc.getUuid(), svc.getInstanceId()), characteristics); }
public void setLinkLossNotificationLevel(String address, int alertType) { Log.d(TAG, "setLinkLossNotificationLevel() - the device " + address); if (bluetoothGatt.get(address) == null || linkLossService == null || linkLossService.getCharacteristics() == null || linkLossService.getCharacteristics().size() == 0) { somethingGoesWrong(); return; } final BluetoothGattCharacteristic characteristic = linkLossService.getCharacteristics().get(0); characteristic.setValue(alertType, BluetoothGattCharacteristic.FORMAT_UINT8, 0); bluetoothGatt.get(address).writeCharacteristic(characteristic); }
private int getCharacteristicProperties(BluetoothGattService service, String characteristicUUIDString) { final UUID characteristicUuid = UUID.fromString(characteristicUUIDString); BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid); int properties = 0; if (characteristic != null) { properties = characteristic.getProperties(); } return properties; }
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; private BluetoothGattCharacteristic characteristic; private OnBoardingService service; private BluetoothGattReceiver receiver = new BluetoothGattReceiver(); @Before public void initialise() { BleDevice bleDevice = mock(BleDevice.class); when(bleDevice.getType()).thenReturn(BleDeviceType.WunderbarMIC); BluetoothGatt gatt = mock(BluetoothGatt.class); BluetoothGattService gattService = mock(BluetoothGattService.class); when(gatt.getServices()).thenReturn(Arrays.asList(gattService)); when(gattService.getUuid()).thenReturn(fromString("00002001-0000-1000-8000-00805f9b34fb")); characteristic = mock(BluetoothGattCharacteristic.class); when(gattService.getCharacteristics()).thenReturn(Arrays.asList(characteristic)); service = new OnBoardingService(bleDevice, gatt, receiver); }