您的足迹:首页 > 蓝牙BLE >Android 蓝牙4.0开启Notification 和 Indication

Android 蓝牙4.0开启Notification 和 Indication

______________________________________________________________

Android BLE 框架发布,功能全面,简单易用:

https://github.com/a1anwang/okble

______________________________________________________________

前言:关于Notification 和Indication  的区别,请见我另一篇帖子:(心情好了再写)


直接放代码:


开启Notification  :


private boolean enableNotification(boolean enable,
			BluetoothGattCharacteristic characteristic) {
		if (mBluetoothGatt == null || characteristic == null)
			return false;
		if (!mBluetoothGatt.setCharacteristicNotification(characteristic,
				enable))
			return false;
		BluetoothGattDescriptor clientConfig = characteristic
				.getDescriptor(UUIDUtils.CCC);
		if (clientConfig == null)
			return false;
 
		if (enable) {
			clientConfig
					.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
		} else {
			clientConfig
					.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
		}
		return mBluetoothGatt.writeDescriptor(clientConfig);
	}
	
public final static UUID CCC = UUID
			.fromString("00002902-0000-1000-8000-00805f9b34fb");


开启Indication:


private boolean enableIndication(boolean enable,
			BluetoothGattCharacteristic characteristic) {
		if (mBluetoothGatt == null || characteristic == null)
			return false;
		if (!mBluetoothGatt.setCharacteristicNotification(characteristic,
				enable))
			return false;
		BluetoothGattDescriptor clientConfig = characteristic
				.getDescriptor(UUIDUtils.CCC);
		if (clientConfig == null)
			return false;

		if (enable) {
			clientConfig
					.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
		} else {
			clientConfig
					.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
		}
		return mBluetoothGatt.writeDescriptor(clientConfig);
	}




本博客所有文章如无特别注明均为原创。作者:AlanWang复制或转载请以超链接形式注明转自 AlanWang的博客-专注android和蓝牙BLE技术分享
原文地址《Android 蓝牙4.0开启Notification 和 Indication

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(0)