bluetooth

支持搜索发现所有蓝牙设备,但仅支持低功耗蓝牙ble传输协议,不支持蓝牙设备的配对连接传输大量数据。如果要连接非ble蓝牙设备,可以使用Native.js调用(请到http://ask.dcloud.net.cn搜索bluetooth相关问答)。

对象:

回调方法:

权限:

5+功能模块(permissions)

closeBluetoothAdapter

关闭蓝牙模块

  1. void plus.bluetooth.closeBluetoothAdapter(options);

说明:

断开所有已经建立的连接,释放系统资源,要求在蓝牙功能使用完成后调用(于openBluetoothAdapter成对使用)。关闭成功后触发options参数中的success回调,失败触发options参数中的fail回调。

参数:

options参数为json类型,包含以下属性:

  • success: (BluetoothSuccessCallback)可选 关闭蓝牙模块成功回调函数
  • fail: ()可选 关闭蓝牙模块失败回调函数
  • complete: (BluetoothCompleteCallback)可选 关闭蓝牙模块操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 关闭蓝牙模块
  2. function closeBluetoothAdapter(){
  3. plus.bluetooth.closeBluetoothAdapter({
  4. success:function(e){
  5. console.log('close success: '+JSON.stringify(e));
  6. },
  7. fail:function(e){
  8. console.log('close failed: '+JSON.stringify(e));
  9. }
  10. });
  11. }

getBluetoothAdapterState

获取本机蓝牙适配器状态

  1. void plus.bluetooth.getBluetoothAdapterState(options);

说明:

获取成功后触发options参数中的success回调,失败触发options参数中的fail回调。

参数:

options参数为json类型,包含以下属性:

  • success: (BluetoothSuccessCallback)可选 获取蓝牙适配器状态成功回调函数回调函数参数event对象包括以下属性:discovering - Boolean类型,蓝牙适配器是否正在搜索设备;available - Boolean类型,蓝牙适配器是否可用。
  • fail: ()可选 获取蓝牙适配器状态错误回调函数
  • complete: (BluetoothCompleteCallback)可选 获取蓝牙适配器状态操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 获取蓝牙状态
  2. function getBluetoothState(){
  3. plus.bluetooth.getBluetoothAdapterState({
  4. success:function(e){
  5. console.log('state success: '+JSON.stringify(e));
  6. },
  7. fail:function(e){
  8. console.log('state failed: '+JSON.stringify(e));
  9. }
  10. });
  11. }

getBluetoothDevices

获取已搜索到的蓝牙设备

  1. void plus.bluetooth.getBluetoothDevices(options);

说明:

包括已经和本机处于连接状态的设备。获取成功后触发options参数中的success回调,失败触发options参数中的fail回调。

参数:

options参数为json类型,包含以下属性:

  • success: (BluetoothSuccessCallback)可选 获取蓝牙设备成功回调函数回调函数参数event对象包括以下属性:devices - Array,设备列表信息。
  • fail: ()可选 获取蓝牙设备失败回调函数
  • complete: (BluetoothCompleteCallback)可选 获取蓝牙设备操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 获取已搜索到的蓝牙设备
  2. function getBluetoothDevices(){
  3. plus.bluetooth.getBluetoothDevices({
  4. success:function(e){
  5. var devices = e.devices;
  6. console.log('get devices success: '+e.length);
  7. for(var i in devices){
  8. console.log(i+': '+JSON.stringify(devices[i]));
  9. }
  10. },
  11. fail:function(e){
  12. console.log('get devices failed: '+JSON.stringify(e));
  13. }
  14. });
  15. }

getConnectedBluetoothDevices

根据uuid获取处于已连接的设备

  1. void plus.bluetooth.getConnectedBluetoothDevices(options);

说明:

获取成功后触发options参数中的success回调,失败触发options参数中的fail回调。

参数:

options参数为json类型,包含以下属性:

  • services: (Array[String])必选 要获取设备的uuid列表蓝牙设备主service的uuid列表。
  • success: (BluetoothSuccessCallback)可选 获取已连接设备成功回调函数回调函数参数event对象包括以下属性:devices - Array,设备列表信息(只包含name和deviceId属性)。
  • fail: ()可选 获取已连接设备失败回调函数
  • complete: (BluetoothCompleteCallback)可选 获取已连接设备操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 获取已连接的蓝牙设备
  2. function getConnectedDevices(){
  3. success:function(e){
  4. var devices = e.devices;
  5. console.log('connected devices success: '+e.length);
  6. for(var i in devices){
  7. console.log(i+': '+JSON.stringify(devices[i]));
  8. }
  9. },
  10. fail:function(e){
  11. console.log('connected devices failed: '+JSON.stringify(e));
  12. }
  13. });
  14. }

onBluetoothAdapterStateChange

监听蓝牙适配器状态变化事件

  1. void plus.bluetooth.onBluetoothAdapterStateChange(changeCB);

说明:

蓝牙适配器状态发生变化时触发回调。

参数:

  • changeCB: (BluetoothAdapterStateChangeCallback)必选 状态变化回调函数回调函数参数event对象包括以下属性:discovering - Boolean类型,蓝牙适配器是否正在搜索设备;available - Boolean类型,蓝牙适配器是否可用。

返回值:

void: 无

示例:

  1. // 监听状态变化
  2. function listenerStateChange(){
  3. plus.bluetooth.onBluetoothAdapterStateChange(function(e){
  4. console.log('state changed: '+JSON.stringify(e));
  5. });
  6. }

onBluetoothDeviceFound

监听搜索到新设备的事件

  1. void plus.bluetooth.onBluetoothDeviceFound(callback);

说明:

搜索到新设备时触发回调。

参数:

  • callback: (BluetoothDeviceFoundCallback)必选 搜索到新设备回调函数回调函数参数event对象包括以下属性:devices - Array,设备列表信息。

返回值:

void: 无

示例:

  1. // 监听发现新设备
  2. function listenerDeviceFound(){
  3. plus.bluetooth.onBluetoothDeviceFound(function(e){
  4. var devices = e.devices;
  5. console.log('device found: '+e.length);
  6. for(var i in devices){
  7. console.log(i+': '+JSON.stringify(devices[i]));
  8. }
  9. });
  10. }

openBluetoothAdapter

初始化蓝牙模块

  1. void plus.bluetooth.openBluetoothAdapter(options);

说明:

初始化成功后触发options参数中的success回调,失败触发options参数中的fail回调。

参数:

options参数为json类型,包含以下属性:

返回值:

void: 无

示例:

  1. // 打开蓝牙模块
  2. function openBluetoothAdapter(){
  3. plus.bluetooth.openBluetoothAdapter({
  4. success:function(e){
  5. console.log('open success: '+JSON.stringify(e));
  6. },
  7. fail:function(e){
  8. console.log('open failed: '+JSON.stringify(e));
  9. }
  10. });
  11. }

startBluetoothDevicesDiscovery

开始搜索附近的蓝牙设备

  1. void plus.bluetooth.startBluetoothDevicesDiscovery(options);

参数:

options参数为json类型,包含以下属性:

  • services: (Array[String])可选 要获取设备的uuid列表
  • allowDuplicatesKey: (Boolean)可选 是否允许重复上报同一设备如果允许重复上报,则onBlueToothDeviceFound方法会多次上报同一设备,但是RSSI值会有不同。
  • interval: (Number)可选 上报设备的间隔0表示找到新设备立即上报,其他数值根据传入的间隔上报。
  • success: (BluetoothSuccessCallback)可选 开始搜索成功回调函数搜索到设备后通过onBluetoothDeviceFound设置的回调返回设备信息,此回调只是表明开始搜索。
  • fail: ()可选 开始搜索失败回调函数
  • complete: (BluetoothCompleteCallback)可选 开始搜索操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

stopBluetoothDevicesDiscovery

停止搜寻附近的蓝牙外围设备

  1. void plus.bluetooth.stopBluetoothDevicesDiscovery(options);

说明:

若已经找到需要的蓝牙设备并不需要继续搜索时,应该调用该接口停止蓝牙搜索。停止成功后触发options参数中的success回调,失败触发options参数中的fail回调。

参数:

options参数为json类型,包含以下属性:

返回值:

void: 无

示例:

  1. // 结束搜索蓝牙
  2. function stopBluetoothDiscovery(){
  3. plus.bluetooth.stopBluetoothDevicesDiscovery({
  4. success:function(e){
  5. console.log('stop discovery success: '+JSON.stringify(e));
  6. plus.bluetooth.closeBluetoothAdapter({
  7. success:function(e){
  8. console.log('close success: '+JSON.stringify(e));
  9. },
  10. fail:function(e){
  11. console.log('close failed: '+JSON.stringify(e));
  12. }
  13. });
  14. },
  15. fail:function(e){
  16. console.log('stop discovery failed: '+JSON.stringify(e));
  17. }
  18. });
  19. }

closeBLEConnection

断开与低功耗蓝牙设备的连接

  1. void plus.bluetooth.closeBLEConnection(options);

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • success: (BluetoothSuccessCallback)可选 断开连接成功回调函数
  • fail: ()可选 断开连接失败回调函数
  • complete: (BluetoothCompleteCallback)可选 断开连接操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. function closeConnection(){
  2. plus.bluetooth.closeBLEConnection({
  3. deviceId:deviceId,
  4. success:function(e){
  5. console.log('close success: '+JSON.stringify(e));
  6. },
  7. fail:function(e){
  8. console.log('close failed: '+JSON.stringify(e));
  9. }
  10. });
  11. }

createBLEConnection

连接低功耗蓝牙设备

  1. void plus.bluetooth.createBLEConnection(options);

说明:

若之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的deviceId尝试连接该设备,无需进行搜索操作。

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • timeout: (Number)可选 超时时间单位为ms(毫秒),不设置此属性表示不会超时。
  • success: (BluetoothSuccessCallback)可选 连接成功回调函数
  • fail: ()可选 连接失败回调函数
  • complete: (BluetoothCompleteCallback)可选 连接操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 连接蓝牙设备
  4. function createConnection(){
  5. plus.bluetooth.createBLEConnection({
  6. deviceId:deviceId,
  7. success:function(e){
  8. console.log('create connection success: '+JSON.stringify(e));
  9. },
  10. fail:function(e){
  11. console.log('create connection failed: '+JSON.stringify(e));
  12. }
  13. });
  14. }

getBLEDeviceCharacteristics

获取蓝牙设备指定服务中所有特征值(characteristic)

  1. void plus.bluetooth.getBLEDeviceCharacteristics(options);

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • serviceId: (String)必选 蓝牙服务 uuid可通过getBLEDeviceServices获取。
  • success: (BluetoothSuccessCallback)可选 获取特征值成功回调函数回调函数参数event对象包括以下属性:characteristics - Array>Bluetoothcharacteristic<类型,蓝牙设备服务的特征值列表。
  • fail: ()可选 获取特征值失败回调函数
  • complete: (BluetoothCompleteCallback)可选 获取特征值操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 蓝牙服务uuid,可通过getBLEDeviceServices方法获取
  4. var serviceId = '';
  5. // 获取蓝牙设备指定服务中所有特征值
  6. function getCharacteristics(){
  7. plus.bluetooth.getBLEDeviceCharacteristics({
  8. deviceId:deviceId,
  9. serviceId:serviceId,
  10. success:function(e){
  11. var characteristics = e.characteristics;
  12. console.log('get characteristics success: '+characteristics.length);
  13. for(var i in characteristics){
  14. console.log(i+': '+JSON.stringify(characteristics[i]));
  15. }
  16. },
  17. fail:function(e){
  18. console.log('get characteristics failed: '+JSON.stringify(e));
  19. }
  20. });
  21. }

getBLEDeviceServices

获取蓝牙设备的所有服务(service)

  1. void plus.bluetooth.getBLEDeviceServices(options);

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • success: (BluetoothSuccessCallback)可选 获取服务成功回调函数回调函数参数event对象包括以下属性:services - Array>BluetoothService<类型,蓝牙设备服务的特征值列表。
  • fail: ()可选 获取服务失败回调函数
  • complete: (BluetoothCompleteCallback)可选 获取服务操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 获取蓝牙设备的所有服务
  4. function getServices(){
  5. plus.bluetooth.getBLEDeviceServices({
  6. deviceId:deviceId,
  7. success:function(e){
  8. var services = e.services;
  9. console.log('get services success: '+services.length);
  10. for(var i in services){
  11. console.log(i+': '+JSON.stringify(services[i]));
  12. }
  13. },
  14. fail:function(e){
  15. console.log('get services failed: '+JSON.stringify(e));
  16. }
  17. });
  18. }

notifyBLECharacteristicValueChange

启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值

  1. void plus.bluetooth.notifyBLECharacteristicValueChange(options);

说明:

蓝牙设备服务的特征值必须支持notify或indicate才可以成功调用。另外,必须先启用notifyBLECharacteristicValueChange才能监听到设备characteristicValueChange事件,即特征值发生变化时通过onBLECharacteristicValueChange注册的事件回调。

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • serviceId: (String)必选 蓝牙服务的uuid
  • characteristicId: (String)必选 蓝牙特征值的uuid
  • state: (Boolean)必选 是否启用 notify
  • success: (BluetoothSuccessCallback)可选 订阅特征值成功回调函数
  • fail: ()可选 订阅特征值失败回调函数
  • complete: (BluetoothCompleteCallback)可选 订阅特征值操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 蓝牙服务id,可通过getBLEDeviceServices方法获取
  4. var serviceId = '';
  5. // 蓝牙特征值id,可通过getBLEDeviceCharacteristics方法获取
  6. var characteristicId = '';
  7. // 启用低功耗蓝牙设备特征值变化时的notify功能
  8. function startCharacteristicsNotify(){
  9. // 监听低功耗蓝牙设备的特征值变化
  10. plus.bluetooth.onBLECharacteristicValueChange(function(e){
  11. console.log('onBLECharacteristicValueChange: '+JSON.stringify(e));
  12. var value = buffer2hex(e.value);
  13. console.log('value(hex) = '+value);
  14. if(characteristicId == e.characteristicId){
  15. // 更新到页面显示
  16. }
  17. });
  18. // 启用notify功能
  19. plus.bluetooth.notifyBLECharacteristicValueChange({
  20. deviceId:deviceId,
  21. serviceId:serviceId,
  22. characteristicId:characteristicId,
  23. success:function(e){
  24. var characteristics = e.characteristics;
  25. console.log('get characteristics success: '+characteristics.length);
  26. for(var i in characteristics){
  27. console.log(i+': '+JSON.stringify(characteristics[i]));
  28. }
  29. },
  30. fail:function(e){
  31. console.log('get characteristics failed: '+JSON.stringify(e));
  32. }
  33. });
  34. }

onBLECharacteristicValueChange

监听低功耗蓝牙设备的特征值变化事件

  1. void plus.bluetooth.onBLECharacteristicValueChange(callback);

参数:

  • callback: (BLECharacteristicValueChangeCallback)必选 特征值变化回调函数回调函数参数event对象包括以下属性:deviceId - String类型,蓝牙设备id;serviceId - String类型,蓝牙服务的uuid;characteristicId - String类型,蓝牙特征值的uuid;value - ArrayBuffer类型,特征值的最新值。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 蓝牙服务id,可通过getBLEDeviceServices方法获取
  4. var serviceId = '';
  5. // 蓝牙特征值id,可通过getBLEDeviceCharacteristics方法获取
  6. var characteristicId = '';
  7. // 启用低功耗蓝牙设备特征值变化时的notify功能
  8. function startCharacteristicsNotify(){
  9. // 监听低功耗蓝牙设备的特征值变化
  10. plus.bluetooth.onBLECharacteristicValueChange(function(e){
  11. console.log('onBLECharacteristicValueChange: '+JSON.stringify(e));
  12. var value = buffer2hex(e.value);
  13. console.log('value(hex) = '+value);
  14. if(characteristicId == e.characteristicId){
  15. // 更新到页面显示
  16. alert('特征值变化: '+value);
  17. }
  18. });
  19. // 启用notify功能
  20. deviceId:deviceId,
  21. serviceId:serviceId,
  22. characteristicId:characteristicId,
  23. success:function(e){
  24. var characteristics = e.characteristics;
  25. console.log('get characteristics success: '+characteristics.length);
  26. for(var i in characteristics){
  27. console.log(i+': '+JSON.stringify(characteristics[i]));
  28. }
  29. },
  30. fail:function(e){
  31. console.log('get characteristics failed: '+JSON.stringify(e));
  32. }
  33. });
  34. }

onBLEConnectionStateChange

监听低功耗蓝牙设备连接状态变化事件

  1. void plus.bluetooth.onBLEConnectionStateChange(callback);

说明:

包括开发者主动连接或断开连接,设备丢失,连接异常断开等。

参数:

  • callback: (BLEConnectionStateChangeCallback)必选 连接状态变化回调函数回调函数参数event对象包括以下属性:deviceId - String类型,蓝牙设备id;connected - Boolean类型,是否处于已连接状态。

返回值:

void: 无

示例:

readBLECharacteristicValue

读取低功耗蓝牙设备指定特征值的二进制数据值

  1. void plus.bluetooth.readBLECharacteristicValue(options);

指定的特征值需支持read操作才可以成功调用。并行调用多次可能导致读取失败,读取的数据需要在onBLECharacteristicValueChange方法注册的回调中获取。

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • serviceId: (String)必选 蓝牙服务的uuid
  • characteristicId: (String)必选 蓝牙特征值的uuid
  • success: ()可选 读取特征值的二进制数据成功回调函数
  • fail: (BluetoothFailCallback)可选 读取特征值的二进制数据失败回调函数
  • complete: ()可选 读取特征值的二进制数据操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 蓝牙服务id,可通过getBLEDeviceServices方法获取
  4. var serviceId = '';
  5. // 蓝牙特征值id,可通过getBLEDeviceCharacteristics方法获取
  6. var characteristicId = '';
  7. // 读取低功耗蓝牙设备的特征值
  8. function readCharacteristics(){
  9. plus.bluetooth.readBLECharacteristicValue({
  10. deviceId:deviceId,
  11. serviceId:serviceId,
  12. characteristicId:characteristicId,
  13. success:function(e){
  14. console.log('read characteristics success: '+JSON.stringify(e));
  15. },
  16. fail:function(e){
  17. console.log('read characteristics failed: '+JSON.stringify(e));
  18. }
  19. });
  20. }

uni-app使用plus注意事项

writeBLECharacteristicValue

向低功耗蓝牙设备指定特征值写入二进制数据

  1. void plus.bluetooth.writeBLECharacteristicValue(options);

说明:

指定的特征值需支持write操作才可以成功调用。并行调用多次可能导致读取失败,系统可能会限制单次传输的数据大小,超过最大字节数后可能会发生写入错误,建议每次写入不超过20字节。

参数:

options参数为json类型,包含以下属性:

  • deviceId: (String)必选 蓝牙设备的id
  • serviceId: (String)必选 蓝牙服务的uuid
  • characteristicId: (String)必选 蓝牙特征值的uuid
  • value: (ArrayBuffer)必选 要写入的数据写入到蓝牙设备指定特征值中的二进制值。
  • success: ()可选 读取特征值的二进制数据成功回调函数
  • fail: (BluetoothFailCallback)可选 读取特征值的二进制数据失败回调函数
  • complete: ()可选 读取特征值的二进制数据操作完成回调函数调用成功或失败都会触发此回调。

返回值:

void: 无

示例:

  1. // 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
  2. var deviceId = '';
  3. // 蓝牙服务id,可通过getBLEDeviceServices方法获取
  4. var serviceId = '';
  5. // 蓝牙特征值id,可通过getBLEDeviceCharacteristics方法获取
  6. var characteristicId = '';
  7. // 要写入的数据
  8. var value = new ArrayBuffer(8);
  9. var iv = new Int32Array(value);
  10. iv[0] = 120, iv[2]=100;
  11. // 写入低功耗蓝牙设备的特征值
  12. function writeCharacteristics(){
  13. plus.bluetooth.writeBLECharacteristicValue({
  14. deviceId:deviceId,
  15. serviceId:serviceId,
  16. characteristicId:characteristicId,
  17. value:value,
  18. success:function(e){
  19. console.log('write characteristics success: '+JSON.stringify(e));
  20. },
  21. fail:function(e){
  22. console.log('write characteristics failed: '+JSON.stringify(e));
  23. }
  24. });
  25. }

uni-app使用plus注意事项

BluetoothDeviceInfo

蓝牙设备信息

  1. interface BluetoothDeviceInfo {
  2. readonly attribute String name;
  3. readonly attribute String deviceId;
  4. readonly attribute String RSSI;
  5. readonly attribute ArrayBuffer advertisData;
  6. readonly attribute Array<String> advertisServiceUUIDs;
  7. readonly attribute String localName;
  8. readonly attribute JSON serviceData;
  9. }

属性:

  • name: (String类型)蓝牙设备名称某些设备可能没有此字段值。

  • deviceId: (String类型)蓝牙设备的id

  • RSSI: (String类型)蓝牙设备的信号强度
  • advertisData: (ArrayBuffer类型)蓝牙设备的广播数据段中的ManufacturerData数据段
  • advertisServiceUUIDs: (Array[String]类型)蓝牙设备的广播数据段中的ServiceUUIDs数据段
  • localName: (String类型)蓝牙设备的广播数据段中的LocalName数据段
  • serviceData: (JSON类型)蓝牙设备的广播数据段中的ServiceData数据段

BluetoothService

蓝牙设备服务信息

  1. interface BluetoothService {
  2. readonly attribute String uuid;
  3. readonly attribute Boolean isPrimary;
  4. }

属性:

  • uuid: (String类型)蓝牙设备服务的uuid
  • isPrimary: (Boolean类型)是否为设备的主服务

Bluetoothcharacteristic

蓝牙设备特征值

  1. interface Bluetoothcharacteristic {
  2. readonly attribute String uuid;
  3. readonly attribute BluetoothcharacteristicProperties properties;
  4. }

属性:

  • uuid: (String类型)蓝牙设备特征值的uuid
  • properties: (类型)设备特征值支持的操作类型

BluetoothcharacteristicProperties

蓝牙设备特征值支持的操作类型

  1. interface BluetoothcharacteristicProperties {
  2. readonly attribute Boolean read;
  3. readonly attribute Boolean write;
  4. readonly attribute Boolean notify;
  5. readonly attribute Boolean indicate;
  6. }

属性:

  • read: (Boolean类型)特征值是否支持read操作
  • write: (Boolean类型)特征值是否支持write操作
  • notify: (Boolean类型)特征值是否支持notify操作
  • indicate: (Boolean类型)特征值是否支持indicate操作

BluetoothSuccessCallback

成功回调函数

  1. void onSuccess(JSON event){
  2. }

说明:

不同接口触发的成功回调参数event包含的属性存在差异,具体参考对应的接口描述说明。

参数:

  • event: (JSON)必选 回调参数回调参数包含的属性由调用接口决定,具体参考对应的接口描述说明。

返回值:

void: 无

BluetoothFailCallback

失败回调函数

  1. function void onFail(Exception error){
  2. // Handle error
  3. var code = error.code; // 错误编码
  4. var message = error.message; // 错误描述信息
  5. }

参数:

  • error: (Exception)必选 回调参数,错误信息可通过error.code(Number类型)获取错误编码;可通过error.message(String类型)获取错误描述信息。

返回值:

void: 无

BluetoothCompleteCallback

操作完成回调函数

  1. function void onComplete(JSON event){
  2. }

说明:

调用成功或失败都会触发此回调。

参数:

  • event: (JSON)可选 回调参数调用成功时回调参数与BluetoothSuccessCallback一致,调用失败时回调参数与BluetoothFailCallback一致。

返回值:

void: 无

BluetoothAdapterStateChangeCallback

蓝牙适配器状态变化事件回调函数

  1. function void onStateChange(JSON event){
  2. // event.available
  3. // event.discovering
  4. }

说明:

蓝牙适配器状态发生变化时触发此回调。

参数:

  • event: (JSON)可选 返回参数回调函数参数event对象包括以下属性:available - Boolean,蓝牙适配器是否可用;discovering - Boolean,蓝牙适配器是否处于搜索状态。

返回值:

void: 无

BluetoothDeviceFoundCallback

蓝牙适配器搜索到新设备事件回调函数

  1. function void onDeviceFound(JSON event){
  2. // event.devices
  3. }

说明:

搜索到蓝牙设备时触发此回调。

参数:

  • event: (JSON)可选 返回参数回调函数参数event对象包括以下属性:devices - Array,设备列表信息。

返回值:

void: 无

BLEConnectionStateChangeCallback

低功耗蓝牙设备连接状态变化事件回调函数

  1. function void onStateChange(JSON event){
  2. // event.deviceId
  3. // event.connected
  4. }

说明:

蓝牙设备连接状态发生变化时触发此回调。

参数:

  • event: (JSON)可选 返回参数回调函数参数event对象包括以下属性:deviceId - String类型,蓝牙设备id;connected - Boolean类型,是否处于已连接状态。

返回值:

void: 无

BLECharacteristicValueChangeCallback

低功耗蓝牙设备的特征值变化事件回调函数

  1. function void onValueChange(JSON event){
  2. // event.deviceId
  3. // event.serviceId
  4. // event.characteristicId
  5. // event.value
  6. }

说明:

蓝牙设备对应的特征值发生变化时触发此回调。

参数:

  • event: (JSON)可选 返回参数回调函数参数event对象包括以下属性:deviceId - String类型,蓝牙设备id;serviceId - String类型,蓝牙服务的uuid;characteristicId - String类型,蓝牙特征值的uuid;value - ArrayBuffer类型,特征值的最新值。

返回值:

void: 无