鸿蒙OS开发文档 鸿蒙OS 传统蓝牙远端设备操作

2024-02-25 开发教程 鸿蒙OS开发文档 匿名 2

场景介绍

传统蓝牙远端管理操作主要是针对远端蓝牙设备的基本操作,包括获取远端蓝牙设备地址、类型、名称和配对状态,以及向远端设备发起配对。

接口说明

接口名功能描述
getDeviceAddr()获取远端蓝牙设备地址。
getDeviceClass()获取远端蓝牙设备类型。
getDeviceName()获取远端蓝牙设备名称。
getPairState()获取远端设备配对状态。
startPair()向远端设备发起配对。

开发步骤

  1. 调用 BluetoothHost 的 getDefaultHost(Context context)接口,获取 BluetoothHost 实例,管理本机蓝牙操作。
  1. 调用 enableBt() 接口,打开蓝牙。
  2. 调用 startBtDiscovery(),扫描设备。
  3. 调用 startPair(),发起配对。
// 获取蓝牙本机管理对象
BluetoothHost bluetoothHost = BluetoothHost.getDefaultHost(context);
// 调用打开接口
bluetoothHost.enableBt();
// 调用扫描接口
bluetoothHost.startBtDiscovery();
//设置界面会显示出扫描结果列表,点击蓝牙设备去配对
BluetoothRemoteDevice device = bluetoothHost.getRemoteDev(TEST_ADDRESS);
device.startPair();