List of usage examples for android.bluetooth BluetoothGattDescriptor setValue
public boolean setValue(byte[] value)
From source file:com.github.akinaru.bleanalyzer.bluetooth.BluetoothCustomManager.java
@SuppressLint("NewApi") @Override//from w ww . j a v a 2 s. c o m public void writeDescriptor(String descriptorUid, BluetoothGatt gatt, byte[] value, String serviceUid, String characUid) { if (gatt != null && descriptorUid != null) { gattThreadPool.execute(new GattTask(gatt, descriptorUid, value, serviceUid, characUid) { @Override public void run() { BluetoothGattDescriptor descriptor = getGatt() .getService(UUID.fromString(getDescriptorServiceUid())) .getCharacteristic(UUID.fromString(getDescriptorCharacUid())) .getDescriptor(UUID.fromString(getUid())); descriptor.setValue(getValue()); getGatt().writeDescriptor(descriptor); eventManager.reset(); try { eventManager.waitOne(BT_TIMEOUT); } catch (Exception e) { e.printStackTrace(); } } }); gattThreadPool.execute(new Runnable() { @Override public void run() { } }); } else Log.e(TAG, "Error int writeCharacteristic() input argument NULL"); }
From source file:net.kenevans.android.blecardiacmonitor.BCMBleService.java
/** * Starts a session./*ww w . jav a 2 s.c om*/ * * @param charBat The BAT characteristic. * @param charHr The HR characteristic. * @return If successful. (Always returns true) */ public boolean startSession(BluetoothGattCharacteristic charBat, BluetoothGattCharacteristic charHr) { Log.d(TAG, "startSession"); // Log.d(TAG, "startSession: mSessionState=" + mSessionState // + " mTimeoutTimer=" + mTimeoutTimer); // // DEBUG // String batVal = mCharBat == null ? "null" : String.format("%8x", // mCharBat.hashCode()); // String hrVal = mCharHr == null ? "null" : String.format("%8x", // mCharHr.hashCode()); // String customVal = mCharCustom == null ? "null" : // String.format("%8x", // mCharCustom.hashCode()); // Log.d(TAG, " mCharBat=" + batVal + " mCharHr=" + hrVal // + " mCharCustom=" + customVal); // batVal = charBat == null ? "null" : String.format("%8x", // charBat.hashCode()); // hrVal = charHr == null ? "null" : String.format("%8x", // charHr.hashCode()); // customVal = charCustom == null ? "null" : String.format("%8x", // charCustom.hashCode()); // Log.d(TAG, " charBat=" + batVal + " charHr=" + hrVal + // " charCustom=" // + customVal); // Log.d(TAG, " mDoBat=" + mDoBat + " mDoHr=" + mDoHr + " mDoCustom=" // + mDoCustom); if (!mSessionInProgress) { mSessionStartTime = new Date().getTime(); } // // DEBUG Check permissions // checkPermissions(charBat, charHr, charCustom); // Stop notifying for existing characteristics if (mCharHr != null) { setCharacteristicNotification(mCharHr, false); } // Clear any queues while (descriptorWriteQueue.size() > 0) { descriptorWriteQueue.remove(); } while (characteristicReadQueue.size() > 0) { characteristicReadQueue.remove(); } // Initialize for the new values mCharBat = charBat; mCharHr = charHr; mLastBat = INVALID_INT; mLastHr = INVALID_INT; mLastRr = INVALID_STRING; mLastHrDate = new Date().getTime(); BluetoothGattDescriptor descriptor; if (mCharBat != null) { characteristicReadQueue.add(mCharBat); } if (mCharHr != null) { descriptor = mCharHr.getDescriptor(UUID_CLIENT_CHARACTERISTIC_CONFIG); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); descriptorWriteQueue.add(descriptor); setCharacteristicNotification(mCharHr, true); } // Start the queues. Do writeDescriptors before any readCharacteristics if (descriptorWriteQueue.size() > 0) { mBluetoothGatt.writeDescriptor(descriptorWriteQueue.element()); } else if (characteristicReadQueue.size() > 0) { mBluetoothGatt.readCharacteristic(characteristicReadQueue.element()); } mSessionInProgress = true; return true; }
From source file:org.bcsphere.bluetooth.BluetoothG43plus.java
@Override public void addServices(JSONArray json, CallbackContext callbackContext) { Log.i(TAG, "addServices"); if (!isOpenGattServer) { mBluetoothGattServer = mBluetoothManager.openGattServer(mContext, mGattServerCallback); isOpenGattServer = true;/*from w w w. j av a 2 s.c om*/ } addServiceCC = callbackContext; JSONArray services = Tools.getArray(json, Tools.SERVICES); gattServerSum = services.length(); for (int i = 0; i < services.length(); i++) { String uniqueID = Tools.getData(services, i, Tools.UINQUE_ID); int serviceType = -1; if (Tools.getData(services, i, Tools.SERVICE_TYPE).equals("0")) { serviceType = BluetoothGattService.SERVICE_TYPE_PRIMARY; } else { serviceType = BluetoothGattService.SERVICE_TYPE_SECONDARY; } UUID serviceUUID = UUID.fromString(Tools.getData(services, i, Tools.SERVICE_UUID)); BluetoothGattService service = new BluetoothGattService(serviceUUID, serviceType); JSONArray characteristics = Tools.getArray(services, i, Tools.CHARACTERISTICS); for (int j = 0; j < characteristics.length(); j++) { byte[] characteristicValue = Tools .decodeBase64(Tools.getData(characteristics, Tools.CHARACTERISTIC_VALUE)); UUID characteristicUUID = UUID .fromString(Tools.getData(characteristics, Tools.CHARACTERISTIC_UUID)); int characteristicProperty = Tools .encodeProperty(Tools.getArray(characteristics, Tools.CHARACTERISTIC_PROPERTY)); int characteristicPermission = Tools .encodePermission(Tools.getArray(characteristics, Tools.CHARACTERISTIC_PERMISSION)); BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUUID, characteristicProperty, characteristicPermission); characteristic.setValue(characteristicValue); JSONArray descriptors = Tools.getArray(characteristics, j, Tools.DESCRIPTORS); for (int k = 0; k < descriptors.length(); k++) { byte[] descriptorValue = Tools.decodeBase64(Tools.getData(descriptors, Tools.DESCRIPTOR_VALUE)); UUID descriptorUUID = UUID.fromString(Tools.getData(descriptors, Tools.DESCRIPTOR_UUID)); int descriptorPermission = Tools .encodePermission(Tools.getArray(descriptors, Tools.DESCRIPTOR_PERMISSION)); BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(descriptorUUID, descriptorPermission); descriptor.setValue(descriptorValue); characteristic.addDescriptor(descriptor); } service.addCharacteristic(characteristic); } if (mBluetoothGattServer.addService(service)) { localServices.put(uniqueID, service); } } }
From source file:org.bcsphere.bluetooth.BluetoothG43plus.java
@Override public void writeValue(JSONArray json, CallbackContext callbackContext) { Log.i(TAG, "writeValue"); String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS); if (connectedDevice.get(deviceAddress) == null) { Tools.sendErrorMsg(callbackContext); return;// w w w . j a va 2 s .c o m } int serviceIndex = Integer.parseInt(Tools.getData(json, Tools.SERVICE_INDEX)); int characteristicIndex = Integer.parseInt(Tools.getData(json, Tools.CHARACTERISTIC_INDEX)); String descriptorIndex = Tools.getData(json, Tools.DESCRIPTOR_INDEX); String writeValue = Tools.getData(json, Tools.WRITE_VALUE); writeValueCC.put(deviceAddress, callbackContext); if (descriptorIndex.equals("")) { BluetoothGattCharacteristic characteristic = deviceServices.get(deviceAddress).get(serviceIndex) .getCharacteristics().get(characteristicIndex); characteristic.setValue(Tools.decodeBase64(writeValue)); mBluetoothGatts.get(deviceAddress).writeCharacteristic(characteristic); } else { BluetoothGattDescriptor descriptor = deviceServices.get(deviceAddress).get(serviceIndex) .getCharacteristics().get(characteristicIndex).getDescriptors() .get(Integer.parseInt(descriptorIndex)); descriptor.setValue(Tools.decodeBase64(writeValue)); mBluetoothGatts.get(deviceAddress).writeDescriptor(descriptor); } }
From source file:net.kenevans.android.hxmmonitor.HxMBleService.java
/** * Starts a session.// www . ja v a 2s .co m * * @param charBat * @param charHr * @param charCustom * @return If successful. */ public boolean startSession(BluetoothGattCharacteristic charBat, BluetoothGattCharacteristic charHr, BluetoothGattCharacteristic charCustom, boolean doBat, boolean doHr, boolean doCustom) { Log.d(TAG, "startSession"); // Log.d(TAG, "startSession: mSessionState=" + mSessionState // + " mTimeoutTimer=" + mTimeoutTimer); mDoBat = doBat; mDoHr = doHr; mDoCustom = doCustom; // // DEBUG // String batVal = mCharBat == null ? "null" : String.format("%8x", // mCharBat.hashCode()); // String hrVal = mCharHr == null ? "null" : String.format("%8x", // mCharHr.hashCode()); // String customVal = mCharCustom == null ? "null" : // String.format("%8x", // mCharCustom.hashCode()); // Log.d(TAG, " mCharBat=" + batVal + " mCharHr=" + hrVal // + " mCharCustom=" + customVal); // batVal = charBat == null ? "null" : String.format("%8x", // charBat.hashCode()); // hrVal = charHr == null ? "null" : String.format("%8x", // charHr.hashCode()); // customVal = charCustom == null ? "null" : String.format("%8x", // charCustom.hashCode()); // Log.d(TAG, " charBat=" + batVal + " charHr=" + hrVal + // " charCustom=" // + customVal); // Log.d(TAG, " mDoBat=" + mDoBat + " mDoHr=" + mDoHr + " mDoCustom=" // + mDoCustom); boolean res = true; mSessionStartTime = new Date().getTime(); // // DEBUG Check permissions // checkPermissions(charBat, charHr, charCustom); // Stop notifying for existing characteristics if (mCharHr != null) { setCharacteristicNotification(mCharHr, false); } if (mCharCustom != null) { setCharacteristicNotification(mCharCustom, false); } // Clear any queues while (descriptorWriteQueue.size() > 0) { descriptorWriteQueue.remove(); } while (characteristicReadQueue.size() > 0) { characteristicReadQueue.remove(); } // Initialize for the new values mCharBat = charBat; mCharHr = charHr; mCharCustom = charCustom; mLastBat = INVALID_INT; mLastHr = INVALID_INT; mLastRr = INVALID_STRING; mLastHrDate = new Date().getTime(); mLastAct = INVALID_INT; mLastPa = INVALID_INT; BluetoothGattDescriptor descriptor; if (mCharBat != null && mDoBat) { characteristicReadQueue.add(mCharBat); } if (mCharHr != null && mDoHr) { descriptor = mCharHr.getDescriptor(UUID_CLIENT_CHARACTERISTIC_CONFIG); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); descriptorWriteQueue.add(descriptor); setCharacteristicNotification(mCharHr, true); } if (mCharCustom != null && mDoCustom) { descriptor = mCharCustom.getDescriptor(UUID_CLIENT_CHARACTERISTIC_CONFIG); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); descriptorWriteQueue.add(descriptor); setCharacteristicNotification(mCharCustom, true); } // Start the queues. Do writeDescriptors before any readCharacteristics if (descriptorWriteQueue.size() > 0) { mBluetoothGatt.writeDescriptor(descriptorWriteQueue.element()); } else if (characteristicReadQueue.size() > 0) { mBluetoothGatt.readCharacteristic(characteristicReadQueue.element()); } mSessionInProgress = res; return res; }
From source file:com.megster.cordova.ble.central.Peripheral.java
private void registerNotifyCallback(CallbackContext callbackContext, UUID serviceUUID, UUID characteristicUUID) { boolean success = false; if (gatt == null) { callbackContext.error("BluetoothGatt is null"); return;//from w w w . j a v a 2s.c o m } BluetoothGattService service = gatt.getService(serviceUUID); BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID); String key = generateHashKey(serviceUUID, characteristic); if (characteristic != null) { notificationCallbacks.put(key, callbackContext); if (gatt.setCharacteristicNotification(characteristic, true)) { // Why doesn't setCharacteristicNotification write the descriptor? BluetoothGattDescriptor descriptor = characteristic .getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID); if (descriptor != null) { // prefer notify over indicate if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); } else { LOG.w(TAG, "Characteristic " + characteristicUUID + " does not have NOTIFY or INDICATE property set"); } if (gatt.writeDescriptor(descriptor)) { success = true; } else { callbackContext.error( "Failed to set client characteristic notification for " + characteristicUUID); } } else { callbackContext.error("Set notification failed for " + characteristicUUID); } } else { callbackContext.error("Failed to register notification for " + characteristicUUID); } } else { callbackContext.error("Characteristic " + characteristicUUID + " not found"); } if (!success) { commandCompleted(); } }
From source file:ble.AndroidBle.java
@Override public boolean characteristicNotification(String address, BleGattCharacteristic characteristic) { BleRequest request = mService.getCurrentRequest(); BluetoothGatt gatt = mBluetoothGatts.get(address); if (gatt == null || characteristic == null) { return false; }// w w w .j av a2s .com boolean enable = true; if (request.type == RequestType.CHARACTERISTIC_STOP_NOTIFICATION) { enable = false; } BluetoothGattCharacteristic c = characteristic.getGattCharacteristicA(); if (!gatt.setCharacteristicNotification(c, enable)) { return false; } BluetoothGattDescriptor descriptor = c.getDescriptor(BleService.DESC_CCC); if (descriptor == null) { return false; } byte[] val_set = null; if (request.type == RequestType.CHARACTERISTIC_NOTIFICATION) { val_set = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE; } else if (request.type == RequestType.CHARACTERISTIC_INDICATION) { val_set = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE; } else { val_set = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE; } if (!descriptor.setValue(val_set)) { return false; } return gatt.writeDescriptor(descriptor); }
From source file:org.bcsphere.bluetooth.BluetoothG43plus.java
@Override public void setNotification(JSONArray json, CallbackContext callbackContext) { Log.i(TAG, "setNotification"); String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS); if (connectedDevice.get(deviceAddress) == null) { Tools.sendErrorMsg(callbackContext); return;/*from w w w.j a v a2s. c o m*/ } int serviceIndex = Integer.parseInt(Tools.getData(json, Tools.SERVICE_INDEX)); int characteristicIndex = Integer.parseInt(Tools.getData(json, Tools.CHARACTERISTIC_INDEX)); String enable = Tools.getData(json, Tools.ENABLE); BluetoothGattCharacteristic characteristic = deviceServices.get(deviceAddress).get(serviceIndex) .getCharacteristics().get(characteristicIndex); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Tools.NOTIFICATION_UUID); if (enable.equals("true")) { setNotificationCC.put(characteristic, callbackContext); if (Tools.lookup(characteristic.getProperties(), BluetoothGattCharacteristic.PROPERTY_NOTIFY) != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else { descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); } mBluetoothGatts.get(deviceAddress).writeDescriptor(descriptor); mBluetoothGatts.get(deviceAddress).setCharacteristicNotification(characteristic, true); recordServiceIndex.put(characteristic, serviceIndex); recordCharacteristicIndex.put(characteristic, characteristicIndex); } else { descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); mBluetoothGatts.get(deviceAddress).writeDescriptor(descriptor); mBluetoothGatts.get(deviceAddress).setCharacteristicNotification(characteristic, false); Tools.sendSuccessMsg(callbackContext); setNotificationCC.remove(characteristic); recordServiceIndex.remove(characteristic); recordCharacteristicIndex.remove(characteristic); } }
From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java
private boolean enableDfuNotification() { Log.d(TAG, "enableDfuNotification"); if (mDfuChar == null) { Log.w(TAG, "enableDfuNotification: DFU control point characteristic not initiated"); return false; }/*from w w w . jav a 2s . c o m*/ BluetoothGattDescriptor dfuDesc = mDfuChar .getDescriptor(GattAttributeUUID.UUID_DFU_CONTROL_POINT_DESCRIPTOR); if (dfuDesc == null || !dfuDesc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) || !mBluetoothGattClient.writeDescriptor(dfuDesc)) { Log.e(TAG, "enableDfuNotification: Failed to init DFU descriptor write operation"); return false; } return true; }
From source file:com.umundus.service.NCallServiceOld.java
/** * Enabl Battery Notification//from w w w .j a v a 2s . c o m * */ public void enableBatteryNotification() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "enableBatteryNotification BluetoothAdapter not initialized"); return; } BluetoothGattService BatteryService = mBluetoothGatt.getService(BATTERY_SERVICE); if (BatteryService == null) { showMessage("Battery service not found!"); broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC); return; } BluetoothGattCharacteristic BatteryChar = BatteryService.getCharacteristic(BATTERY_CHARACTERISTIC); if (BatteryChar == null) { showMessage("Battery charateristic not found!"); broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC); return; } mBluetoothGatt.setCharacteristicNotification(BatteryChar, true); mRequestCompleted = false; BluetoothGattDescriptor descriptor = BatteryChar.getDescriptor(CCCD); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); synchronized (mLock) { if (!mRequestCompleted && mConnectionState == STATE_DISCOVERED) { try { mLock.wait(); // onDescriptorWrite ? notify . } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }