Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#setValue()
The following examples show how to use
android.bluetooth.BluetoothGattCharacteristic#setValue() .
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: BleSensor.java From BLE-Heart-rate-variability-demo with MIT License | 6 votes |
public BluetoothGattExecutor.ServiceAction write(final String uuid, final byte[] value) { return new BluetoothGattExecutor.ServiceAction() { @Override public boolean execute(BluetoothGatt bluetoothGatt) { final BluetoothGattCharacteristic characteristic = getCharacteristic(bluetoothGatt, uuid); if (characteristic != null) { characteristic.setValue(value); bluetoothGatt.writeCharacteristic(characteristic); return false; } Log.i(TAG, "Characteristc not found with uuid: " + uuid); return true; } }; }
Example 2
Source File: ACSUtilityService.java From ESeal with Apache License 2.0 | 6 votes |
public void enablePhoneMode() { Log.i(TAG, "Enabling Phone Mode..."); /*if (mBluetoothGatt == null) { Log.i(TAG, "mBtGatt == null"); } BluetoothGattService disService = mBluetoothGatt.getService(ACS_SERVICE_UUID); if (disService == null) { // showMessage("Dis service not found!"); Log.i(TAG, "disService == null"); return; } BluetoothGattCharacteristic characCMD = disService .getCharacteristic(CMD_LINE_UUID);*/ BluetoothGattCharacteristic characCMD = getACSCharacteristic(CMD_LINE_UUID); if (characCMD == null) { Log.i(TAG, "characCMD == null"); return; } byte[] value = {0x06, 0x01}; characCMD.setValue(value); mBluetoothGatt.writeCharacteristic(characCMD); }
Example 3
Source File: G5CollectionService.java From xDrip with GNU General Public License v3.0 | 5 votes |
private synchronized void doVersionRequestMessage(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.d(TAG, "doVersionRequestMessage() start"); final VersionRequestTxMessage versionTx = new VersionRequestTxMessage(); characteristic.setValue(versionTx.byteSequence); gatt.writeCharacteristic(characteristic); Log.d(TAG, "doVersionRequestMessage() finished"); }
Example 4
Source File: NukiPairingCallback.java From trigger with GNU General Public License v2.0 | 5 votes |
public void onConnected(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (!this.setup.shared_key.isEmpty()) { this.listener.onTaskResult(setup_id, ReplyCode.LOCAL_ERROR, "Already paired to some device!"); closeConnection(gatt); return; } NukiCommand.NukiRequest nr = new NukiCommand.NukiRequest(0x03); characteristic.setValue(NukiRequestHandler.crc_calc_and_add(nr.generate())); boolean ok = gatt.writeCharacteristic(characteristic); if (!ok) { Log.e(TAG, "writeCharacteristic failed for NukiRequest"); closeConnection(gatt); } }
Example 5
Source File: P_Task_SendNotification.java From SweetBlue with GNU General Public License v3.0 | 5 votes |
@Override void execute() { final BluetoothGattCharacteristic characteristic = getServer().getNativeCharacteristic(m_serviceUuid, m_charUuid); if( characteristic == null ) { fail(BleServer.OutgoingListener.Status.NO_MATCHING_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE); } else { if( !characteristic.setValue(data_sent()) ) { fail(BleServer.OutgoingListener.Status.FAILED_TO_SET_VALUE_ON_TARGET, BleStatuses.GATT_STATUS_NOT_APPLICABLE); } else { if( !getServer().getNativeLayer().notifyCharacteristicChanged(m_nativeDevice, characteristic, m_confirm) ) { fail(BleServer.OutgoingListener.Status.FAILED_TO_SEND_OUT, BleStatuses.GATT_STATUS_NOT_APPLICABLE); } else { // SUCCESS, at least so far...we will see } } } }
Example 6
Source File: ESenseManager.java From flutter-plugins with MIT License | 5 votes |
/** * Requests a change of the sensor configuration on the connected device. * @param config new configuration to be written on the device * @return <code>true</code> if the request was successfully made, * <code>false</code> otherwise */ public boolean setSensorConfig(ESenseConfig config) { if(config != null) { BluetoothGattCharacteristic c = mCharacteristicMap.get(SENSOR_CONFIG_CHARACTERISTIC); byte[] bytes = config.prepareCharacteristicData(); bytes[1] = getCheckSum(bytes,1); c.setValue(bytes); return mGatt.writeCharacteristic(c); } else { Log.e(TAG, "In setSensorConfig(), config is set to null!!"); return false; } }
Example 7
Source File: ESenseManager.java From flutter-plugins with MIT License | 5 votes |
/** * Unregisters a sensor listener and stops sensor sampling on the connected device */ public void unregisterSensorListener(){ BluetoothGattCharacteristic c = mCharacteristicMap.get(CONFIG_CHARACTERISTIC); byte[] IMU_STOP_CMD = new byte[]{0x53, 0x02, 0x02, 0x00, 0x00}; c.setValue(IMU_STOP_CMD); mGatt.writeCharacteristic(c); enableNotification(SENSOR_CHARACTERISTIC,false); mSensorListener = null; }
Example 8
Source File: G5CollectionService.java From xDrip-plus with GNU General Public License v3.0 | 5 votes |
private synchronized void sendAuthRequestTxMessage(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.e(TAG, "Sending new AuthRequestTxMessage to " + getUUIDName(characteristic.getUuid()) + " ..."); authRequest = new AuthRequestTxMessage(getTokenSize()); Log.i(TAG, "AuthRequestTX: " + JoH.bytesToHex(authRequest.byteSequence)); characteristic.setValue(authRequest.byteSequence); if (gatt != null) { gatt.writeCharacteristic(characteristic); } else { Log.e(TAG, "Cannot send AuthRequestTx as supplied gatt is null!"); } }
Example 9
Source File: WriteGattServerCharacteristicValueTransaction.java From bitgatt with Mozilla Public License 2.0 | 5 votes |
@Override protected void transaction(GattTransactionCallback callback) { super.transaction(callback); getGattServer().setState(GattState.WRITING_CHARACTERISTIC); BluetoothGattCharacteristic localCharacteristic = service.getCharacteristic(this.characteristic.getUuid()); if(localCharacteristic == null) { respondWithError(null, callback); return; } TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName()); try { if (localCharacteristic.setValue(data)) { // success builder.responseStatus(GattStatus.GATT_SUCCESS.ordinal()); getGattServer().setState(GattState.WRITE_CHARACTERISTIC_SUCCESS); builder.data(localCharacteristic.getValue()) .gattState(getGattServer().getGattState()) .resultStatus(TransactionResult.TransactionResultStatus.SUCCESS) .serviceUuid(service.getUuid()) .characteristicUuid(localCharacteristic.getUuid()); callCallbackWithTransactionResultAndRelease(callback, builder.build()); getGattServer().setState(GattState.IDLE); } else { // failure respondWithError(localCharacteristic, callback); } } catch (NullPointerException ex) { Timber.w(ex,"[%s] We are going to fail this tx due to the stack NPE, this is probably poor peripheral behavior, this should become a FW bug.", getDevice()); respondWithError(localCharacteristic, callback); Strategy strategy = strategyProvider. getStrategyForPhoneAndGattConnection(null, getConnection(), Situation.TRACKER_WENT_AWAY_DURING_GATT_OPERATION); if (strategy != null) { strategy.applyStrategy(); } } }
Example 10
Source File: G5CollectionService.java From xDrip with GNU General Public License v3.0 | 5 votes |
private synchronized void sendAuthRequestTxMessage(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.e(TAG, "Sending new AuthRequestTxMessage to " + getUUIDName(characteristic.getUuid()) + " ..."); authRequest = new AuthRequestTxMessage(getTokenSize()); Log.i(TAG, "AuthRequestTX: " + JoH.bytesToHex(authRequest.byteSequence)); characteristic.setValue(authRequest.byteSequence); if (gatt != null) { gatt.writeCharacteristic(characteristic); } else { Log.e(TAG, "Cannot send AuthRequestTx as supplied gatt is null!"); } }
Example 11
Source File: P_AndroidGatt.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
@Override public final boolean setCharValue(BluetoothGattCharacteristic characteristic, byte[] data) { if (characteristic != null) { return characteristic.setValue(data); } return false; }
Example 12
Source File: G5CollectionService.java From xDrip-plus with GNU General Public License v3.0 | 5 votes |
private synchronized void doDisconnectMessage(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.d(TAG, "doDisconnectMessage() start"); gatt.setCharacteristicNotification(controlCharacteristic, false); final DisconnectTxMessage disconnectTx = new DisconnectTxMessage(); characteristic.setValue(disconnectTx.byteSequence); gatt.writeCharacteristic(characteristic); gatt.disconnect(); Log.d(TAG, "doDisconnectMessage() finished"); }
Example 13
Source File: BluetoothUtilMockImpl.java From android-ponewheel with MIT License | 4 votes |
void setByteCharacteristic(String mockOnewheelCharacteristic, byte[] v) { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString(mockOnewheelCharacteristic), 0, 0); characteristic.setValue(v); owDevice.processUUID(characteristic); }
Example 14
Source File: BleManager.java From BLEChat with GNU General Public License v3.0 | 4 votes |
public boolean write(BluetoothGattCharacteristic chr, byte[] data) { if (mBluetoothGatt == null) { Logs.d(TAG, "# BluetoothGatt not initialized"); return false; } BluetoothGattCharacteristic writableChar = null; if(chr == null) { if(mDefaultChar == null) { for(BluetoothGattCharacteristic bgc : mWritableCharacteristics) { if(isWritableCharacteristic(bgc)) { writableChar = bgc; } } if(writableChar == null) { Logs.d(TAG, "# Write failed - No available characteristic"); return false; } } else { if(isWritableCharacteristic(mDefaultChar)) { Logs.d("# Default GattCharacteristic is PROPERY_WRITE | PROPERTY_WRITE_NO_RESPONSE"); writableChar = mDefaultChar; } else { Logs.d("# Default GattCharacteristic is not writable"); mDefaultChar = null; return false; } } } else { if (isWritableCharacteristic(chr)) { Logs.d("# user GattCharacteristic is PROPERY_WRITE | PROPERTY_WRITE_NO_RESPONSE"); writableChar = chr; } else { Logs.d("# user GattCharacteristic is not writable"); return false; } } writableChar.setValue(data); writableChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); mBluetoothGatt.writeCharacteristic(writableChar); mDefaultChar = writableChar; return true; }
Example 15
Source File: BleManagerHandler.java From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License | 4 votes |
private boolean assignAndNotify(@NonNull final BluetoothDevice device, @NonNull final BluetoothGattCharacteristic characteristic, @NonNull final byte[] value) { final boolean isShared = characteristicValues == null || !characteristicValues.containsKey(characteristic); if (isShared) { characteristic.setValue(value); } else { characteristicValues.put(characteristic, value); } // Notify listener ValueChangedCallback callback; if ((callback = valueChangedCallbacks.get(characteristic)) != null) { callback.notifyValueChanged(device, value); } // Check if a request awaits, if (awaitingRequest instanceof WaitForValueChangedRequest // is registered for this characteristic && awaitingRequest.characteristic == characteristic // and didn't have a trigger, or the trigger was started // (not necessarily completed) && !awaitingRequest.isTriggerPending()) { final WaitForValueChangedRequest waitForWrite = (WaitForValueChangedRequest) awaitingRequest; if (waitForWrite.matches(value)) { // notify that new data was received. waitForWrite.notifyValueChanged(device, value); // If no more data are expected if (!waitForWrite.hasMore()) { // notify success, waitForWrite.notifySuccess(device); // and proceed to the next request only if the trigger has completed. // Otherwise, the next request will be started when the request's callback // will be received. awaitingRequest = null; return waitForWrite.isTriggerCompleteOrNull(); } } } return false; }
Example 16
Source File: GattPlugin.java From bitgatt with Mozilla Public License 2.0 | 4 votes |
private void writeLocalGattServerCharacteristic(DumperContext dumpContext, Iterator<String> args) throws InterruptedException { int index = 0; String serviceString = null; String characteristicString = null; String data = null; while (args.hasNext()) { if (index == 0) { serviceString = args.next(); } else if (index == 1) { characteristicString = args.next(); } else if (index == 2) { data = args.next(); } index++; } if (serviceString == null) { logError(dumpContext, new IllegalArgumentException("No service uuid provided")); return; } else if (characteristicString == null) { logError(dumpContext, new IllegalArgumentException("No characteristic uuid provided")); return; } else if (data == null) { logError(dumpContext, new IllegalArgumentException("No data provided")); return; } GattServerConnection conn = fitbitGatt.getServer(); if (conn == null) { logError(dumpContext, new IllegalArgumentException("No valid gatt server available")); return; } BluetoothGattService localService = conn.getServer().getService(UUID.fromString(serviceString)); if (localService == null) { logError(dumpContext, new IllegalStateException("No service for the uuid " + serviceString + " found")); return; } BluetoothGattCharacteristic localCharacteristic = localService.getCharacteristic(UUID.fromString(characteristicString)); if (localCharacteristic == null) { logError(dumpContext, new IllegalStateException("No characteristic for the uuid " + characteristicString + " found")); return; } localCharacteristic.setValue(data.getBytes()); WriteGattServerCharacteristicValueTransaction tx = new WriteGattServerCharacteristicValueTransaction(conn, GattState.WRITE_CHARACTERISTIC_SUCCESS, localService, localCharacteristic, data.getBytes()); CountDownLatch cdl = new CountDownLatch(1); conn.runTx(tx, result -> { logSuccessOrFailure(result, dumpContext, "Successfully wrote to " + localCharacteristic.getUuid().toString() + " on " + localService.getUuid().toString(), "Failed writing to " + localCharacteristic.getUuid().toString() + " on " + localService.getUuid().toString()); cdl.countDown(); }); cdl.await(); }
Example 17
Source File: G5CollectionService.java From xDrip with GNU General Public License v3.0 | 4 votes |
private synchronized void doBatteryInfoRequestMessage(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.d(TAG, "doBatteryInfoMessage() start"); characteristic.setValue(new BatteryInfoTxMessage().byteSequence); gatt.writeCharacteristic(characteristic); Log.d(TAG, "doBatteryInfoMessage() finished"); }
Example 18
Source File: BleGattImpl.java From EasyBle with Apache License 2.0 | 4 votes |
@Override public void write(final BleDevice device, String serviceUuid, String writeUuid, byte[] data, final BleWriteCallback callback) { checkNotNull(callback, BleWriteCallback.class); if (data == null || data.length < 1 || data.length > 509) { callback.onFailure(BleCallback.FAIL_OTHER, data == null ? "data is null" : "data length must range from 1 to 509", device); return; } if (data.length > 20) { Logger.w("data length is greater than the default(20 bytes), make sure MTU >= " + (data.length + 3)); } if (!checkConnection(device, callback)) { return; } BluetoothGatt gatt = mGattMap.get(device.address); if (!checkUuid(serviceUuid, writeUuid, gatt, device, callback)) { return; } OperationIdentify identify = getOperationIdentifyFromMap(mWriteCallbackMap, device.address, serviceUuid, writeUuid); if (identify != null) { mWriteCallbackMap.put(identify, callback); } else { mWriteCallbackMap.put(new OperationIdentify(device.address, serviceUuid, writeUuid), callback); } BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid)); BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(writeUuid)); boolean writable = (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0; if (!writable) { mHandler.post(new Runnable() { @Override public void run() { callback.onFailure(BleCallback.FAIL_OTHER, "the characteristic is not writable", device); } }); return; } if (!characteristic.setValue(data) || !gatt.writeCharacteristic(characteristic)) { mHandler.post(new Runnable() { @Override public void run() { callback.onFailure(BleCallback.FAIL_OTHER, "write fail because of unknown reason", device); } }); } }
Example 19
Source File: BluetoothLeService.java From WheelLogAndroid with GNU General Public License v3.0 | 4 votes |
public boolean writeBluetoothGattCharacteristic(byte[] cmd) { if (this.mBluetoothGatt == null) { return false; } StringBuilder stringBuilder = new StringBuilder(cmd.length); for (byte aData : cmd) stringBuilder.append(String.format(Locale.US, "%02X", aData)); Timber.i("Transmitted: " + stringBuilder.toString()); switch (WheelData.getInstance().getWheelType()) { case KINGSONG: BluetoothGattService ks_service = this.mBluetoothGatt.getService(UUID.fromString(Constants.KINGSONG_SERVICE_UUID)); if (ks_service == null) { Timber.i("writeBluetoothGattCharacteristic service == null"); return false; } BluetoothGattCharacteristic ks_characteristic = ks_service.getCharacteristic(UUID.fromString(Constants.KINGSONG_READ_CHARACTER_UUID)); if (ks_characteristic == null) { Timber.i("writeBluetoothGattCharacteristic characteristic == null"); return false; } ks_characteristic.setValue(cmd); Timber.i("writeBluetoothGattCharacteristic writeType = %d", ks_characteristic.getWriteType()); ks_characteristic.setWriteType(1); return this.mBluetoothGatt.writeCharacteristic(ks_characteristic); case GOTWAY: BluetoothGattService gw_service = this.mBluetoothGatt.getService(UUID.fromString(Constants.GOTWAY_SERVICE_UUID)); if (gw_service == null) { Timber.i("writeBluetoothGattCharacteristic service == null"); return false; } BluetoothGattCharacteristic characteristic = gw_service.getCharacteristic(UUID.fromString(Constants.GOTWAY_READ_CHARACTER_UUID)); if (characteristic == null) { Timber.i("writeBluetoothGattCharacteristic characteristic == null"); return false; } characteristic.setValue(cmd); Timber.i("writeBluetoothGattCharacteristic writeType = %d", characteristic.getWriteType()); return this.mBluetoothGatt.writeCharacteristic(characteristic); case NINEBOT_Z: BluetoothGattService nz_service = this.mBluetoothGatt.getService(UUID.fromString(Constants.NINEBOT_Z_SERVICE_UUID)); if (nz_service == null) { Timber.i("writeBluetoothGattCharacteristic service == null"); return false; } BluetoothGattCharacteristic nz_characteristic = nz_service.getCharacteristic(UUID.fromString(Constants.NINEBOT_Z_WRITE_CHARACTER_UUID)); if (nz_characteristic == null) { Timber.i("writeBluetoothGattCharacteristic characteristic == null"); return false; } nz_characteristic.setValue(cmd); Timber.i("writeBluetoothGattCharacteristic writeType = %d", nz_characteristic.getWriteType()); return this.mBluetoothGatt.writeCharacteristic(nz_characteristic); case INMOTION: BluetoothGattService im_service = this.mBluetoothGatt.getService(UUID.fromString(Constants.INMOTION_WRITE_SERVICE_UUID)); if (im_service == null) { Timber.i("writeBluetoothGattCharacteristic service == null"); return false; } BluetoothGattCharacteristic im_characteristic = im_service.getCharacteristic(UUID.fromString(Constants.INMOTION_WRITE_CHARACTER_UUID)); if (im_characteristic == null) { Timber.i("writeBluetoothGattCharacteristic characteristic == null"); return false; } byte[] buf = new byte[20]; int i2 = cmd.length / 20; int i3 = cmd.length - (i2 * 20); for (int i4 = 0; i4 < i2; i4++) { System.arraycopy(cmd, i4 * 20, buf, 0, 20); im_characteristic.setValue(buf); if (!this.mBluetoothGatt.writeCharacteristic(im_characteristic)) return false; try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } if (i3 > 0) { System.arraycopy(cmd, i2 * 20, buf, 0, i3); im_characteristic.setValue(buf); if(!this.mBluetoothGatt.writeCharacteristic(im_characteristic)) return false; } Timber.i("writeBluetoothGattCharacteristic writeType = %d", im_characteristic.getWriteType()); return true; } return false; }