List of usage examples for android.bluetooth BluetoothGattDescriptor ENABLE_INDICATION_VALUE
null ENABLE_INDICATION_VALUE
To view the source code for android.bluetooth BluetoothGattDescriptor ENABLE_INDICATION_VALUE.
Click Source Link
From source file:com.wolkabout.hexiwear.service.BluetoothService.java
private void createGATT(final BluetoothDevice device) { bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() { @Override/* w w w .ja v a 2 s .c o m*/ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { isConnected = BluetoothProfile.STATE_CONNECTED == newState; if (isConnected) { Log.i(TAG, "GATT connected."); startForeground(442, getNotification(device)); gatt.discoverServices(); } else { Log.i(TAG, "GATT disconnected."); NotificationService_.intent(BluetoothService.this).stop(); notificationManager.notify(442, getNotification(device)); gatt.connect(); } final Intent connectionStateChanged = new Intent(CONNECTION_STATE_CHANGED); connectionStateChanged.putExtra(CONNECTION_STATE, isConnected); sendBroadcast(connectionStateChanged); } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { Log.i(TAG, "Services discovered."); if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) { handleAuthenticationError(gatt); return; } discoverCharacteristics(gatt); } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Log.i(TAG, "Characteristic written: " + status); if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) { handleAuthenticationError(gatt); return; } final byte command = characteristic.getValue()[0]; switch (command) { case WRITE_TIME: Log.i(TAG, "Time written."); final BluetoothGattCharacteristic batteryCharacteristic = readableCharacteristics .get(Characteristic.BATTERY.getUuid()); gatt.setCharacteristicNotification(batteryCharacteristic, true); for (BluetoothGattDescriptor descriptor : batteryCharacteristic.getDescriptors()) { if (descriptor.getUuid().toString().startsWith("00002904")) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); gatt.writeDescriptor(descriptor); } } break; case WRITE_NOTIFICATION: Log.i(TAG, "Notification sent."); if (notificationsQueue.isEmpty()) { Log.i(TAG, "Reading characteristics..."); readNextCharacteristics(gatt); } else { Log.i(TAG, "writing next notification..."); alertIn.setValue(notificationsQueue.poll()); gatt.writeCharacteristic(alertIn); } break; default: Log.w(TAG, "No such ALERT IN command: " + command); break; } } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { readCharacteristic(gatt, Characteristic.MANUFACTURER); } @Override public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic gattCharacteristic, int status) { if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) { handleAuthenticationError(gatt); return; } final String characteristicUuid = gattCharacteristic.getUuid().toString(); final Characteristic characteristic = Characteristic.byUuid(characteristicUuid); switch (characteristic) { case MANUFACTURER: manufacturerInfo.manufacturer = gattCharacteristic.getStringValue(0); readCharacteristic(gatt, Characteristic.FW_REVISION); break; case FW_REVISION: manufacturerInfo.firmwareRevision = gattCharacteristic.getStringValue(0); readCharacteristic(gatt, Characteristic.MODE); break; default: Log.v(TAG, "Characteristic read: " + characteristic.name()); if (characteristic == Characteristic.MODE) { final Mode newMode = Mode.bySymbol(gattCharacteristic.getValue()[0]); if (mode != newMode) { onModeChanged(newMode); } } else { onBluetoothDataReceived(characteristic, gattCharacteristic.getValue()); } if (notificationsQueue.isEmpty()) { readNextCharacteristics(gatt); } else { alertIn.setValue(notificationsQueue.poll()); gatt.writeCharacteristic(alertIn); } break; } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic gattCharacteristic) { final String characteristicUuid = gattCharacteristic.getUuid().toString(); final Characteristic characteristic = Characteristic.byUuid(characteristicUuid); Log.d(TAG, "Characteristic changed: " + characteristic); if (characteristic == Characteristic.BATTERY) { onBluetoothDataReceived(Characteristic.BATTERY, gattCharacteristic.getValue()); } } }); }
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;// w ww . j a v a 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 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.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;/*w ww. j a va 2 s .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; }/*from ww w . j av a 2s.co m*/ 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:com.nbplus.iotapp.bluetooth.BluetoothLeService.java
/** * Enables or disables notification on a give characteristic. * * @param enabled If true, enable notification. False otherwise. *///from w w w . j a v a 2s . co m public boolean setCharacteristicNotification(String address, String serviceUuid, String characteristicUuid, boolean enabled) { Log.d(TAG, "writeRemoteCharacteristic add = " + address + ", svc = " + serviceUuid + ", char = " + characteristicUuid); if (StringUtils.isEmptyString(address) || StringUtils.isEmptyString(serviceUuid) || StringUtils.isEmptyString(characteristicUuid)) { Log.w(TAG, "Unknown parameter"); return false; } BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address); if (mBluetoothAdapter == null || bluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return false; } BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUuid)); if (service == null) { Log.w(TAG, "Service not found."); return false; } BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUuid)); if (characteristic == null) { Log.w(TAG, "characteristic not found."); return false; } bluetoothGatt.setCharacteristicNotification(characteristic, enabled); final int charaProp = characteristic.getProperties(); if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { BluetoothGattDescriptor descriptor = characteristic .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); if (descriptor != null) { Log.d(TAG, ">>>> ENABLE_NOTIFICATION_VALUE : " + characteristic.getUuid().toString()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); bluetoothGatt.writeDescriptor(descriptor); return true; } else { return false; } } else if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) { BluetoothGattDescriptor descriptor = characteristic .getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); if (descriptor != null) { Log.d(TAG, ">>>> ENABLE_INDICATION_VALUE : " + characteristic.getUuid().toString()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); bluetoothGatt.writeDescriptor(descriptor); return true; } else { return false; } } else { return false; } }
From source file:de.frank_durr.ble_v_monitor.MainActivity.java
/** * Query the GATT server for a voltage history. * * @param historyType the history to be retrieved from the GATT server *//*from ww w . j a v a 2s . c o m*/ synchronized private void bleRetrieveHistory(HistoryTypes historyType) { switch (historyType) { case historyMinutely: if (activeBluetoothTask != BluetoothTasks.getMinutelyHistory) { return; } break; case historyHourly: if (activeBluetoothTask != BluetoothTasks.getHourlyHistory) { return; } break; case historyDaily: if (activeBluetoothTask != BluetoothTasks.getDailyHistory) { return; } break; } // Check whether a BLE device has already been selected. If not, select device first. if (bluetoothDevice == null) { // No device selected so far -> let user select device now selectDevice(); return; // Wait for activity result } // BLE device has been selected. if (!bluetoothAdapter.isEnabled()) { // First, turn on Bluetooth Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, REQUEST_ENABLE_BT); return; // Wait for activity result } // BLE device has been selected. BLE is turned on. // From here on, we show a progress dialog ... retrieving hundreds of // BLE indications (stop&wait protocol) can take longer. if (progressDialog == null) { String dialogTitle = getString(R.string.waiting); String dialogMessage = getString(R.string.download_in_progress); String cancel = getString(R.string.cancel); progressDialog = new ProgressDialog(this); progressDialog.setTitle(dialogTitle); progressDialog.setMessage(dialogMessage); progressDialog.setCancelable(true); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(MAX_HISTORY_SIZE); progressDialog.setProgress(0); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finishTask(); } }); progressDialog.show(); } if (gatt == null) { bluetoothDevice.connectGatt(this, false, gattHandler); return; // Wait for callback } // BLE device has been selected. BLE is turned on. We are connected to GATT server. if (!gattServicesDiscovered) { if (!gatt.discoverServices()) { // Cannot start discovery toast(R.string.err_bluetooth_discovery); finishTask(); return; } return; // Wait for GATT callback } // BLE device has been selected. BLE is turned on. We are connected to GATT server. // GATT services have been discovered. gattService = gatt.getService(gattServiceUUID); if (gattService == null) { // Required service not offered by device toast(R.string.err_bluetooth_service); finishTask(); return; } // BLE device has been selected. BLE is turned on. We are connected to GATT server. // GATT services have been discovered. Service is ready. switch (historyType) { case historyMinutely: characteristic = gattService.getCharacteristic(minutelyHistoryCharacteristicUUID); break; case historyHourly: characteristic = gattService.getCharacteristic(hourlyHistoryCharacteristicUUID); break; case historyDaily: characteristic = gattService.getCharacteristic(dailyHistoryCharacteristicUUID); break; } if (characteristic == null) { // Required characteristic is not available. toast(R.string.err_bluetooth_characteristic); finishTask(); return; } // BLE device has been selected. BLE is turned on. We are connected to GATT server. // GATT services have been discovered. Service is ready. Characteristic is available. // History values are returned as indications. -> subscribe for notifications. if (!gatt.setCharacteristicNotification(characteristic, true)) { toast(R.string.err_bluetooth_notification); finishTask(); return; } BluetoothGattDescriptor descriptor = characteristic.getDescriptor(clientCharacteristicConfigurationUUID); if (descriptor == null) { toast(R.string.err_bluetooth_notification); finishTask(); return; } if (!descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)) { toast(R.string.err_bluetooth_notification); finishTask(); return; } if (!gatt.writeDescriptor(descriptor)) { toast(R.string.err_bluetooth_notification); finishTask(); return; } // Data is received and processed in GATT callback. Wait for GATT callback. }