构建 Bluetooth 移动应用 主讲人 : 任凯 kren@bluetooth.com 微信 :kaiser-tech 2016 年 5 月 6 日 3
无处不在 IOS ANDROID WINDOWS BLACKBERRY TIZEN 4
平台性能
平台 操作系统 操作系统版本 ios / Android / Windows / BlackBerry 10 / Tizen Android 4.3 / 4.4 / 5.0 / 6.0 等 蓝牙协议栈 4.0 / 4.1 / 4.2 硬件 SDK 芯片 Android Java / Android Native / Objective- C / Swift / Apache Cordova / Qt etc 6
性能变化示例 GAP 角色广播数据包内容服务模拟硬件性能语言和 API 中心 / 外围是 / 否 / 部分是 / 否多种 Java / C++ / JavaScript 7
平台性能指南 https://www.bluetooth.com/develop-with-bluetooth/developer-resources-tools/platform-capability-guide 8
基本流程
基本程序 设备发现? 10
广播和扫描 11
扫描 过滤和语法分析 要考虑耗电量! 考虑广播频率 过滤掉不相关的设备 对硬件进行有效的过滤? 对 AD 类型进行语法分析 行动! 12
Android 类 BluetoothManager BluetoothAdapter BluetoothLeScanner ScanCallback ScanFilter ScanSettings ScanResult BluetoothDevice ScanRecord 启动 配置 结果 13
android 配置和扫描 ScanCallback ScanFilter ScanSettings List<ScanFilter> filters = new ArrayList<ScanFilter>(); ScanFilter filter = new ScanFilter.Builder().setDeviceName("Flex").build(); filters.add(filter); ScanSettings settings = new ScanSettings.Builder().setScanMode( ScanSettings.SCAN_MODE_LOW_LATENCY).build(); scanner.startscan(filters, settings, scan_callback); 14
android 扫描结果 ScanResult ScanRecord BluetoothDevice private ScanCallback mlescancallback = new ScanCallback() { public void onscanresult(int callbacktype, final ScanResult result) { runonuithread(new Runnable() { public void run() { ScanRecord ad_packet = result.getscanrecord(); byte[] ad_bytes = ad_packet.getbytes(); AdvertisingPacket packet = new AdvertisingPacket(ad_bytes); adv_log.additem(packet); }}); 15
基本程序 设备发现 建立和维持连接 16
android 连接至设备 BluetoothDevice 通过扫描过程获得可能由用户选择 BluetoothGatt 在连接设备中获得启动蓝牙 GATT 操作 BluetoothGattCallback 通过回调函数来同步相关操作 17
android 扫描结果 ScanResult ScanRecord BluetoothDevice private ScanCallback mlescancallback = new ScanCallback() { public void onscanresult(int callbacktype, final ScanResult result) { runonuithread(new Runnable() { public void run() { ScanRecord ad_packet = result.getscanrecord(); byte[] ad_bytes = ad_packet.getbytes(); AdvertisingPacket packet = new AdvertisingPacket(ad_bytes); adv_log.additem(packet); }}); 18
android 连接至设备 // Extend BluetoothGattCallback public class GattCallbacks extends BluetoothGattCallback { } public void onconnectionstatechange(bluetoothgatt gatt, int status, int newstate) { } 19
android 连接至设备 BluetoothGatt bluetooth_gatt = device.connectgatt(this, true, gatt_callbacks); // after connecting we receive a call back to our BluetoothGattCallback object public void onconnectionstatechange(bluetoothgatt bluetooth_gatt, int status,int newstate) { } if (newstate == BluetoothProfile.STATE_CONNECTED) { observer.gattconnected(); } else if (newstate == BluetoothProfile.STATE_DISCONNECTED) { observer.gattdisconnected(); } 20
维持连接 API 访问连接参数? 否 应用层保持连接 是 设定超时时限 否 重新连接 21
基本程序 服务发现 建立和维持连接 使用 GATT 属性 22
属性协议 属性协议 GATT 客户端 GATT 服务器 23
属性协议 请求 回应 GATT 客户端 GATT 服务器 24
属性协议 通知 确认 GATT 客户端 GATT 服务器 25
属性协议 命令 GATT 客户端 GATT 服务器 无流程控制 服务器可投放命令 链路层的 ACK 确保不会丢失数据包 26
属性协议 通知 GATT 客户端 GATT 服务器 无流程控制 客户端可投放通知 链路层的 ACK 确保不会丢失数据包 27
更多 android 类 BluetoothGattService BluetoothGattCharacteristic BluetoothGattDescriptor BluetoothGatt BluetoothGattCallback 28
android 模拟服务 // call discoverservices on our BluetoothGatt object bluetooth_gatt.discoverservices(); // we'll get a call back when the operation completes public void onservicesdiscovered(bluetoothgatt bluetooth_gatt, int status) { } if (status == BluetoothGatt.GATT_SUCCESS) { List<BluetoothGattService> = bluetooth_gatt.getservices(); } 29
android 读出特征值 public static String ALERT_LEVEL_CHARACTERISTIC = "00002a06-0000-1000-8000-00805f9b34fb";... BluetoothGattService gatt_service;... BluetoothGattCharacteristic gatt_char = gatt_service.getcharacteristic (java.util.uuid.fromstring(characteristicuuid)); // this is an asynchronous operation bluetooth_gatt.readcharacteristic(gatt_char); 30
android 读出特征值 // callback in a BluetoothGattCallback sub-class public void oncharacteristicread(bluetoothgatt gatt, BluetoothGattCharacteristic gatt_char, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { // note we also have getstringvalue(), // getfloatvalue() and getintvalue() } } byte [] char_bytes = gatt_char.getvalue(); 31
android 启用通知 - 本地 public static String HEART_RATE_MEASUREMENT_CHARACTERISTIC = "00002a37-0000-1000-8000-00805f9b34fb"; BluetoothGattCharacteristic gatt_char = gatt_service.getcharacteristic (java.util.uuid.fromstring( HEART_RATE_MEASUREMENT_CHARACTERISTIC)); // Enable local notifications true means ON bluetooth_gatt.setcharacteristicnotification(gatt_char, true); 32
android 启用通知 - 远程 public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb"; // Enable remote notifications BluetoothGattDescriptor descriptor = gatt_char.getdescriptor (UUID.fromString(Constants.CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setvalue (BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // this is an asynchronous operation bluetooth_gatt.writedescriptor(descriptor); 33
android 启用通知 本地 // in our BluetoothGattCallback sub-class public void oncharacteristicchanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } // use the characteristic! 34
基本程序 服务发现 建立和维持连接 使用 GATT 服务 GAP 外围 /GATT 服务器 35
android GAP 外围 ADV 设置 AdvertiseSettings.Builder adv_sett_builder = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setConnectable(true).setTimeout(180000).setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM); 36
android GAP 外围 ADV 数据 String DEVICE_INFORMATION_SERVICE_UUID = "0000180A-0000-1000-8000-00805f9b34fb"; AdvertiseData.Builder adv_data_builder = new AdvertiseData.Builder().setIncludeDeviceName(true).addServiceUuid(new ParcelUuid(UUID.fromString(DEVICE_INFORMATION_SERVICE_UUID))); advertiser.startadvertising(adv_sett_builder.build(), adv_data_builder.build(), adv_cb); 37
android GATT 服务器 App BluetoothGattServer BluetoothGattService BluetoothGattCharacteristic 构建 addcharacteristic 构建 构建 addcharacteristic addservice 38
资源
应用加速器 智能入门套件 AdvScanner 2016 年 5 月 6 日 40
课程计划 41
官方社交媒体与网站 微信微博开发者门户 42
有问题? 微信 :kaiser-tech 微博 :@kaiser_ren Unthinkably Connected 2016 / Bluetooth 年 5 月 6 SIG 日专有文件 43