转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359

参考 : 

-- 官方文档 : https://developer.android.com/guide/topics/connectivity/bluetooth-le.html;

1. 概述

BLE 概述 :

-- 版本支持 : Android 4.3 (API Level 18) 内置框架引入了 蓝牙低功耗方案 (Bluetooth Low Energy, BLE) 支持;

-- 角色支持 : Android 手机只能作为 主设备 (central role), 开发者开发的 APP 可以使用其提供的 API 接口, 用于 发现设备, 遍历服务 (services),  读写服务中的特性 (characteristics).

-- 传统蓝牙对比 : 与传统的蓝牙对比, 蓝牙低功耗方案 (Bluetooth Low Energy) 是出于更低的电量消耗考虑而设计的. 这可以使 Android 应用可以与 BLE 设备进行交流, 这些设备需要很低的电量, 如 近距离传感器, 心率测量设备, 健康设备 等等.

2. 关键术语 和 概念

(1) Generic Attribute Profile (GATT) 通用属性规范

Generic Attribute Profile (GATT) 通用属性规范 :

-- GATT 作用 : GATT 规范是一个针对 在 BLE 连接上的, 发送 和 接收 少量数据的一个规范, 所有的现有的低功耗应用的规范都是基于这个 GATT 规范制定的.

-- 制定者 : 蓝牙技术联盟 (Bluetooth SIG) 为低功耗设备定义了许多规范, 一个 规范 (Profile) 就是 设备如何在特定的应用中工作的详述.

-- 设备规范对应关系 : 此外, 一个设备可以实现多个规范, 如 : 一个设备可以包含一个心率检测器, 和 电量检测器.

(2) Attribute Protocol (ATT) 属性协议

Attribute Protocol (ATT) 属性协议 :

-- ATT 与 GATT 关系 : GATT 规范是建立在 ATT 的上一层的, 这套改改通常被称为 GATT/ATT.

-- ATT 作用 : ATT 被用于优化 BLE 设备的运行, 为了这个目的, ATT (属性协议) 使用尽可能少的字节.

-- ATT 唯一标识 : ATT 中的每个属性都被 一个 UUID (Universally Unique Identifier) 独一无二的进行标识, UUID 是一个 128 比特的标准的字符串 ID, 用于信息的唯一标识.

-- ATT 属性 : ATT 中定义的属性就是 Charicteristics (特性) 和 Services (服务);

(3) Characteristic 特性

Characteristic 特性 :

-- Characteristic 概念 : 一个 Characteristic 特性包含了一个值 和 多个 Descriptor (描述符) 用于描述这个特性的值.

-- 本质 : 一个特性可以被认为是一个类型, 类似于一个类.

(4) Descriptor 描述符

Descriptor 描述符 :

-- 作用 : 描述符 被定义为一些属性, 这些属性用于描述 Characteristic (特性) 的值.

-- 示例 : 例如, 一个 描述符 可以说明一个 可读的描述, 一个 特性值的可接受范围, 或者 一个特性值的测量单元.

(5) Service 服务

Service 服务 :

-- 服务本质 : 服务是 Characteristic (特性) 的集合.

-- 示例 : 如, 你可以有一个 名称为 "Heart Rate Monitor (心率监控)" 的服务, 包含了特性 "Heart Rate Measurement (心率测量)".

-- 参考资料 : 你可以在 bluetooth.org 官网查询到一个基于 GATT 服务 和 规范的列表.

3. 角色 和 职责

(1) 四种角色

Android 设备 与 BLE 设备互动时, 设备的角色 和 职责 :

-- 中心设备 和 外围设备 : 这个角色体系适用于 BLE 连接. 中心设备角色 可以扫描, 查找广播. 外围设备角色 发送广播.

-- GATT 服务器 和 GATT 客户端 : 这个决定了两个设备之间, 一旦建议连接后, 如何进行互相通信.

(2) 中心设备 和 外围设备

BLE 连接需要两种设备都存在 : 为了理解其中的区别, 想象一下 你有一个 Android 设备 和 一个激活的 智能腕表 蓝牙设备. 手机支持作为 中心设备 角色, 智能腕表 蓝牙设备支持作为外围设备角色, 为了建立 BLE 连接, 只有外围设备 或者 只有 中心设备 都不能建立 BLE 连接.

(3) GATT 服务器 和 GATT 客户端

GATT 服务器 和 GATT 客户端 简介 :

-- GATT 服务器 和 GATT 客户端 角色不是固定的 : 一旦手机 和 智能腕表 设备建立了 BLE 连接, 它们开始互相交换 GATT 元数据. 根据它们之间传输的数据类型, 其中的一个会扮演 GATT 服务器的角色.

-- 角色改变示例 : 如果 智能腕表 设备想要向手机报告传感器数据, 那么智能腕表必须当做 GATT 服务器. 如果智能腕表 想要从手机上接受更新数据, 那么 Android 手机就是 GATT 服务器.

-- 手机 和 设备 都可以作为 GATT 服务器 和 客户端 : 在本文档中使用的示例代码, 在 Android 设备上运行的 Android APP 就是 GATT 客户端, BLE 外围设备 就是 GATT 服务器. Android APP 从 GATT 服务器上获取数据, 服务器的 BLE "heart rate monitor (心率监测)" 支持 "Heart Rate Profile (心率规范 - 一种 BLE 蓝牙标准规范)". Android APP 也可以作为 GATT 服务器;

4. BLE 权限

(1) 蓝牙权限简介

Android 蓝牙权限简介 :

-- 权限作用 : 为了在应用中使用蓝牙功能, 必须在 AndroidManifest.xml 中 声明蓝牙权限. 所有的蓝牙通信操作都需要 蓝牙权限 来允许执行, 例如 搜索蓝牙, 蓝牙连接, 数据交互等操作.

-- 搜索设置蓝牙权限 : 如果 APP 要发起设备搜索 或者 管理 蓝牙设置, 需要 提前声明 BLUETOOTH_ADMIN 权限.

-- 注意 : 使用 BLUETOOTH_ADMIN 权限的前提是 必须声明 BLUETOOTH 权限.

(2) 蓝牙权限简介

蓝牙权限示例 :

-- AndroidManifest.xml 声明蓝牙权限示例 :

  1. <uses-permission android:name="android.permission.BLUETOOTH"/>
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

-- 充当 BLE 设备权限 : 如果你的 APP 只需要胜任 BLE 设备的工作, 只需要如下配置 : 

  1. <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

(3) 动态控制 BLE 功能是否使用

动态控制 BLE 是否可用 : 不管怎样, 如果你想要让你的 APP 可以当做 BLE 设备, 但是手机不支持这个操作, 你仍然可以进行如下配置, 只是将其中的 android:required 设置成 false. 此时在运行时, 你可以使用 "PackageManager.hasSystemFeature()" 方法决定 BLE 是否可用.

  1. //使用下面的函数决定 设备上的 BLE 功能 是否可用
  2. //此时你可以选择性的关闭 BLE 相关的功能
  3. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  4. Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
  5. finish();
  6. }

5. 创建 BLE

(1) 创建 BLE 简介

创建 BLE 简介 :

-- 验证 BLE 功能 : 在应用可以通过 BLE 交互之前, 你需要验证设备是否支持 BLE 功能, 如果支持, 确定它是可以使用的.

-- 注意 : 这个检查只有在 下面的配置 设置为 false 时才是必须的;

  1. <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

-- 不支持 BLE 关闭相关功能 : 如果 Android 手机不支持 BLE 功能, 你应该优雅的 关闭 BLE 相关功能. 

-- 支持 BLE 打开蓝牙 : 如果 BLE 支持 BLE 功能, 但是设备的蓝牙是关闭的, 你可以在应用中请求打开设备的蓝牙模块.

-- 步骤总结 : 创建 BLE 蓝牙的过程分成两个步骤, 1. 获取 BluetoothAdapter, 2. 打开 设备的蓝牙模块.

(2) 获取 BluetoothAdapter (蓝牙适配器)

获取 BluetoothAdapter 蓝牙适配器 :

-- BluetoothAdapter 类作用 : 所有的蓝牙活动都需要 BluetoothAdapter, BluetoothAdapter 代表了设备本身的蓝牙适配器 (蓝牙无线设备). 整个系统中只有一个 蓝牙适配器, 应用可以使用 BluetoothAdapter 对象与 蓝牙适配器硬件进行交互.

-- 获取 BluetoothAdapter 代码示例 :

  1. // 初始化蓝牙适配器
  2. final BluetoothManager bluetoothManager =
  3. (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  4. mBluetoothAdapter = bluetoothManager.getAdapter();

-- 注意 : 这个方法使用了 getSystemService() 方法, 返回了一个 BluetoothManager 实例对象, 从 BluetoothManager 实例对象中可以获取 BluetoothAdapter 对象;

(3) 打开蓝牙功能

打开蓝牙 :

-- 检查是否可用 : 为了保证 蓝牙功能是打开的, 调用 BluetoothAdapter 的 isEnable() 方法, 检查蓝牙在当前是否可用. 如果返回 false, 说明当前蓝牙不可用.

-- 示例代码 :

  1. private BluetoothAdapter mBluetoothAdapter;
  2. ...
  3. // 确认当前设备的蓝牙是否可用,
  4. // 如果不可用, 弹出一个对话框, 请求打开设备的蓝牙模块
  5. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
  6. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  7. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  8. }

6. 查找 BLE 设备

(1) 查找所有的 BLE 设备

查找 BLE 设备 :

-- 查找方法参数 : 为了搜索到 BLE 设备, 调用 BluetoothAdapter 的 startLeScan() 方法, 该方法需要一个 BluetoothAdapter.LeScanCallback 类型的参数. 你必须实现这个 LeScanCallback 接口, 因为 BLE 蓝牙设备扫描结果在这个接口中返回.

-- 查找策略 : 蓝牙搜索是非常耗电的, 你需要遵守以下的 中断策略 和 不循环策略.

-- 中断策略 : 只要一发现蓝牙设备, 马上中断扫描.

-- 不循环策略 : 不要循环扫描, 设置一个扫描的最大时间限制. 一个设备在之前可用, 继续扫描可能会使设备不可用, 此外继续扫描会持续浪费电池电量.

-- 源码示例 :

  1. /**
  2. * 搜索 和 展示 可用的蓝牙设备 的 Activity 界面
  3. */
  4. public class DeviceScanActivity extends ListActivity {
  5.  
  6. private BluetoothAdapter mBluetoothAdapter;
  7. private boolean mScanning;
  8. private Handler mHandler;
  9.  
  10. // 10 秒后停止搜索
  11. private static final long SCAN_PERIOD = 10000;
  12. ...
  13. private void scanLeDevice(final boolean enable) {
  14. if (enable) {
  15. // 在一个预先定义的时间段后停止扫描.
  16. mHandler.postDelayed(new Runnable() {
  17. @Override
  18. public void run() {
  19. mScanning = false;
  20. //开始扫描
  21. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  22. }
  23. }, SCAN_PERIOD);
  24.  
  25. mScanning = true;
  26. mBluetoothAdapter.startLeScan(mLeScanCallback);
  27. } else {
  28. mScanning = false;
  29. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  30. }
  31. ...
  32. }
  33. ...
  34. }

(2) 查找特定 BLE 设备

查找特定 BLE 设备 :

-- 方法调用 : 查找特定类型的外围设备, 可以调用下面的方法, 这个方法需要提供一个 UUID 对象数组, 这个 UUID 数组是 APP 支持的 GATT 服务的特殊标识.

-- 示例 :

  1. startLeScan(UUID[], BluetoothAdapter.LeScanCallback)

(3) BluetoothAdapter.LeScanCallback 回调接口

扫描回调接口

-- 接口作用 : BluetoothAdapter.LeScanCallback 实现类, 在这个实现类的接口中返回 BLE 设备扫描结果;

-- 源码示例 :

  1. private LeDeviceListAdapter mLeDeviceListAdapter;
  2. ...
  3. // 设备扫描回调接口
  4. private BluetoothAdapter.LeScanCallback mLeScanCallback =
  5. new BluetoothAdapter.LeScanCallback() {
  6. @Override
  7. public void onLeScan(final BluetoothDevice device, int rssi,
  8. byte[] scanRecord) {
  9. runOnUiThread(new Runnable() {
  10. @Override
  11. public void run() {
  12. mLeDeviceListAdapter.addDevice(device);
  13. mLeDeviceListAdapter.notifyDataSetChanged();
  14. }
  15. });
  16. }
  17. };

(4) 设备扫描类型

设备扫描类型 : 蓝牙设备扫描 在同一个时间扫描时, 只能扫描 BLE 设备 或者 SPP 设备中的一种, 不能同时扫描两种设备.

7. 连接到 GATT 服务

(1) 连接指定 BluetoothDevice 蓝牙设备

连接指定设备 :

-- 连接到 GATT 服务 : 与 BLE 设备交互的第一步是 连接到 BLE 设备中的 GATT 服务.

-- 实现方法 : 调用 BluetoothDevice 的 connectGatt() 方法可以连接到 BLE 设备的 GATT 服务.

-- 参数解析 : connectGatt() 方法需要三个参数, 参数一 Context 上下文对象, 参数二 boolean autoConnect 是否自动连接扫描到的蓝牙设备, 参数三 BluetoothGattCallback 接口实现类.

-- 用法示例 :

  1. mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

-- 获取 BluetoothGatt 对象 : 调用 connectGatt() 方法可以连接到 BLE 设备上的 GATT 服务, 返回一个 BluetoothGatt 实例对象, 你可以使用这个对象去 管理 GATT 客户端操作.

-- GATT 客户端操作 : Android APP 可以调用 GATT Client (客户端). BluetoothGattCallback 可以用于传递结果到 GATT 客户端, 如 连接状态 和 更进一步的 GATT Client 操作.

(2) GATT 数据交互示例

BLE 蓝牙数据交互 :

-- 界面 : 在下面的示例中, BLE 应用提供了一个 Activity 界面, 该 Activity 界面用于 连接, 展示数据, 展示 GATT 服务 和 设备支持的特性.

-- BLE 蓝牙服务类 : 基于用户的输入, 这个 Activity 界面可以与一个 BluetoothLeService 的服务进行交流, 该交流的本质就是 BLE 设备的 GATT 服务 与 Android 的 BLE API 进行交流.

-- BLE 蓝牙服务类 示例代码 :

  1. // BLE 设备可以通过该服务 与 Android 的 BLE API 进行互动
  2. public class BluetoothLeService extends Service {
  3. private final static String TAG = BluetoothLeService.class.getSimpleName();
  4.  
  5. private BluetoothManager mBluetoothManager;
  6. private BluetoothAdapter mBluetoothAdapter;
  7. private String mBluetoothDeviceAddress;
  8. private BluetoothGatt mBluetoothGatt;
  9. private int mConnectionState = STATE_DISCONNECTED;
  10.  
  11. private static final int STATE_DISCONNECTED = 0;
  12. private static final int STATE_CONNECTING = 1;
  13. private static final int STATE_CONNECTED = 2;
  14.  
  15. public final static String ACTION_GATT_CONNECTED =
  16. "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
  17. public final static String ACTION_GATT_DISCONNECTED =
  18. "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
  19. public final static String ACTION_GATT_SERVICES_DISCOVERED =
  20. "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
  21. public final static String ACTION_DATA_AVAILABLE =
  22. "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
  23. public final static String EXTRA_DATA =
  24. "com.example.bluetooth.le.EXTRA_DATA";
  25.  
  26. public final static UUID UUID_HEART_RATE_MEASUREMENT =
  27. UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
  28.  
  29. // BLE API 中定义的不同的回调方法.
  30. private final BluetoothGattCallback mGattCallback =
  31. new BluetoothGattCallback() {
  32. @Override
  33. // BLE 设备的状态改变 连接 断开
  34. public void onConnectionStateChange(BluetoothGatt gatt, int status,
  35. int newState) {
  36. String intentAction;
  37. if (newState == BluetoothProfile.STATE_CONNECTED) {
  38. intentAction = ACTION_GATT_CONNECTED;
  39. mConnectionState = STATE_CONNECTED;
  40. broadcastUpdate(intentAction);
  41. Log.i(TAG, "连接到了 GATT 服务.");
  42. Log.i(TAG, "尝试搜索服务:" +
  43. mBluetoothGatt.discoverServices());
  44.  
  45. } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
  46. intentAction = ACTION_GATT_DISCONNECTED;
  47. mConnectionState = STATE_DISCONNECTED;
  48. Log.i(TAG, "于 GATT 服务断开连接.");
  49. broadcastUpdate(intentAction);
  50. }
  51. }
  52.  
  53. @Override
  54. // BLE 设备中 新的 GATT 服务被发现
  55. public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  56. if (status == BluetoothGatt.GATT_SUCCESS) {
  57. broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
  58. } else {
  59. Log.w(TAG, "发现 GATT 服务 : " + status);
  60. }
  61. }
  62.  
  63. @Override
  64. // 特性读取操作返回的数据
  65. public void onCharacteristicRead(BluetoothGatt gatt,
  66. BluetoothGattCharacteristic characteristic,
  67. int status) {
  68. if (status == BluetoothGatt.GATT_SUCCESS) {
  69. broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
  70. }
  71. }
  72. ...
  73. };
  74. ...
  75. }

-- 广播发送 : 当一个特定的回调被触发, 它调用适当的 broadcastUpdate() 帮助方法, 将其当做一个 Action 操作传递出去.

-- 注意蓝牙心率 : 这部分的数据解析 与 蓝牙心率测量 是一起被执行的.

-- 广播发送 示例代码 :

  1. private void broadcastUpdate(final String action) {
  2. final Intent intent = new Intent(action);
  3. sendBroadcast(intent);
  4. }
  5.  
  6. private void broadcastUpdate(final String action,
  7. final BluetoothGattCharacteristic characteristic) {
  8. final Intent intent = new Intent(action);
  9.  
  10. // This is special handling for the Heart Rate Measurement profile. Data
  11. // parsing is carried out as per profile specifications.
  12. // 心率监测规范的特殊处理
  13. // 数据解析在每个规范中完成
  14. if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
  15. int flag = characteristic.getProperties();
  16. int format = -1;
  17. if ((flag & 0x01) != 0) {
  18. format = BluetoothGattCharacteristic.FORMAT_UINT16;
  19. Log.d(TAG, "心率格式 UINT16.");
  20. } else {
  21. format = BluetoothGattCharacteristic.FORMAT_UINT8;
  22. Log.d(TAG, "心率格式 UINT8.");
  23. }
  24. final int heartRate = characteristic.getIntValue(format, 1);
  25. Log.d(TAG, String.format("接收到心跳检测 : %d", heartRate));
  26. intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
  27. } else {
  28. // 对于其它的规范, 写出 HEX 十六进制格式的数据
  29. final byte[] data = characteristic.getValue();
  30. if (data != null && data.length > 0) {
  31. final StringBuilder stringBuilder = new StringBuilder(data.length);
  32. for(byte byteChar : data)
  33. stringBuilder.append(String.format("%02X ", byteChar));
  34. intent.putExtra(EXTRA_DATA, new String(data) + "\n" +
  35. stringBuilder.toString());
  36. }
  37. }
  38. sendBroadcast(intent);
  39. }

-- 处理广播事件 : 在 DeviceControlActivity 中处理广播事件, 示例代码 : 

  1. // 处理 Service 发起的的不同事件
  2. // ACTION_GATT_CONNECTED: 连接到 GATT 服务.
  3. // ACTION_GATT_DISCONNECTED: 与 GATT 服务断开.
  4. // ACTION_GATT_SERVICES_DISCOVERED: 发现 GATT 服务.
  5. // ACTION_DATA_AVAILABLE: 从 BLE 设备中接收数据, 数据可以是 read 或者 notification 操作的结果.
  6. private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. final String action = intent.getAction();
  10. if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
  11. mConnected = true;
  12. updateConnectionState(R.string.connected);
  13. invalidateOptionsMenu();
  14. } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
  15. mConnected = false;
  16. updateConnectionState(R.string.disconnected);
  17. invalidateOptionsMenu();
  18. clearUI();
  19. } else if (BluetoothLeService.
  20. ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
  21. // 在用户界面 显示所有支持的服务 和 特性.
  22. displayGattServices(mBluetoothLeService.getSupportedGattServices());
  23. } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
  24. displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
  25. }
  26. }
  27. };

8. 读取 BLE 属性

读写属性简介 :

-- 读写属性前提 : Android 应用连接到了 设备中的 GATT 服务, 并且发现了 各种服务 (特性集合), 可以读写其中的属性.

-- 读写属性代码示例 : 遍历服务 (特性集合) 和 特性, 将其展示在 UI 界面中.

  1. public class DeviceControlActivity extends Activity {
  2. ...
  3. // 示范如何通过其所支持的 GATT 遍历 服务 (Services) 和 特性 (Characteristics)
  4. // 在这个示例中, 我们将查询出的数据填充到 UI 界面中的 ExpandableListView 中
  5. private void displayGattServices(List<BluetoothGattService> gattServices) {
  6. if (gattServices == null) return;
  7. String uuid = null;
  8. String unknownServiceString = getResources().
  9. getString(R.string.unknown_service);
  10. String unknownCharaString = getResources().
  11. getString(R.string.unknown_characteristic);
  12. ArrayList<HashMap<String, String>> gattServiceData =
  13. new ArrayList<HashMap<String, String>>();
  14. ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
  15. = new ArrayList<ArrayList<HashMap<String, String>>>();
  16. mGattCharacteristics =
  17. new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
  18.  
  19. // 遍历 GATT 服务
  20. for (BluetoothGattService gattService : gattServices) {
  21. HashMap<String, String> currentServiceData =
  22. new HashMap<String, String>();
  23. uuid = gattService.getUuid().toString();
  24. currentServiceData.put(
  25. LIST_NAME, SampleGattAttributes.
  26. lookup(uuid, unknownServiceString));
  27. currentServiceData.put(LIST_UUID, uuid);
  28. gattServiceData.add(currentServiceData);
  29.  
  30. ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
  31. new ArrayList<HashMap<String, String>>();
  32.  
  33. // 获取服务中的特性集合
  34. List<BluetoothGattCharacteristic> gattCharacteristics =
  35. gattService.getCharacteristics();
  36. ArrayList<BluetoothGattCharacteristic> charas =
  37. new ArrayList<BluetoothGattCharacteristic>();
  38.  
  39. // 循环遍历特性集合
  40. for (BluetoothGattCharacteristic gattCharacteristic :
  41. gattCharacteristics) {
  42. charas.add(gattCharacteristic);
  43. HashMap<String, String> currentCharaData =
  44. new HashMap<String, String>();
  45. uuid = gattCharacteristic.getUuid().toString();
  46. currentCharaData.put(
  47. LIST_NAME, SampleGattAttributes.lookup(uuid,
  48. unknownCharaString));
  49. currentCharaData.put(LIST_UUID, uuid);
  50. gattCharacteristicGroupData.add(currentCharaData);
  51. }
  52. mGattCharacteristics.add(charas);
  53. gattCharacteristicData.add(gattCharacteristicGroupData);
  54. }
  55. ...
  56. }
  57. ...
  58. }

9. 接收 GATT 通知

GATT 通知简介
-- 特性改变通知 : 当 BLE 设备中的一些特殊的特性改变, 需要通知与之连接的 Android BLE 应用.

-- 代码示例 : 使用 setCharacteristicNotification() 方法为特性设置通知.

  1. private BluetoothGatt mBluetoothGatt;
  2. BluetoothGattCharacteristic characteristic;
  3. boolean enabled;
  4. ...
  5. // 设置是否监听某个特性改变
  6. mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
  7. ...
  8. BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
  9. UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
  10. descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  11. mBluetoothGatt.writeDescriptor(descriptor);

-- 特性改变回调 : 一但特性开启了改变通知监听, 如果特性发生了改变, 就会回调 BluetoothGattCallback 接口中的 onCharacteristicChanged() 方法.

  1. @Override
  2. // 特性通知
  3. public void onCharacteristicChanged(BluetoothGatt gatt,
  4. BluetoothGattCharacteristic characteristic) {
  5. broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
  6. }

10. 关闭 APP 中的 BLE 连接

关闭 BLE 设备连接 :

-- 关闭方法 : 一旦结束了 BLE 设备的使用, 调用 BluetoothGatt 的 close() 方法, 关闭 BLE 连接, 释放相关的资源.

-- 关闭示例 :

  1. public void close() {
  2. if (mBluetoothGatt == null) {
  3. return;
  4. }
  5. mBluetoothGatt.close();
  6. mBluetoothGatt = null;
  7. }

转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359

【Android应用开发】Android 蓝牙低功耗 (BLE) ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)的更多相关文章

  1. 使用wepy开发微信小程序商城第一篇:项目初始化

    使用wepy开发微信小程序商城 第一篇:项目初始化 前言: wepy小程序项目初始化的操作,官方文档看了好几遍,感觉写得不是很清楚. 这篇写得挺好的:小程序开发之wepy 1.初始化项目 (1)全局安 ...

  2. Android开发UI之开源项目第一篇——个性化控件(View)篇

    原文:http://blog.csdn.net/java886o/article/details/24355907 本文为那些不错的Android开源项目第一篇——个性化控件(View)篇,主要介绍A ...

  3. 码农人生——从未学过Android如何开发Android App 案例讲解-第002期案例

    标题有点晃眼,本次分享是002期博文的实践故事,不会有任何代码.也不会教别人android 如何开发,类似博文已经有大批大批,而且还会有陆陆续续的人写,我写的文章,主要是经验之谈,希望总结出的一些方法 ...

  4. android studio 开发android app 真机调试

    大家都知道开发android app 的时候可以有2种调试方式, 一种是Android Virtual Device(虚拟模拟器) ,另一种就是真机调试. 这里要说的是真机调试的一些安装步骤: 1. ...

  5. 【笔记】Python集成开发环境——PyCharm 2018.3下载、注册、帮助文档

    [博客导航] [Python导航] 前言 使用好的开发环境将有效提高编程效率,在Python使用上我是小白,所以特意请教了从事语言处理的成同学,告知我,推荐使用Pycharm和IntelliJ. 目前 ...

  6. Apache nifi 第一篇(概述)

    1.什么是Apache NiFi? 简单地说,NiFi是为了自动化系统之间的数据流.虽然数据流这种形式很容易理解,但我们在此使用它来表示系统之间的自动化和不同系统之间数据的流转.企业拥有多个系统,其中 ...

  7. 用Android Studio 开发Android应用

    目前AndroidStudio已经到了1.2版本了,我用了下,觉得还蛮好的,有些自动生成的资源,它会自动帮你管理.下面开始列一下,我的开发环境搭配.在开始前,你得有个VPN,可者代理.嗯.不然你下不了 ...

  8. [Hybrid App]--Android混合开发,Android、Js的交互

    AndroidJs通信 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !imp ...

  9. Android Studio开发Android应用如何签名

    1.使用jdk自带的工具生成keystore 使用cmd命令行进入到jdk的bin目录(比如:C:\Program Files\Java\jdk1.7.0_01\bin) 运行如下命令: C:\Pro ...

随机推荐

  1. [ZJOI 2006]超级麻将

    Description Input 第一行一个整数N(N<=100),表示玩了N次超级麻将. 接下来N行,每行100个数a1..a100,描述每次玩牌手中各种牌的数量.ai表示数字为i的牌有ai ...

  2. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  3. 【Remmarguts' Date】

    一道来自POJ2449的题,它融合了单源点最短路算法.启发式搜索,让我们对“启发式”有更深的理解和体会.Wow! ·英文题,述大意:       读入n,m(n<=1000,m<=1000 ...

  4. 习题 7-3 uva211

    题意:给你28个多米勒牌,要求刚好铺满一个7x8的图,输出所有答 案.每个牌只能使用一次 思路: 对每个位置分别搜索其右边 和 下边. 但是在中途,细节上有点问题.最开始想的是搜到最后一个点输出答案, ...

  5. 基于 Hexo + GitHub Pages 搭建个人博客(三)

    一.添加扫描二维码关注功能 打开 themes 目录下的 next 主题配置文件,找到 Wechat Subscriber 标签,将该标签下的配置改成如下形式: # Wechat Subscriber ...

  6. teachable-machine:探索机器学习如何工作,浏览器中实时浏览

    教学机器是一个实验,让所有人都非常方便的探索机器学习,在浏览器中实时浏览,不需要编程.学习更多实验,然后亲自尝试它 访问:https://teachablemachine.withgoogle.com ...

  7. (MariaDB)开窗函数用法

    本文目录: 1.1 窗口和开窗函数简介 1.2 OVER()语法和执行位置 1.3 row_number()对分区排名 1.4 rank()和dense_rank() 1.5 percent_rank ...

  8. 使用foreach需要判空。

    今天写代码的时候,需要遍历一个作为参数传递进来的容器, 当时顺手就加上了判空条件: if(null==list)return; 后来就像,不知道遍历(foreach)有没有帮我做这个工作: 下面看实验 ...

  9. javascript装饰器模式

    装饰器模式 什么是装饰器 原名decorator 被翻译为装饰器 可以理解为装饰 修饰 包装等意 现实中的作用 一间房子通过装饰可以变得更华丽,功能更多 类似一部手机可以单独使用 但是很多人都愿意家个 ...

  10. 重写轮子之 kNN

    # !/usr/bin/python # -*- coding:utf-8 -*- """ Re-implement kNN algorithm as a practic ...