打开蓝牙设备
<template><view><button type="primary" @click="open">打开蓝牙设备</button></view></template><script>export default {methods: {open () {uni.openBluetoothAdapter({success: res => {console.log(JSON.stringify(res)) // {"errMsg":"openBluetoothAdapter:ok"}},fail: err => {switch (err.errCode) {case 10001: // "openBluetoothAdapter:fail not available"uni.showToast({title: '请打开蓝牙设备后重试',mask: false,duration: 1500,icon: 'none'});break}console.log(JSON.stringify(err)) // {"errMsg":"openBluetoothAdapter:fail not available","errCode":10001}}})}}}</script>
:::warning
 注意:
其他蓝牙相关 API 必须在 uni.openBluetoothAdapter 调用之后使用。否则 API 会返回错误(errCode=10000)。
:::
获取本机蓝牙适配器状态
uni.getBluetoothAdapterState({success: state => {console.log(JSON.stringify(state)) // {"discovering":false,"available":true,"errMsg":"getBluetoothAdapterState:ok"}},fail: err => {console.log(JSON.stringify(err))}})
错误码查询
| 错误码 | 错误信息 | 说明 | 
|---|---|---|
| 0 | ok | 正常 | 
| 10000 | not init | 未初始化蓝牙适配器 | 
| 10001 | not available | 当前蓝牙适配器不可用 | 
| 10002 | no device | 没有找到指定设备 | 
| 10003 | connection fail | 连接失败 | 
| 10004 | no service | 没有找到指定服务 | 
| 10005 | no characteristic | 没有找到指定特征值 | 
| 10006 | no connection | 当前连接已断开 | 
| 10007 | property not support | 当前特征值不支持此操作 | 
| 10008 | system error | 其余所有系统上报的异常 | 
| 10009 | system not support | Android 系统特有,系统版本低于 4.3 不支持 BLE | 
