public void readData(UUID service, UUID Characteristics) { if (!isConnectedToGatt || myGatBand == null) { Log.d(TAG, "Cant read from BLE, not initialized."); return; } Log.d(TAG, "* Getting gatt service, UUID:" + service.toString()); BluetoothGattService myGatService = myGatBand.getService(service /*Consts.UUID_SERVICE_GENERIC*/); if (myGatService != null) { Log.d(TAG, "* Getting gatt Characteristic. UUID: " + Characteristics.toString()); BluetoothGattCharacteristic myGatChar = myGatService.getCharacteristic(Characteristics /*Consts.UUID_CHARACTERISTIC_DEVICE_NAME*/); if (myGatChar != null) { Log.d(TAG, "* Reading data"); boolean status = myGatBand.readCharacteristic(myGatChar); Log.d(TAG, "* Read status :" + status); } } }
private void setCharacteristicNotification( @NonNull BluetoothGatt bluetoothgatt, @NonNull BluetoothGattCharacteristic bluetoothgattcharacteristic, boolean flag ) { bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, flag); if (FIND_ME_CHARACTERISTIC.equals(bluetoothgattcharacteristic.getUuid())) { BluetoothGattDescriptor descriptor = bluetoothgattcharacteristic.getDescriptor( CLIENT_CHARACTERISTIC_CONFIG ); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); bluetoothgatt.writeDescriptor(descriptor); } } }
/** * 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; }
public void writeData(UUID service, UUID Characteristics,byte[] data) { if (!isConnectedToGatt || myGatBand == null) { Log.d(TAG, "Cant read from BLE, not initialized."); return; } Log.d(TAG, "* Getting gatt service, UUID:" + service.toString()); BluetoothGattService myGatService = myGatBand.getService(service /*Consts.UUID_SERVICE_HEARTBEAT*/); if (myGatService != null) { Log.d(TAG, "* Getting gatt Characteristic. UUID: " + Characteristics.toString()); BluetoothGattCharacteristic myGatChar = myGatService.getCharacteristic(Characteristics /*Consts.UUID_START_HEARTRATE_CONTROL_POINT*/); if (myGatChar != null) { Log.d(TAG, "* Writing trigger"); myGatChar.setValue(data /*Consts.BYTE_NEW_HEART_RATE_SCAN*/); boolean status = myGatBand.writeCharacteristic(myGatChar); Log.d(TAG, "* Writting trigger status :" + status); } } }
@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; }
/** * 收到BLE终端写入数据回调 */ @Override public void onCharacteristicWrite(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status) { Log.e(TAG,"onCharWrite "+gatt.getDevice().getName() +" write " +characteristic.getUuid().toString() +" -> " +new String(characteristic.getValue())); runOnUiThread(new Runnable() { @Override public void run() { int t = test[0]; Integer.toString(t); ResponseContent.setText(Integer.toString(t)); } }); }
@Override public void onCharacteristicReadRequest(final BluetoothDevice device, final int requestId, final int offset, final BluetoothGattCharacteristic characteristic) { Logger.d(mLogSession, "[Server callback] Read request for characteristic " + characteristic.getUuid() + " (requestId=" + requestId + ", offset=" + offset + ")"); Logger.i(mLogSession, "[Server] READ request for characteristic " + characteristic.getUuid() + " received"); byte[] value = characteristic.getValue(); if (value != null && offset > 0) { byte[] offsetValue = new byte[value.length - offset]; System.arraycopy(value, offset, offsetValue, 0, offsetValue.length); value = offsetValue; } if (value != null) Logger.d(mLogSession, "server.sendResponse(GATT_SUCCESS, value=" + ParserUtils.parse(value) + ")"); else Logger.d(mLogSession, "server.sendResponse(GATT_SUCCESS, value=null)"); mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value); Logger.v(mLogSession, "[Server] Response sent"); }
/** * Writes given operation parameters to the characteristic * * @param characteristic * the characteristic to write. This must be the Record Access Control Point characteristic * @param opCode * the operation code * @param operator * the operator (see {@link #OPERATOR_NULL} and others * @param params * optional parameters (one for >=, <=, two for the range, none for other operators) */ private void setOpCode(final BluetoothGattCharacteristic characteristic, final int opCode, final int operator, final Integer... params) { final int size = 2 + ((params.length > 0) ? 1 : 0) + params.length * 2; // 1 byte for opCode, 1 for operator, 1 for filter type (if parameters exists) and 2 for each parameter characteristic.setValue(new byte[size]); // write the operation code int offset = 0; characteristic.setValue(opCode, BluetoothGattCharacteristic.FORMAT_UINT8, offset); offset += 1; // write the operator. This is always present but may be equal to OPERATOR_NULL characteristic.setValue(operator, BluetoothGattCharacteristic.FORMAT_UINT8, offset); offset += 1; // if parameters exists, append them. Parameters should be sorted from minimum to maximum. Currently only one or two params are allowed if (params.length > 0) { // our implementation use only sequence number as a filer type characteristic.setValue(FILTER_TYPE_SEQUENCE_NUMBER, BluetoothGattCharacteristic.FORMAT_UINT8, offset); offset += 1; for (final Integer i : params) { characteristic.setValue(i, BluetoothGattCharacteristic.FORMAT_UINT16, offset); offset += 2; } } }
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); // This is special handling for the Heart Rate Measurement profile. Data parsing is // carried out as per profile specifications: // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { int flag = characteristic.getProperties(); int format = -1; if ((flag & 0x01) != 0) { format = BluetoothGattCharacteristic.FORMAT_UINT16; Log.d(TAG, "Heart rate format UINT16."); } else { format = BluetoothGattCharacteristic.FORMAT_UINT8; Log.d(TAG, "Heart rate format UINT8."); } final int heartRate = characteristic.getIntValue(format, 1); Log.d(TAG, String.format("Received heart rate: %d", heartRate)); intent.putExtra(EXTRA_DATA, String.valueOf(heartRate)); } else { // For all other profiles, writes the data formatted in HEX. final byte[] data = characteristic.getValue(); if (data != null && data.length > 0) { final StringBuilder stringBuilder = new StringBuilder(data.length); for(byte byteChar : data) stringBuilder.append(String.format("%02X ", byteChar)); intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); } } sendBroadcast(intent); }
/** * 相当于一个监听器, 当蓝牙设备有数据返回时执行 * * @param gatt * @param characteristic */ @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { Log.e(TAG, "onCharacteristicChanged 返回数据: " + Arrays.toString(characteristic.getValue())); Message message = new Message(); message.what = Constant.RESULT_DATA; message.obj = characteristic.getValue(); handler.sendMessage(message); //回调 onCharacteristicRefresh(gatt, characteristic); }
private void writeCharacteristic(String serviceGuid, String characteristic, int value, int type) { if(!isConnected()) { logi("writeCharacteristic() :: Not connected. Returning"); return; } BluetoothGattService s = getService(UUID.fromString(serviceGuid)); if(s == null) { logi("writeCharacteristic() :: Service not found"); return; } BluetoothGattCharacteristic c = s.getCharacteristic(UUID.fromString(characteristic)); if(c == null) { logi("writeCharacteristic() :: characteristic not found"); return; } c.setValue(value, type, 0); int ret = writeCharacteristic(c); logi("writeCharacteristic() :: returns - " + ret); }
@Override public void onCharacteristicReadRequest( BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic chr) { int status = BluetoothGatt.GATT_FAILURE; byte[] bytes = null; if (offset != 0) { status = BluetoothGatt.GATT_INVALID_OFFSET; } else if (chr.equals(U2FGattService.U2F_CONTROL_POINT_LENGTH)) { status = BluetoothGatt.GATT_SUCCESS; bytes = new byte[] { 0x02, 0x00 }; /* Length == 512, see U2F BT 6.1 */ } else if (chr.equals(U2FGattService.U2F_SERVICE_REVISION_BITFIELD)) { status = BluetoothGatt.GATT_SUCCESS; bytes = new byte[] { 0x40 }; /* Version == 1.2, see U2F BT 6.1 */ } Log.d(getClass().getCanonicalName(), Integer.valueOf(bytes.length).toString()); mGattServer.sendResponse(device, requestId, status, 0, bytes); }
@Override public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value); mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value); if (mProvClientDev.getPeerDevice().equals(device)) { if (characteristic.getUuid().compareTo(UUID.fromString(UUID_PB_CHAR_DATA_IN)) == 0) { String strAddr=device.getAddress(); byte[] addr = stringToAddress(strAddr); provServerPduInNative(addr, value); } } }
/** * 处理向特征码写入数据的回调 * @param bleCallback */ private void handleCharacteristicWriteCallback(final BleCharactCallback bleCallback) { if (bleCallback != null) { // 添加连接回调到LiteBluetooth的回调集合中 listenAndTimer(bleCallback, MSG_WRIATE_CHA, new BluetoothGattCallback() { @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { handler.removeMessages(MSG_WRIATE_CHA, this); if (status == BluetoothGatt.GATT_SUCCESS) { bleCallback.onSuccess(characteristic); } else { bleCallback.onFailure(new GattException(status)); } } }); } }
private void handleCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { String UUID = characteristic.getUuid().toString(); Integer integerValue = characteristic.getIntValue(GattFormats.FORMAT_UINT32, 0); if(integerValue == null) { return; } int value = integerValue; int eventSrc = value & 0x0ffff; if(eventSrc < 1001) { return; } logi("Characteristic UUID = " + UUID); logi("Characteristic Value = " + value); logi("eventSrc = " + eventSrc); int event = (value >> 16) & 0x0ffff; logi("event = " + event); sendMessage(eventSrc, event); }
/** * 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; }
/** * 读取特征码刷新数据 * @param bleCallback */ private void handleCharacteristicNotificationCallback(final BleCharactCallback bleCallback) { if (bleCallback != null) { listenAndTimer(bleCallback, MSG_NOTIY_CHA, new BluetoothGattCallback() { AtomicBoolean msgRemoved = new AtomicBoolean(false); @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (!msgRemoved.getAndSet(true)) { handler.removeMessages(MSG_NOTIY_CHA, this); } bleCallback.onSuccess(characteristic); } }); } }
@Override public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); if (characteristic.getUuid().compareTo(UUID.fromString(UUID_PROXY_CHAR_DATA_IN)) == 0) { if (mProxyDev != null && mProxyDev.getConnectionState() == PeerDevice.STATE_CONNECTED) { mProxyDev.onDataOut(); } } else if (characteristic.getUuid().compareTo(UUID.fromString(UUID_PB_CHAR_DATA_IN)) == 0) { if (mProvServerDev != null && mProvServerDev.getConnectionState() == PeerDevice.STATE_CONNECTED) { String strAddr=gatt.getDevice().getAddress(); byte[] addr = stringToAddress(strAddr); provClientPduSentNative(addr); mProvServerDev.onDataOut(); } } }
/** * read data from specified characteristic */ public boolean readCharacteristic(BluetoothGattCharacteristic charact, BleCharactCallback bleCallback) { if ((characteristic.getProperties() | BluetoothGattCharacteristic.PROPERTY_READ) > 0) { // If there is an active notification on a characteristic, clear // it first so it doesn't update the data field on the user interface. setCharacteristicNotification(getBluetoothGatt(), charact, false); // 读取到的数据回调返回 handleCharacteristicReadCallback(bleCallback); return handleAfterInitialed(getBluetoothGatt().readCharacteristic(charact), bleCallback); } else { if (bleCallback != null) { // 特征码不可读 bleCallback.onFailure(new OtherException("Characteristic [is not] readable!")); } return false; } }
@Test public void testWriteFirstChunkFailed() throws IOException { InputSource inputSource = Mockito.mock(InputSource.class); doThrow(IOException.class).when(inputSource).open(); when(gatt.getService(eq(serviceUUID))).thenReturn(gattService); when(gattService.getCharacteristic(characteristicUUID)).thenReturn(gattCharacteristic); WriteCommand writeCommand = new WriteCommand( serviceUUID, characteristicUUID, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT, inputSource, commandObserver); writeCommand.execute(device, operationCommandObserver, gatt); CommandResult result = CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE); verify(commandObserver, times(1)).finished(eq(writeCommand), refEq(result, "timestamp")); verify(operationCommandObserver, times(1)).finished(eq(writeCommand), refEq(result, "timestamp")); Mockito.reset(commandObserver, operationCommandObserver, inputSource); when(inputSource.nextChunk()).thenThrow(IOException.class); writeCommand.execute(device, operationCommandObserver, gatt); verify(commandObserver, times(1)).finished(eq(writeCommand), refEq(result, "timestamp")); verify(operationCommandObserver, times(1)).finished(eq(writeCommand), refEq(result, "timestamp")); }
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); Logger.i("onCharacteristicRead data status:" + GattError.parseConnectionError(status) + " " + characteristic.getUuid().toString()); mOpratorQueue.nextOperator(); if (getBluetoothGattCallback() != null) getBluetoothGattCallback().onCharacteristicRead(gatt, characteristic, status); }
private synchronized void readCharacteristic(BluetoothGattCharacteristic characteristic) throws InterruptedException, GattException { if (VDBG) { Log.d(TAG, "readCharacteristic: [CMD]"); } if (!mBtGatt.readCharacteristic(characteristic)) { throw new GattException("Could not read characteristic."); }; while (lastMessage != MESSAGE_READ) { wait(); } if (VDBG) { Log.d(TAG, "readCharacteristic: [DONE] " + HexStrings.toHexString(characteristic.getValue())); } lastMessage = -1; }
@Override protected void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status != BluetoothGatt.GATT_SUCCESS) { NeatleLogger.i("Write on " + characteristic.getUuid() + " failed with status " + status); finish(CommandResult.createErrorResult(characteristicUUID, status)); return; } if (asyncMode) { synchronized (bufferReadLock) { bufferReadLock.notify(); } } else { byte[] chunk; try { chunk = buffer.nextChunk(); } catch (IOException ex) { NeatleLogger.e("Failed to get the first chunk", ex); finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE)); return; } if (chunk == null) { finish(CommandResult.createEmptySuccess(characteristicUUID)); return; } nextChunkReady(chunk); } }
public boolean setCharacteristicValue(UUID serviceUuid, UUID characteristicUuid, String value, boolean notify){ BluetoothGattService service = gattServer.getService(serviceUuid); if(service == null) return false; BluetoothGattCharacteristic ch = service.getCharacteristic(characteristicUuid); if(ch == null) return false; boolean rtn = ch.setValue(value); if(rtn && notify){ notifyDevices(ch); } return rtn; }
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if(status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } else { Log.w(TAG, "onCharacteristicWrite received: " + status); } writeNextCharacteristic(); }
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.d(TAG, "Write successful: " + Arrays.toString(characteristic.getValue())); raiseonWrite(gatt,characteristic,status); super.onCharacteristicWrite(gatt,characteristic,status); }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (bluetoothAdapter == null || bluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } bluetoothGatt.setCharacteristicNotification(characteristic, enabled); for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) { descriptor.setValue( enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] {0x00, 0x00}); bluetoothGatt.writeDescriptor(descriptor); } }
/** * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported * asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)} * callback. * * @param characteristic The characteristic to read from. */ public void readCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readCharacteristic(characteristic); }
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (mOnDataAvailableListener!=null) mOnDataAvailableListener.onCharacteristicWrite(gatt, characteristic, status); }
public boolean setCharacteristicValue(UUID serviceUuid, UUID characteristicUuid, byte[] value){ if(gattConnection == null) return false; BluetoothGattService service = gattConnection.getService(serviceUuid); if(service == null) return false; BluetoothGattCharacteristic ch = service.getCharacteristic(characteristicUuid); if(ch == null) return false; ch.setValue(value); return gattConnection.writeCharacteristic(ch); }
public void receiveNotifications(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, boolean receive){ BluetoothGattCharacteristic characteristic = getCharacteristic(serviceUuid, characteristicUuid); gattConnection.setCharacteristicNotification(characteristic, receive); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gattConnection.writeDescriptor(descriptor); }
@Override public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) { if (CHARACTERISTIC_COUNTER_UUID.equals(characteristic.getUuid())) { Log.i(TAG, "Read counter"); byte[] value = mListener.onCounterRead(); mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, value); } else { // Invalid characteristic Log.w(TAG, "Invalid Characteristic Read: " + characteristic.getUuid()); mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, 0, null); } }
/** * 刷新返回的数据 * * @return */ public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) { BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUIDCons.d); if (enable) { Log.i(TAG, "Enable Notification"); mGatt.setCharacteristicNotification(characteristic, true); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mGatt.writeDescriptor(descriptor); } else { Log.i(TAG, "Disable Notification"); mGatt.setCharacteristicNotification(characteristic, false); descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); mGatt.writeDescriptor(descriptor); } }
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)); } } }
@Override public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value); Log.i("BLEMainActivity:BluetoothGattServerCallback:onCharacteristicWriteRequest", "device : " + device.getAddress() + " characteristic : " + characteristic.getUuid() + "Value = " + value.toString()); for (ServerService service : serverServices) { if (service.knowsCharacteristic(characteristic)) { service.processWriteRequest(characteristic, offset, value); } } }
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.i(TAG, "onCharacteristicRead called..."); if (status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } }
@Override protected void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { // Intermediate Cuff Pressure characteristic read if (mLogSession != null) Logger.a(mLogSession, IntermediateCuffPressureParser.parse(characteristic)); parseBPMValue(characteristic); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); deviceCount = (TextView) findViewById(R.id.lbl_device_count); slider = (SeekBar) findViewById(R.id.seekBar); listView = (ListView) findViewById(R.id.lst_main_options); deviceList = new ArrayList<>(); deviceAddresses = new ArrayList<>(); deviceNames = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1); devices = (ListView) findViewById(R.id.lst_discovered_devices); devices.setAdapter(deviceNames); listView.setOnItemClickListener(this); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { bleServer = new BLEServer(MainActivity.this, this); sliderCharacteristic = bleServer.buildCharacteristic(sliderCharacteristicUuid, new BluetoothGattDescriptor[0], BLEServer.CharProperties.Read | BLEServer.CharProperties.Write | BLEServer.CharProperties.Notify, BLEServer.CharPermissions.Read | BLEServer.CharPermissions.Write); comService = bleServer.buildService(communicationServiceUuid, BLEServer.ServiceType.Primary, new BluetoothGattCharacteristic[]{sliderCharacteristic}); bleServer.addService(comService); } if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){ bleClient = new BLEClient(MainActivity.this, this); bleClient.setUseNewMethod(false); //This requires more work with permissions than the old method } }
@Override protected void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (status != BluetoothGatt.GATT_SUCCESS) { finish(CommandResult.createErrorResult(null, status)); } else { if (observer != null) { observer.characteristicRead(CommandResult.createCharacteristicRead(characteristic, status)); } readNext(gatt); } }
public void readCharacteristic(BluetoothGattCharacteristic characteristic) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.readCharacteristic(characteristic); }