harmony 鸿蒙@system.bluetooth (蓝牙)

2022-08-09 浏览 (1305)

@system.bluetooth (蓝牙)

说明:

  • 从API Version 7 开始,该接口不再维护,推荐使用@ohos.bluetooth.ble等相关profile接口。

  • 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import bluetooth from '@system.bluetooth';

bluetooth.startBLEScan(OBJECT)

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

系统能力: 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)接口配套使用。

系统能力: 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)

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

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

参数: 表3 SubscribeBLEFoundOptions

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

表4 success返回值:

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

表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, code:' + code + ', data:' + data);
  }
});

bluetooth.unsubscribeBLEFound()

解除订阅寻找到新设备。

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

示例:

bluetooth.unsubscribeBLEFound();

常见错误码

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

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/1d097c32a9ed41e1991c147a1e704586