List of usage examples for android.bluetooth BluetoothGattDescriptor setValue
public boolean setValue(byte[] value)
From source file:com.umundus.service.NCallServiceOld.java
/** * Enable Key Button Notification/*from www . j av a2s . c o m*/ * */ public void enableKeyInputNotification() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "enableKeyInputNotification BluetoothAdapter not initialized"); return; } BluetoothGattService KeyInputService = mBluetoothGatt.getService(NCALL_REMOCON_SERVICE); if (KeyInputService == null) { showMessage("key input service not found!"); broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC); return; } // ? KEY ? ?? . BluetoothGattCharacteristic KeyInputChar = KeyInputService .getCharacteristic(NCALL_REMOCON_KEY_NOTI_CHARACTERISTIC); if (KeyInputChar == null) { showMessage("Key input charateristic not found!"); broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_SRC); return; } mRequestCompleted = false; mBluetoothGatt.setCharacteristicNotification(KeyInputChar, true); BluetoothGattDescriptor descriptor = KeyInputChar.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(); } } } }
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 *//* www. j a v a2s . com*/ 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. }
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 ava 2 s .c om*/ 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:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java
/** * Enables or disables the notifications for given characteristic. This method is SYNCHRONOUS and wait until the * {@link BluetoothGattCallback#onDescriptorWrite(BluetoothGatt, BluetoothGattDescriptor, int)} will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If * connection state will change, or an error will occur, an exception will be thrown. * /*w w w . j av a 2s. c om*/ * @param gatt * the GATT device * @param characteristic * the characteristic to enable or disable notifications for * @param enable * <code>true</code> to enable notifications, <code>false</code> to disable them * @throws DfuException * @throws UploadAbortedException */ private void setCharacteristicNotification(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final boolean enable) throws DeviceDisconnectedException, DfuException, UploadAbortedException { if (mConnectionState != STATE_CONNECTED_AND_READY) throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState); mErrorState = 0; if (mNotificationsEnabled == enable) return; logi((enable ? "Enabling " : "Disabling") + " notifications..."); // enable notifications locally gatt.setCharacteristicNotification(characteristic, enable); // enable notifications on the device final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG); descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); // We have to wait until device gets disconnected or an error occur try { synchronized (mLock) { while ((mNotificationsEnabled != enable && mConnectionState == STATE_CONNECTED_AND_READY && mErrorState == 0 && !mAborted) || mPaused) mLock.wait(); } } catch (final InterruptedException e) { loge("Sleeping interrupted", e); } if (mAborted) throw new UploadAbortedException(); if (mErrorState != 0) throw new DfuException("Unable to set notifications state", mErrorState); if (mConnectionState != STATE_CONNECTED_AND_READY) throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState); }
From source file:br.liveo.ndrawer.ui.activity.MainActivity.java
License:asdf
public int sendBlockingNotifySetting(bleRequest request) { request.status = bleRequestStatus.processing; int timeout = 0; if (request.characteristic == null) { return -1; }/*from w w w . java 2s . com*/ if (!checkGatt()) return -2; if (mGatt.setCharacteristicNotification(request.characteristic, request.notifyenable)) { BluetoothGattDescriptor clientConfig = request.characteristic .getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG); if (clientConfig != null) { if (request.notifyenable) { // Log.i(TAG, "Enable notification: " + // characteristic.getUuid().toString()); clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else { // Log.i(TAG, "Disable notification: " + // characteristic.getUuid().toString()); clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); } mGatt.writeDescriptor(clientConfig); // Log.i(TAG, "writeDescriptor: " + // characteristic.getUuid().toString()); this.blocking = true; // Set read to be blocking while (this.blocking) { timeout++; waitIdle(1); if (timeout > GATT_TIMEOUT) { this.blocking = false; request.status = bleRequestStatus.timeout; return -1; } //Read failed TODO: Fix this to follow connection interval ! } request.status = bleRequestStatus.done; return lastGattStatus; } } return -3; // Set notification to android was wrong ... }