更新记录
1.0.5(2024-07-22)
- 优化扫描逻辑
1.0.4(2024-07-03)
- android权限配置文件AndroidManifest.xml迁移到项目根目录
1.0.3(2024-05-12)
- 优化Android蓝牙权限
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:4.4,iOS:不支持,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
android蓝牙扫描
集成步骤
- 拷贝demo示例里的AndroidManifest.xml到项目根目录
- 集成插件步骤请参考https://www.cnblogs.com/wenrisheng/p/18323027
API接口
引入插件变量:
import {
UTSBluetoothAdapter
} from "@/uni_modules/wrs-uts-bluetooth";
let isEnabled = bluetoothAdapter.isEnabled()
if(isEnabled) {
}
bluetoothAdapter.enable()
bluetoothAdapter.registerReceiver((resp) => {
let action = resp.action;
switch (action) {
case "android.bluetooth.device.action.FOUND": {
// 扫描到蓝牙
let device = resp.device;
console.log("扫描到蓝牙:" + JSON.stringify(resp))
// 用这个条件device.bluetoothClass.majorDeviceClass == 1536筛选过滤蓝牙
// if (device.bluetoothClass.majorDeviceClass == 1536) {
// this.bluetoothArray.push(device);
// }
this.bluetoothArray.push(device);
}
break;
case "android.bluetooth.device.action.BOND_STATE_CHANGED": {
// 蓝牙状态变更
let device = resp.device;
let bondState = device.bondState;
switch (bondState) {
case 11: {
// 正在配对......
}
break;
case 12: {
// 完成配对
}
break;
case 10: {
// 取消配对
}
break;
default:
break;
}
}
break;
case "android.bluetooth.adapter.action.DISCOVERY_FINISHED": {
// 蓝牙扫描完成
}
break;
default:
break;
}
});
bluetoothAdapter.startDiscovery();
bluetoothAdapter.cancelDiscovery();
let isDiscovering = bluetoothAdapter.isDiscovering()