BLE低功耗蓝牙设备开发
2015-08-28 15:24
411 查看
概述
Android API 18开始支持低功耗蓝牙设备(BLE,Bluetooth Low Energy)。BLE与以往的蓝牙设备不同,它长期处于sleep状态,只在有连接事件发生时才被唤醒,所以待机时间很长,适合用于穿戴设备等。本文参考了这篇tutorial:http://toastdroid.com/2014/09/22/android-bluetooth-low-energy-tutorial/
相关概念
介绍与BLE开发相关的几个重要概念GATT Profile
每个BLE设备都实现了一个多个profile,profile是定义手机与BLE通信方式的较高级别的概念。BLE的profile是基于Generic Attribute Profile (GATT)的,GATT描述了BLE中数据片(即Attribute)的传输方式。Client
Client负责发出GATT命令,并接收结果。Android设备即为Client。Server
Server负责接收GATT命令,并返回结果,BLE设备即为Server。Characteristic
Characteristic表示Client和Server之间传输的数据。Service
由若干个Characteristic组成,共同完成某一功能。Descriptor
用于提供Characteristic的额外信息。Attributes
Characteristic、Service、Descriptor都被称为Attributes,由128bit的UUID唯一表示。但开发者一般只需要关心其中的16个bit,即第5到第8个16进制位。xxxxXXXX-xxxx-xxxx-xxxx-xxxxxxxxxxxx
在Android平台开发BLE
接下来是Android平台下的开发要点,再次提醒,需要API 18及以上。AndroidManifest.xml
添加如下权限:<uses-permission android:name=“android.permission.BLUETOOTH” /> <uses-permission android:name=“android.permission.BLUETOOTH_ADMIN” />
获得并启动BluetoothAdapter
BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter btAdapter = btManager.getAdapter(); if (btAdapter != null && !btAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,REQUEST_ENABLE_BT); }
扫描Device
得到BluetoothAdapter后,可以用一下方法开始/停止扫描BLE设备:
btAdapter.startLeScan(leScanCallback); btAdapter.stopLeScan(leScanCallback);
其中
leScanCallback是扫描到BLE设备的回调接口:
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { // 对扫描到的Device进行操作,如建立GATT连接,获取Service、Characteristic等 } }
扫描到需要的设备后,应该尽快停止扫描,扫描是耗电量很大的操作。
定义BluetoothGattCallback
获得Device之后,下一步就是建立连接:BluetoothGatt bluetoothGatt = bluetoothDevice.connectGatt(context, false, btleGattCallback);
其中btleGattCallback是监测连接状态的回调接口:
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() { @Override public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { // 发生characteristic读写操作时被调用 } @Override public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { // 连接建立或连接断开时被调用,建立连接后,可以开始获取Service等 gatt.discoverServices(); } @Override public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { // 当GATT调用了BluetoothGatt.discoverServices()方法时被回调 } }
获得Services和Characteristics
建立GATT连接后,获取GATT包含的BluetoothGattService,返回一个
List<BluetoothGattService>:
bluetoothGatt.discoverServices();
可以使用如下方法继续获得
BluetoothGattService中的
Characteristic:
BluetoothGattService.getCharacteristics();
根据需要,可以对Characteristic进行读写操作:
Characteristic.setValue(byte[]); BluetoothGatt.writeCharacteristic(Characteristic);
关闭
使用结束后关闭GATT:bluetoothGatt.disconnect(); bluetoothGatt.close();
相关文章推荐
- 关于android 控件的单击双击以及长按的问题
- HDU 1026 Ignatius and the Princess I(广搜+路径记录+优先队列)
- 为程序创建桌面快捷方式
- 调整UILabel行高间距(利用iOS 6的特性来手动调整行间矩)
- linux的tar命令详情;linux多个文件压缩打包到一个压缩文件
- java list add的弊病
- 关于查看进程、查看端口号对应的pid
- Python 日期全集
- 例题3.15 子串 UVa11468
- Retrofit在项目中的实战应用
- java 中类为啥要序列化
- Solaris 线程和 POSIX 线程的 API
- 利用canvas的rotate创建一个简易的风车动画
- C# Socket通信
- C语言的那些个关键字们
- 对BitmapFactory.Options.inJustDecodeBounds的理解
- 关于文本输入框获取光标位置以及指定位置插入内容
- 下载matlab2014a的安装程序
- zzuli OJ 1007: 鸡兔同笼
- Note For Linux By Jes(15)-系统配置工具(网络与打印机)与硬件侦测