本文整理汇总了Java中android.bluetooth.BluetoothGatt.setCharacteristicNotification方法的典型用法代码示例。如果您正苦于以下问题:Java BluetoothGatt.setCharacteristicNotification方法的具体用法?Java BluetoothGatt.setCharacteristicNotification怎么用?Java BluetoothGatt.setCharacteristicNotification使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.bluetooth.BluetoothGatt
的用法示例。
在下文中一共展示了BluetoothGatt.setCharacteristicNotification方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCharacteristicChanged
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.d(mTAG, "onCharacteristicChanged");
byte[] mValue = characteristic.getValue();
if(characteristic.getUuid().equals(UUID_nRF51822_GET_TEMP)){
mAirTemperature = (mValue[0] << 8 | mValue[1])/10;
gatt.setCharacteristicNotification(characteristic, false);
List<BluetoothGattCharacteristic> lstChars = mGattnRF51822Service.getCharacteristics();
for(BluetoothGattCharacteristic mCharacteristic : lstChars){
List<BluetoothGattDescriptor> descriptors = mCharacteristic.getDescriptors();
BluetoothGattDescriptor mGattnRF51822Descriptor = mCharacteristic.getDescriptor(UUID_nRF51822_DESCRIPTOR_ID);
if(mGattnRF51822Descriptor != null && mCharacteristic.getUuid().equals(UUID_nRF51822_GET_LIGHT)){
gatt.setCharacteristicNotification(mCharacteristic, true);
byte[] mDesValue = {0x01, 0x00};
mGattnRF51822Descriptor.setValue(mDesValue);
gatt.writeDescriptor(mGattnRF51822Descriptor);
}
}
}
else if(characteristic.getUuid().equals(UUID_nRF51822_GET_LIGHT)){
mLight = (mValue[0] << 8 | mValue[1]);
gatt.close();
Log.d(mTAG, "onCharacteristicChanged data=" + getData());
mIsConnecting = false;
}
}
示例2: onServicesDiscovered
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
boolean connected = false;
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_COUNTER_UUID);
if (characteristic != null) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_CONFIG);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
connected = gatt.writeDescriptor(descriptor);
}
}
}
mListener.onConnected(connected);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
示例3: enableNotifications
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public final boolean enableNotifications(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_NOTIFY) == 0)
return false;
gatt.setCharacteristicNotification(characteristic, true);
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
return gatt.writeDescriptor(descriptor);
}
return false;
}
示例4: enableIndications
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@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;
}
示例5: enableNotifications
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Enables notifications 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 enableNotifications(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_NOTIFY) == 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_NOTIFICATION_VALUE);
Logger.v(mLogSession, "Enabling notifications for " + characteristic.getUuid());
Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
return gatt.writeDescriptor(descriptor);
}
return false;
}
示例6: enableIndications
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* 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;
}
示例7: onServicesDiscovered
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(mTAG, "onServicesDiscovered");
if(status == BluetoothGatt.GATT_SUCCESS){
mGattnRF51822Service = gatt.getService(UUID_nRF51822_SERVICE_ID);
if(mGattnRF51822Service != null){
List<BluetoothGattCharacteristic> lstChars = mGattnRF51822Service.getCharacteristics();
for(BluetoothGattCharacteristic mCharacteristic : lstChars){
List<BluetoothGattDescriptor> descriptors = mCharacteristic.getDescriptors();
BluetoothGattDescriptor mGattnRF51822Descriptor = mCharacteristic.getDescriptor(UUID_nRF51822_DESCRIPTOR_ID);
if(mGattnRF51822Descriptor != null && mCharacteristic.getUuid().equals(UUID_nRF51822_GET_TEMP)){
gatt.setCharacteristicNotification(mCharacteristic, true);
byte[] mValue = {0x01, 0x00};
mGattnRF51822Descriptor.setValue(mValue);
gatt.writeDescriptor(mGattnRF51822Descriptor);
}
}
}
}
else{
gatt.close();
mIsConnecting = false;
}
}
示例8: setCharacteristicNotification
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Enable or disable notifications/indications for a given characteristic.
*
* 是否允许刷新特征码
*
* <p>Once notifications are enabled for a characteristic, a
* {@link BluetoothGattCallback#onCharacteristicChanged} callback will be
* triggered if the remote device indicates that the given characteristic
* has changed.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
*
* @param characteristic The characteristic for which to enable notifications
* @param enable Set to true to enable notifications/indications
* @return true, if the requested notification status was set successfully
*/
public boolean setCharacteristicNotification(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
boolean enable) {
if (gatt != null && characteristic != null) {
BleLog.i(TAG, "Characteristic set notification value: " + enable);
/** 是否允许刷新特征码获取数据 */
boolean success = gatt.setCharacteristicNotification(characteristic, enable);
// This is specific to Heart Rate Measurement.(心率测量专用)
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
BleLog.i(TAG, "Heart Rate Measurement set [descriptor] notification value: " + enable);
BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
// 向描述符中写入 'ENABLE_NOTIFICATION_VALUE'
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
return success;
}
return false;
}
示例9: setCharacteristicNotification
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
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);
}
}
}
示例10: setBatteryNotifications
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* This method tries to enable notifications on the Battery Level characteristic.
*
* @param enable <code>true</code> to enable battery notifications, false to disable
* @return true if request has been sent
*/
public boolean setBatteryNotifications(final boolean enable) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null) {
return false;
}
final BluetoothGattService batteryService = gatt.getService(BATTERY_SERVICE);
if (batteryService == null)
return false;
final BluetoothGattCharacteristic batteryLevelCharacteristic = batteryService.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC);
if (batteryLevelCharacteristic == null)
return false;
// Check characteristic property
final int properties = batteryLevelCharacteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
return false;
gatt.setCharacteristicNotification(batteryLevelCharacteristic, enable);
final BluetoothGattDescriptor descriptor = batteryLevelCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
if (enable) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Logger.a(mLogSession, "Enabling battery level notifications...");
Logger.v(mLogSession, "Enabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
} else {
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
Logger.a(mLogSession, "Disabling battery level notifications...");
Logger.v(mLogSession, "Disabling notifications for " + BATTERY_LEVEL_CHARACTERISTIC);
Logger.d(mLogSession, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x00-00)");
}
return gatt.writeDescriptor(descriptor);
}
return false;
}
示例11: setNotifySensor
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
private void setNotifySensor(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic = gatt.getService(Globals.SNACH_SYSTEM_SERVICE_UUID).getCharacteristic(Globals.SNACH_SYSTEM_UART_TX_UUID);
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor desc = characteristic.getDescriptor(Globals.SNACH_DESCRIPTOR_UUID);
if(desc != null) {
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Log.i("BLE", "Descriptor write: " + gatt.writeDescriptor(desc));
}
}
示例12: onCharacteristicWrite
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Callback indicating the result of a characteristic write operation.
*
* <p>If this callback is invoked while a reliable write transaction is
* in progress, the value of the characteristic represents the value
* reported by the remote device. An application should compare this
* value to the desired value to be written. If the values don't match,
* the application must abort the reliable write transaction.
*
* @param gatt GATT client invoked {@link BluetoothGatt#writeCharacteristic}
* @param characteristic Characteristic that was written to the associated
* remote device.
* @param status The result of the write operation
* {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
// boolean indicating whether or not the next step is successful, default is false
boolean success = false;
// Check if writing was successful and check what it was that we have written so we can
// determine the next step
if (status == BluetoothGatt.GATT_SUCCESS) {
if (characteristic.getUuid().equals(COMMAND_CHARACTERISTIC_UUID)) {
final byte[] value = characteristic.getValue();
// Check if we have written the "Set LED color" command and go to the next step
// (enabling action notification, step 6) if it is
if ((value != null) && (value.length > 0)) {
if (value[0] == (byte) 0x09) {
// Check if the SPIN Service is found
final BluetoothGattService spinService = gatt.getService(SPIN_SERVICE_UUID);
if (spinService != null) {
// Check if the Action Characteristic is found
final BluetoothGattCharacteristic actionCharacteristic
= spinService.getCharacteristic(ACTION_CHARACTERISTIC_UUID);
if (actionCharacteristic != null) {
// Enable notifications for the descriptor and store the result
final BluetoothGattDescriptor descriptor
= actionCharacteristic.getDescriptor(UUID_CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
success = gatt.setCharacteristicNotification(actionCharacteristic, true)
&& gatt.writeDescriptor(descriptor);
}
}
}
} else {
// There's no next step, set success to true
success = true;
}
}
}
}
onStep(gatt, success);
}
示例13: start
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
@Override
protected void start(Connection connection, BluetoothGatt gatt) {
if (type == Type.UNSUBSCRIBE && connection.getCharacteristicsChangedListenerCount(characteristicUUID) > 0) {
NeatleLogger.d("Won't unsubscribe on " + characteristicUUID + " since it has registered listeners");
finish(CommandResult.createEmptySuccess(characteristicUUID));
return;
}
BluetoothGattService service = gatt.getService(serviceUUID);
if (service == null) {
finish(CommandResult.createErrorResult(characteristicUUID, SERVICE_NOT_FOUND));
return;
}
BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID);
if (characteristic == null) {
finish(CommandResult.createErrorResult(characteristicUUID, CHARACTERISTIC_NOT_FOUND));
return;
}
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor == null) {
finish(CommandResult.createErrorResult(characteristicUUID, DESCRIPTOR_NOT_FOUND));
return;
}
boolean turnOn;
byte[] valueToWrite;
switch (type) {
case Type.SUBSCRIBE_INDICATION:
NeatleLogger.d("Subscribing to indications on " + characteristicUUID);
valueToWrite = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
turnOn = true;
break;
case Type.SUBSCRIBE_NOTIFICATION:
NeatleLogger.d("Subscribing to notifications on " + characteristicUUID);
valueToWrite = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
turnOn = true;
break;
case Type.UNSUBSCRIBE:
NeatleLogger.d("Unsubscribing from notifications/indications on " + characteristicUUID);
valueToWrite = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
turnOn = false;
break;
default:
throw new IllegalStateException();
}
byte[] descriptorValue = descriptor.getValue();
if (Arrays.equals(descriptorValue, valueToWrite)) {
NeatleLogger.d("No subscription changes needed - is at " + valueToWrite[0] + " on " + characteristicUUID);
finish(CommandResult.createEmptySuccess(characteristicUUID));
return;
}
if (!gatt.setCharacteristicNotification(characteristic, turnOn)) {
NeatleLogger.e("Failed to change characteristics notification flag on " + characteristicUUID);
finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
return;
}
descriptor.setValue(valueToWrite);
NeatleLogger.e("Writing descriptor on " + characteristicUUID);
if (!gatt.writeDescriptor(descriptor)) {
NeatleLogger.e("Failed to write descriptor on " + characteristicUUID);
finish(CommandResult.createErrorResult(characteristicUUID, BluetoothGatt.GATT_FAILURE));
}
}
示例14: enableCCCD
import android.bluetooth.BluetoothGatt; //导入方法依赖的package包/类
/**
* Enables or disables the notifications for given characteristic. This method is SYNCHRONOUS and wait until the
* {@link android.bluetooth.BluetoothGattCallback#onDescriptorWrite(android.bluetooth.BluetoothGatt, android.bluetooth.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.
*
* @param gatt the GATT device
* @param characteristic the characteristic to enable or disable notifications for
* @param type {@link #NOTIFICATIONS} or {@link #INDICATIONS}
* @throws DfuException
* @throws UploadAbortedException
*/
private void enableCCCD(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int type) throws DeviceDisconnectedException, DfuException, UploadAbortedException {
final String debugString = type == NOTIFICATIONS ? "notifications" : "indications";
if (mConnectionState != STATE_CONNECTED_AND_READY)
throw new DeviceDisconnectedException("Unable to set " + debugString + " state", mConnectionState);
mReceivedData = null;
mError = 0;
if ((type == NOTIFICATIONS && mNotificationsEnabled) || (type == INDICATIONS && mServiceChangedIndicationsEnabled))
return;
logi("Enabling " + debugString + "...");
sendLogBroadcast(LOG_LEVEL_VERBOSE, "Enabling " + debugString + " for " + characteristic.getUuid());
// enable notifications locally
gatt.setCharacteristicNotification(characteristic, true);
// enable notifications on the device
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
descriptor.setValue(type == NOTIFICATIONS ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.writeDescriptor(" + descriptor.getUuid() + (type == NOTIFICATIONS ? ", value=0x01-00)" : ", value=0x02-00)"));
gatt.writeDescriptor(descriptor);
// We have to wait until device receives a response or an error occur
try {
synchronized (mLock) {
while ((((type == NOTIFICATIONS && !mNotificationsEnabled) || (type == INDICATIONS && !mServiceChangedIndicationsEnabled))
&& mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
mLock.wait();
}
} catch (final InterruptedException e) {
loge("Sleeping interrupted", e);
}
if (mAborted)
throw new UploadAbortedException();
if (mError != 0)
throw new DfuException("Unable to set " + debugString + " state", mError);
if (mConnectionState != STATE_CONNECTED_AND_READY)
throw new DeviceDisconnectedException("Unable to set " + debugString + " state", mConnectionState);
}