鸿蒙语言基础类库:【@system.bluetooth (蓝牙)】
创始人
2024-12-29 02:03:26
0次

蓝牙

说明:
开发前请熟悉鸿蒙开发指导文档:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

  • 从API Version 7 开始,该接口不再维护,推荐使用新接口[@ohos.bluetooth]。
  • 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import bluetooth from '@system.bluetooth'; 

bluetooth.startBLEScan(OBJECT)

开始搜寻附近的低功耗蓝牙外围设备。此操作比较耗费系统资源,请在搜索并连接到设备后调用[bluetooth.stopBLEScan]方法停止搜索。

需要权限: ohos.permission.DISCOVER_BLUETOOTH、ohos.permission.LOCATION

系统能力: SystemCapability.Communication.Bluetooth.Lite

参数:表1 StartBLEScanOptions

参数名类型必填说明
intervalnumber否上报设备的间隔,单位毫秒,默认值为0。0表示找到新设备立即上报,其他数值根据传入的间隔上报。
successFunction否接口调用成功的回调函数。
failFunction否接口调用失败的回调函数。
completeFunction否接口调用结束的回调函数。

示例:

bluetooth.startBLEScan({   interval:0,   success() {     console.log('call bluetooth.startBLEScan success.');   },   fail(code, data) {     console.log('call bluetooth.startBLEScan failed, code: ${code}, data: ${data}.');   },   complete() {     console.log('call bluetooth.startBLEScan complete.');   } }); 

bluetooth.stopBLEScan(OBJECT)

停止搜寻附近的低功耗蓝牙外围设备。与[bluetooth.startBLEScan(OBJECT)]接口配套使用。

需要权限: ohos.permission.DISCOVER_BLUETOOTH、ohos.permission.LOCATION

系统能力: SystemCapability.Communication.Bluetooth.Lite

参数:表2 StopBLEScanOptions

参数名类型必填说明
successFunction否接口调用成功的回调函数。
failFunction否接口调用失败的回调函数。
completeFunction否接口调用结束的回调函数。

示例:

bluetooth.stopBLEScan({   success() {     console.log('call bluetooth.stopBLEScan success.');   },   fail(data, code) {     console.log('call bluethooth.stopBLEScan fail, code: ${code}, data: ${data}.');   },   complete() {     console.log('call bluethooth.stopBLEScan complete.');   } }); 

bluetooth.subscribeBLEFound(OBJECT)

订阅寻找到新设备。再次调用时,会覆盖前一次调用效果,即仅最后一次调用生效。

需要权限: ohos.permission.DISCOVER_BLUETOOTH、ohos.permission.LOCATION

系统能力: SystemCapability.Communication.Bluetooth.Lite

参数:表3 SubscribeBLEFoundOptions

参数类型必填说明
successFunction是寻找到新设备上报时调用的回调函数。
failFunction否接口调用失败的回调函数。

表4 success返回值:

参数名类型说明
devicesArray新搜索到的设备列表。

表5 BluethoothDevice

在这里插入图片描述

参数名类型说明
addrTypestring设备地址类型,可能值有: - public: 公共地址 - random: 随机地址
addrstring设备MAC地址。
rssinumber设备蓝牙的信号强弱指标。
txpowerstring广播数据中的txpower字段。
datahex string广播数据(包含广播数据和扫描响应数据),十六进制字符串。

示例:

bluetooth.subscribeBLEFound({   success(data) {     console.log('call bluetooth.subscribeBLEFound success, data: ${data}.');   },   fail(data, code) {     console.log('call bluetooth.startBLEScan failed, data: ${data}, code: ${code}.');   } }); 

bluetooth.unsubscribeBLEFound()

解除订阅寻找到新设备。

需要权限: ohos.permission.DISCOVER_BLUETOOTH、ohos.permission.LOCATION

系统能力: SystemCapability.Communication.Bluetooth.Lite

示例:

bluetooth.unsubscribeBLEFound(); 

常见错误码

错误码说明
1100是否处于已连接状态。
1101当前蓝牙适配器不可用。
1102没有找到指定设备。
1103连接失败。
1104没有找到指定服务。
1105没有找到指定特征值。
1106当前连接已断开。
1107当前特征值不支持此操作。
1108其余所有系统上报的异常。
1109系统版本不支持 BLE。

相关内容

热门资讯

开挂插件!超级三加一辅助透视插... 开挂插件!超级三加一辅助透视插件,闲来广东麻将真的有外挂,切实真是有挂1、玩家可以在超级三加一透视最...
开挂工具!欢乐龙城3辅助透视插... 开挂工具!欢乐龙城3辅助透视插件,天天卡五星确实有外挂,果然竟然有挂开挂工具!欢乐龙城3辅助透视插件...
开挂攻略!酷玩部落辅助透视插件... 开挂攻略!酷玩部落辅助透视插件,科乐松原麻将真的是有外挂,果然有挂透视1、这是跨平台的酷玩部落轻量版...
开挂神器!天天乐比赛辅助透视插... 开挂神器!天天乐比赛辅助透视插件,众乐乐是有外挂,真是有挂秘笈1、不需要AI权限,帮助你快速的进行天...
开挂方法!启航竞技辅助透视插件... 开挂方法!启航竞技辅助透视插件,掌上约牌是真的有外挂,确实有挂秘籍掌上约牌是不是有人用挂微扑克wpk...
开挂软件!皇豪众娱辅助透视插件... 开挂软件!皇豪众娱辅助透视插件,莲花麻将确实有外挂,总是证实有挂1、每一步都需要思考,不同水平的挑战...
开挂技巧!886辅助透视插件,... 开挂技巧!886辅助透视插件,西域麻将是真的有外挂,果然有挂分享1、进入游戏-大厅左侧-新手福利-激...
开挂技巧!新九五辅助透视插件,... 开挂技巧!新九五辅助透视插件,皮皮云南麻将存在有外挂,好像有挂技巧1、下载好新九五正确养号方法之后点...
开挂方法!玄龙二厅辅助透视插件... 开挂方法!玄龙二厅辅助透视插件,中至汾阳麻将是真的有外挂,其实有挂方法1、许多玩家不知道玄龙二厅辅助...
开挂器!莆仙薇乐辅助透视插件,... 开挂器!莆仙薇乐辅助透视插件,山水广西麻将真的是有外挂,都是有挂攻略所有人都在同一条线上,像星星一样...