uniapp蓝牙密码
关于UniApp蓝牙密码的使用
在UniApp中开发蓝牙功能时,涉及密码或配对码的场景通常出现在蓝牙设备连接阶段。以下是关键信息整理:
蓝牙配对流程
UniApp通过uni.connectBluetoothDevice发起连接,部分设备会触发系统级配对弹窗,密码由设备厂商预设(如1234或0000)。需查阅设备说明书获取默认密码。
代码示例
监听配对请求时,需在uni.onBluetoothDeviceFound回调中处理:
uni.onBluetoothDeviceFound(devices => {
if (devices[0].name.includes('YourDevice')) {
uni.connectBluetoothDevice({
deviceId: devices[0].deviceId,
success: () => console.log('连接触发系统配对')
})
}
})
常见密码类型
- 固定密码:多数简单设备采用0000、1234、1111等
- 动态密码:部分医疗/金融设备每次连接生成临时密码
- 无密码:BLE设备通常无需配对,直接通过服务UUID通信
安全建议

- 在
manifest.json声明蓝牙权限:"permission": { "scope.bluetooth": {} } - 对敏感操作启用加密通信,使用
uni.writeBLECharacteristicValue时设置encrypt参数
调试技巧
通过uni.getBluetoothDevices获取设备信息,检查advertisData字段中是否包含密码提示。安卓设备可通过logcat查看配对过程日志。






