uniapp在鸿蒙系统上使用蓝牙
·
使用流程
下载插件地址
https://ext.dcloud.net.cn/plugin?name=km-bluetooth
配置权限
在工程目录下面的harmony-configs/entry/src/main/module.json5
配置权限
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
{
"name" : "ohos.permission.ACCESS_BLUETOOTH",
"reason": "$string:module_desc",
"usedScene": {
"abilities": [
"FormAbility"
],
"when":"inuse"
}
},
]
使用前声明
import {openBluetoothAdapter,
startBluetoothDevicesDiscovery,
writeBLECharacteristicObject,
sendb64,
onBLEConnectionStateChange,
createBLEConnection,
closeBLEConnection,
stopBluetoothDevicesDiscovery} from "@/uni_modules/km-bluetooth"
初始化蓝牙并打开权限
openBluetoothAdapter({name:"123"},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
扫描蓝牙
startBluetoothDevicesDiscovery({},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
连接蓝牙
//设备deviceI的,在扫描到的设备中获取
let deviceId = "A4:5C:76:08:D8:3D";
createBLEConnection({deviceId:deviceId},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
写入base64模式的模版
let base64 = "";
sendb64({b64:base64},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
查询打印机状态
onBLEConnectionStateChange({name:"123"},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
断开打印机连接
closeBLEConnection({name:"123"},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
停止扫描
stopBluetoothDevicesDiscovery({name:"123"},(res : any) => {
console.log('success: ', JSON.stringify(res));
});
有疑问+V:Xzw421771880
更多推荐


所有评论(0)