更新记录
1.0.0(2024-07-08)
经典蓝牙扫描连接收发数据
平台兼容性
HbuilderX/cli最低兼容版本 |
3.6.8 |
uni-app
app-vue |
app-nvue |
app-android |
app-ios |
? |
? |
? |
? |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
? |
? |
? |
? |
? |
? |
? |
? |
? |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
? |
? |
? |
? |
? |
? |
? |
? |
? |
uni-app x
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
? |
? |
? |
? |
? |
? |
? |
? |
? |
android经典蓝牙
集成步骤
import {
UTSBluetoothBT
} from "@/uni_modules/wrs-uts-bluetoothbt"
UTSBluetoothBT.registerReceiver((resp) => {
let action = resp.action
switch (action) {
// 蓝牙状态变化
case "android.bluetooth.adapter.action.STATE_CHANGED":
let intent = resp.intent
let extras = intent.extras
let state = extras["android.bluetooth.adapter.extra.STATE"]
this.showMsg("蓝牙状态变化 state:" + state)
break;
// 开始扫描蓝牙
case "android.bluetooth.adapter.action.DISCOVERY_STARTED":
break;
// 蓝牙搜索结束
case "android.bluetooth.adapter.action.DISCOVERY_FINISHED":
break;
// 搜索到蓝牙设备
case "android.bluetooth.device.action.FOUND": {
let device = resp.device
let name = device.name
if (name) {
let length = this.bluetoothArray.length
let isExist = false
for (let i = 0; i < length; i++) {
let tempName = this.bluetoothArray[i].name
if (name == tempName) {
isExist = true
}
}
if (!isExist) {
console.log(JSON.stringify(resp))
this.bluetoothArray.push(device)
}
}
//this.showMsg(JSON.stringify(device))
}
break;
//在系统弹出配对框之前(确认/输入配对码)
case "android.bluetooth.device.action.PAIRING_REQUEST":
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.device.action.ACL_CONNECTED":
break;
//最底层连接断开
case "android.bluetooth.device.action.ACL_DISCONNECTED":
break;
//BluetoothAdapter连接状态
case "android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED":
break;
// BluetoothHeadset连接状态
case "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED":
break;
//BluetoothA2dp连接状态
case "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED":
break;
default:
break;
}
})
let isEnabled = UTSBluetoothBT.isEnabled()
if (isEnabled) {
}
UTSBluetoothBT.enable()
- 扫描蓝牙,扫描结果在回调里registerReceiver
UTSBluetoothBT.startDiscovery()
UTSBluetoothBT.cancelDiscovery()
let params = {}
params.address = device.address
params.uuid = "00001101-0000-1000-8000-00805F9B34FB" // 这个uuid一般都是固定
// 明文连接
let callback = (resp) => {
let opt = resp.opt
switch (opt) {
case "onConnected": {
this.showMsg("连接成功")
}
break;
case "onReceiveData":
let data = resp.data;
this.showMsg("收到数据:" + JSON.stringify(resp))
// let hex = HexUtils.uint8ArrayToHex(data);
// // let hex = "4765745f4944e58da0e4b889"
// let decodeStr = ASCIIUtils.decodeUtf8(hex);
// this.showMsg("收到数据:" + decodeStr)
break;
case "onConnectFail":
this.showMsg("连接失败:" + JSON.stringify(resp))
break;
case "onDisConnected":
this.isRead = false
this.showMsg("断开连接")
break;
default:
break;
}
// this.showMsg(JSON.stringify(resp))
}
UTSBluetoothBT.createInsecureRfcommSocketToServiceRecord(params, callback)
// 加密连接,弹窗显示PIN配对码
// UTSBluetoothBT.createRfcommSocketToServiceRecord(params,callback)
let address = this.curDevice.address
UTSBluetoothBT.close({
address: address // 蓝牙mac地址
})
let params = {
address: address, // 蓝牙mac地址
value: [0x01, 0xff]
}
UTSBluetoothBT.writeByte(params)
UTSBluetoothBT.writeUTF({
address: address, // 蓝牙mac地址
value: "我是中国人"
})
如果觉得可以就点个👍吧,欢迎粉丝收藏,土豪打赏,您的关注就是我们创作的动力!