文档

蓝牙 API 概览

更新时间:

版本要求

蓝牙类型 版本要求 Android 或 iOS 版本要求
BLE 低功耗蓝牙 mPaaS 10.1.60 及以上版本。
  • Android: 5.0 及以上版本
  • iOS:无版本要求
传统蓝牙 mPaaS 10.1.60 及以上版本。 -

基本流程

低功耗蓝牙流程图

传统蓝牙流程图

蓝牙 API

低功耗蓝牙

名称 功能说明
my.connectBLEDevice 连接低功耗蓝牙设备。
my.disconnectBLEDevice 断开与低功耗蓝牙设备的连接。
my.getBLEDeviceCharacteristics 获取蓝牙设备所有 characteristic(特征值)。
my.getBLEDeviceServices 获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。
my.notifyBLECharacteristicValueChange 启用低功耗蓝牙设备特征值变化时的 notify 功能。
my.offBLECharacteristicValueChange 取消监听低功耗蓝牙设备的特征值变化的事件。
my.offBLEConnectionStateChanged 取消低功耗蓝牙连接状态变化事件的监听。
my.onBLECharacteristicValueChange 监听低功耗蓝牙设备的特征值变化的事件。
my.onBLEConnectionStateChanged 监听低功耗蓝牙连接的错误事件,包括设备丢失,连接异常断开等。
my.readBLECharacteristicValue 读取低功耗蓝牙设备特征值中的数据。
my.writeBLECharacteristicValue 向低功耗蓝牙设备特征值中写入数据。

传统蓝牙

名称 功能说明
my.closeBluetoothAdapter 关闭本机蓝牙模块。
my.getBluetoothAdapterState 获取本机蓝牙模块状态。
my.getBluetoothDevices 获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。
my.getConnectedBluetoothDevices 获取处于已连接状态的设备。
my.offBluetoothAdapterStateChange 移除本机蓝牙状态变化的事件的监听。
my.offBluetoothDeviceFound 移除寻找到新的蓝牙设备事件的监听。
my.onBluetoothDeviceFound 搜索到新的蓝牙设备时触发此事件。
my.onBluetoothAdapterStateChange 监听本机蓝牙状态变化的事件。
my.openBluetoothAdapter 初始化小程序蓝牙适配器。
my.startBluetoothDevicesDiscovery 开始搜寻附近的蓝牙外围设备。
my.stopBluetoothDevicesDiscovery 停止搜寻附近的蓝牙外围设备。

调用示例

  1. //初始化
  2. my.openBluetoothAdapter({
  3. success: (res) => {
  4. console.log(res);
  5. }
  6. });
  7. //注册发现事件
  8. my.onBluetoothDeviceFound({
  9. success: (res) => {
  10. let device = res.devices[0];
  11. //连接发现的设备
  12. my.connectBLEDevice({
  13. deviceId: deviceId,
  14. success: (res) => {
  15. console.log(res)
  16. },
  17. fail:(res) => {
  18. },
  19. complete: (res)=>{
  20. }
  21. });
  22. //停止搜索
  23. my.stopBluetoothDevicesDiscovery({
  24. success: (res) => {
  25. console.log(res)
  26. },
  27. fail:(res) => {
  28. },
  29. complete: (res)=>{
  30. }
  31. });
  32. }
  33. });
  34. //注册连接事件
  35. my.onBLEConnectionStateChanged({
  36. success: (res) => {
  37. console.log(res);
  38. if (res.connected) {
  39. //开始读写 notify 等操作
  40. my.notifyBLECharacteristicValueChange({
  41. deviceId: deviceId,
  42. serviceId: serviceId,
  43. characteristicId: characteristicId,
  44. success: (res) => {
  45. console.log(res)
  46. },
  47. fail:(res) => {
  48. },
  49. complete: (res)=>{
  50. }
  51. });
  52. }
  53. }
  54. });
  55. //注册接收 read 或 notify 的数据
  56. my.onBLECharacteristicValueChange({
  57. success: (res) => {
  58. console.log(res);
  59. }
  60. });
  61. //开始搜索
  62. my.startBluetoothDevicesDiscovery({
  63. services: ['fff0'],
  64. success: (res) => {
  65. console.log(res)
  66. },
  67. fail:(res) => {
  68. },
  69. complete: (res)=>{
  70. }
  71. });
  72. //断开连接
  73. my.disconnectBLEDevice({
  74. deviceId: deviceId,
  75. success: (res) => {
  76. console.log(res)
  77. },
  78. fail:(res) => {
  79. },
  80. complete: (res)=>{
  81. }
  82. });
  83. //注销事件
  84. my.offBluetoothDeviceFound();
  85. my.offBLEConnectionStateChanged();
  86. my.offBLECharacteristicValueChange();
  87. //退出蓝牙模块
  88. my.closeBluetoothAdapter({
  89. success: (res) => {
  90. },
  91. fail:(res) => {
  92. },
  93. complete: (res)=>{
  94. }
  95. });
  • 本页导读 (0)
文档反馈