前言
此文件就是介绍 OpenHarmony 中的蓝牙接口和如何自己开发一个简单的蓝牙应用程序。
OpenHarmony 蓝牙接口简介
getState
enum BluetoothState {
/** Indicates the local Bluetooth is off */
STATE_OFF = 0,
/** Indicates the local Bluetooth is turning on */
STATE_TURNING_ON = 1,
/** Indicates the local Bluetooth is on, and ready for use */
STATE_ON = 2,
/** Indicates the local Bluetooth is turning off */
STATE_TURNING_OFF = 3,
/** Indicates the local Bluetooth is turning LE mode on */
STATE_BLE_TURNING_ON = 4,
/** Indicates the local Bluetooth is in LE only mode */
STATE_BLE_ON = 5,
/** Indicates the local Bluetooth is turning off LE only mode */
STATE_BLE_TURNING_OFF = 6
setBluetoothScanMode
enum ScanMode {
/** Indicates the scan mode is none */
SCAN_MODE_NONE = 0,
/** Indicates the scan mode is connectable */
SCAN_MODE_CONNECTABLE = 1,
/** Indicates the scan mode is general discoverable */
SCAN_MODE_GENERAL_DISCOVERABLE = 2,
/** Indicates the scan mode is limited discoverable */
SCAN_MODE_LIMITED_DISCOVERABLE = 3,
/** Indicates the scan mode is connectable and general discoverable */
SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE = 4,
/** Indicates the scan mode is connectable and limited discoverable */
SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE = 5
}
OpenHarmony 典蓝牙配对流程
1.开启蓝牙
//开蓝牙
ListItem() {
TitleComponent({
title: "enableBluetooth", //显示功能的名称
bgColor: this.currentClick === 0 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
}); //点击后颜色变化
}
.padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
.onClick(() => { //点击事件
if (this.btEnable) { //判断蓝牙是否已经打开
this.message = '蓝牙已经使能';
return;
}
let ret = BluetoothModel.enableBluetooth(); //打开蓝牙
this.btEnable = ret; //如果启用了蓝牙,则返回true,否则返回false
AppStorage.SetOrCreate('bluetoothIsOn', this.btEnable); //存储蓝牙打开的结果,方便调用
AppStorage.SetOrCreate('currentClick', this.currentClick);
this.message = "蓝牙使能执行结果:" + ret; //输出结果
})
2.设置发现模式
ListItem() {
TitleComponent({
title: "setBluetoothScanMode", //显示功能的名称
bgColor: this.currentClick === 6 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
}); //点击后颜色变化
}
.padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
.onClick(() => { //点击事件
this.currentClick = 6;
if (this.btEnable) { //判断蓝牙是否已经打开
retObj = {mod: 0, duration: -1} //mode表示要设置的蓝牙扫描模式{@link ScanMode}。 //*@param duration指示主机可发现的持续时间(秒)。
setLocalNameRet = BluetoothModel.setBluetoothScanMode(mode, dur);
if (setLocalNameRet) {
AppStorage.SetOrCreate('setScanModeRet', setLocalNameRet);
retObj.mod = mode;
retObj.duration = dur;
} else { //如果设置了蓝牙扫描模式,返回true,否则返回false。
console.info("BluetoothModel.setBluetoothScanMode failed!")
onsole.info("BluetoothModel.setBluetoothScanMode success!", retObj)
this.message = "setBluetoothScanMode执行" + this.setLocalNameRet ? '成功' : '失败';
}
}else {
this.message = "蓝牙未使能";
}
})
3.注册 pinRequest
ListItem() {
TitleComponent({
title: "btPinRequired" //显示功能的名称
bgColor: this.currentClick === 18 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
}); //点击后颜色变化
}
.padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
.onClick(() => { //点击事件
if (!this.btEnable) { //判断蓝牙是否已经打开
this.message = "蓝牙未使能";
return;
}
if (this.isPinRequiredClick) { //判断监听是否开启,即为一个“开关”,并存储数据
bluetooth.off('pinRequired', () => {
})
this.message = 'on:pinRequired监听已关闭,再次点击将开启监听';
this.isPinRequiredClick = false;
AppStorage.SetOrCreate('on_pinRequired', this.btPinRequired);
return;
}
this.isPinRequiredClick = true;
AppStorage.SetOrCreate('on_pinRequired', this.btPinRequired); //type要侦听的配对请求事件的类型为:pinRequired
this.pinMessage = 'PIN: ';
bluetooth.on('pinRequired', (data) => { //callback回调用于侦听配对请求事件。
this.pinMessage = 'PIN: ';
this.pinMessage += data.pinCode;
})
this.message = 'on:pinRequired监听已启动,再次点击将关闭监听。';
})
4.配对设备
ListItem() {
TitleComponent({
title: "pairDevice", //显示功能的名称
bgColor: this.currentClick === 10 ? $r('app.color.font_color_007DFF') : $r('app.color.white_bg_color')
}); //点击后颜色变化
}
.padding({ top: $r('app.float.distance_2'), bottom: $r('app.float.distance_2') })
.onClick(() => { //点击事件
if (!this.btEnable) { //判断蓝牙是否已经打开
this.message = '蓝牙未使能';
return;
}
if (this.deviceId == '') { //deviceId为要配对的远程设备的地址
this.message = "请输入目标设备的MAC"; //若为空,请输入目标MAC
return;
} else {
this.pairDevice(this.deviceId); //如果配对过程已启动,则返回tru