Refine search
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { RxBleLog.d("onCharacteristicRead characteristic=%s status=%d", characteristic.getUuid(), status); nativeCallbackDispatcher.notifyNativeReadCallback(gatt, characteristic, status); super.onCharacteristicRead(gatt, characteristic, status); if (readCharacteristicOutput.hasObservers() && !propagateErrorIfOccurred( readCharacteristicOutput, gatt, characteristic, status, BleGattOperationType.CHARACTERISTIC_READ )) { readCharacteristicOutput.valueRelay.accept(new ByteAssociation<>(characteristic.getUuid(), characteristic.getValue())); } }
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; }
@Override public BluetoothGattDescriptor apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) { final BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(descriptorUuid); if (descriptor == null) { throw new BleDescriptorNotFoundException(descriptorUuid); } return descriptor; } });
@Override public BluetoothGattDescriptor apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) { BluetoothGattDescriptor bluetoothGattDescriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID); if (bluetoothGattDescriptor == null) { //adding notification descriptor if not present bluetoothGattDescriptor = new BluetoothGattDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID, 0); bluetoothGattCharacteristic.addDescriptor(bluetoothGattDescriptor); } return bluetoothGattDescriptor; } });
/** * Adds a {@link BluetoothGattCharacteristic} with specified parameters. * * @param uuid characteristic UUID * @param data locally stored value of the characteristic * @param properties OR-ed {@link BluetoothGattCharacteristic} property constants * @param descriptors list of characteristic descriptors. Use {@link DescriptorsBuilder} to create them. */ public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid, @NonNull byte[] data, int properties, List<BluetoothGattDescriptor> descriptors) { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(uuid, properties, 0); for (BluetoothGattDescriptor descriptor : descriptors) { characteristic.addDescriptor(descriptor); } characteristic.setValue(data); this.bluetoothGattCharacteristics.add(characteristic); return this; }
@SuppressWarnings("WrongConstant") public String createMismatchMessage(BluetoothGattCharacteristic characteristic, @BluetoothGattCharacteristicProperty int neededProperties) { return String.format( Locale.getDefault(), "Characteristic %s supports properties: %s (%d) does not have any property matching %s (%d)", characteristic.getUuid(), propertiesParser.propertiesIntToString(characteristic.getProperties()), characteristic.getProperties(), propertiesParser.propertiesIntToString(neededProperties), neededProperties ); } }
@Override public synchronized void onDataAvailable(BluetoothGattCharacteristic characteristic) { super.onDataAvailable(characteristic); // UART RX if (characteristic.getService().getUuid().toString().equalsIgnoreCase(UUID_SERVICE)) { if (characteristic.getUuid().toString().equalsIgnoreCase(UUID_RX)) { final byte[] bytes = characteristic.getValue(); final UartDataChunk dataChunk = new UartDataChunk(System.currentTimeMillis(), UartDataChunk.TRANSFERMODE_RX, bytes); mDataBuffer.add(dataChunk); } } }
@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 nextChunkReady(byte[] chunk) { if (chunk == null) { finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } NeatleLogger.d("Writing " + chunk.length + " bytes onto " + writeCharacteristic.getUuid()); writeCharacteristic.setValue(chunk); if (!gatt.writeCharacteristic(writeCharacteristic)) { NeatleLogger.d("Write returned false"); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); } }
@Override public boolean test(ByteAssociation<UUID> uuidByteAssociation) { return uuidByteAssociation.first.equals(bluetoothGattCharacteristic.getUuid()); } };
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
@Override public byte[] apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) throws Exception { return bluetoothGattCharacteristic.getValue(); } });
private void addLinkLossService() { /* * 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 linkLossAlertLevel = new BluetoothGattCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID, BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ); linkLossAlertLevel.setValue(HIGH_ALERT); final BluetoothGattService linkLossService = new BluetoothGattService(LINKLOSS_SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY); linkLossService.addCharacteristic(linkLossAlertLevel); mBluetoothGattServer.addService(linkLossService); }
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(); }
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { RxBleLog.d("onCharacteristicChanged characteristic=%s", characteristic.getUuid()); nativeCallbackDispatcher.notifyNativeChangedCallback(gatt, characteristic); super.onCharacteristicChanged(gatt, characteristic); /* * It is important to call changedCharacteristicSerializedPublishRelay as soon as possible because a quick changing * characteristic could lead to out-of-order execution since onCharacteristicChanged may be called on arbitrary * threads. */ if (changedCharacteristicSerializedPublishRelay.hasObservers()) { changedCharacteristicSerializedPublishRelay.accept( new CharacteristicChangedEvent( characteristic.getUuid(), characteristic.getInstanceId(), characteristic.getValue() ) ); } }
private boolean isCharacteristicWriteable(BluetoothGattCharacteristic characteristic) { return (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) != 0; } }
@Override public byte[] apply(BluetoothGattCharacteristic characteristic) { characteristic.setValue(data); return data; } });
@Override protected Deque<BleManager.Request> initGatt(final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(UART_SERVICE_UUID); mTXCharacteristic = service.getCharacteristic(UART_TX_CHARACTERISTIC_UUID); mRXCharacteristic = service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID); final int rxProperties = mRXCharacteristic.getProperties(); boolean writeRequest = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0; // Set the WRITE REQUEST type when the characteristic supports it. This will allow to send long write (also if the characteristic support it). // In case there is no WRITE REQUEST property, this manager will divide texts longer then 20 bytes into up to 20 bytes chunks. if (writeRequest) mRXCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); // We don't want to enable notifications on TX characteristic as we are not showing them here. A watch may be just used to send data. At least now. // final LinkedList<BleProfileApi.Request> requests = new LinkedList<>(); // requests.add(BleProfileApi.Request.newEnableNotificationsRequest(mTXCharacteristic)); // return requests; return null; }
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); }
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_HEIGHT_INPUT_CHAR); if (tChar == null) throw new AssertionError("characteristic null when sync time!"); // use one of them in regards of the Characteristic's property tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); //tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); tChar.setValue(/*another int*/, BluetoothGattCharacteristic.FORMAT_SINT32, 0); gatt.writeCharacteristic(tChar);