微信小程序蓝牙授权完整流程

本文详细介绍了微信小程序中与权限和蓝牙功能相关的API,如authorize用于获取用户授权,openBluetoothAdapter用于初始化蓝牙模块,以及处理iOS和Android系统的差异化授权流程。还提供了Taro框架中的示例代码来实现蓝牙权限请求和蓝牙功能检查。
摘要由CSDN通过智能技术生成

1.用到的api

        1.1 authorize:

                提前向用户发起授权请求。调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据,但不会实际调用对应接口。如果用户之前已经同意授权,则不会出现弹窗,直接返回成功。更多用法详见 用户授权。 > 小程序插件可以使用 wx.authorizeForMiniProgram

        1.2 getSetting

                获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限

        1.3 openSetting

                调起客户端小程序设置界面,返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限

        1.4 getSystemInfoSync

                获取系统信息。由于历史原因,wx.getSystemInfo 是异步的调用格式,但是是同步返回,需要异步获取系统信息请使用 wx.getSystemInfoAsync

        1.5 openBluetoothAdapter

                初始化蓝牙模块。iOS 上开启主机/从机(外围设备)模式时需分别调用一次,并指定对应的 mode

  • 其他蓝牙相关 API 必须在 wx.openBluetoothAdapter 调用之后使用。否则 API 会返回错误(errCode=10000)。

  • 在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下,调用 wx.openBluetoothAdapter 会返回错误(errCode=10001),表示手机蓝牙功能不可用。此时小程序蓝牙模块已经初始化完成,可通过 wx.onBluetoothAdapterStateChange 监听手机蓝牙状态的改变,也可以调用蓝牙模块的所有API。

        1.6 openAppAuthorizeSetting

                跳转系统微信授权管理页

        1.7 openSystemBluetoothSetting

                跳转系统蓝牙设置页。仅支持安卓

        1.8 getBluetoothAdapterState       

                 获取本机蓝牙适配器状态

2.api对应效果图

authorize/openBluetoothAdapter

openSetting

        ⚠️注意:这里需要区分用户的微信应用蓝牙授权和系统蓝

根据openBluetoothAdapter失败返回的state区分ios系统下情况

state=3 =》 微信应用蓝牙授权

state=4 =》系统蓝牙

 3.思维导图

        ⚠️注意:完整的授权流程需要区分ios和android

4.代码 (这里以Taro为例)

const bringBluetoothRight = () => {
            return new Promise((resolve, reject) => {
                getSetting({
                    success: (res) => {
                        if (res.authSetting['scope.bluetooth']) {
                            // 判断微信蓝牙是否授权
                            resolve(wechatBluetoothAuthUtils());
                        } else {
                            Taro.authorize({
                                scope: 'scope.bluetooth',
                                success: () => {
                                    // 同意微信授权
                                    resolve(wechatBluetoothAuthUtils());
                                },
                                fail: () => {
                                    showModal({
                                        content: 'xxx想要开启蓝牙完成连接,请开启小程序蓝牙授权',
                                        showCancel: false,
                                        success: () => {
                                            openSetting({
                                                success: (res) => {
                                                    if (res.authSetting['scope.bluetooth']) {
                                                        // 同意微信授权
                                                        resolve(wechatBluetoothAuthUtils());
                                                    }
                                                },
                                            });
                                        },
                                    });
                                },
                            });
                        }
                    },
                });
            });
        };

        const wechatBluetoothAuthUtils = () => {
            const { bluetoothEnabled, platform } = getSystemInfoSync();
            // 设备为IOS时,微信蓝牙是否开启
            if (platform === 'ios') {
                return new Promise((resolve, reject) => {
                    // 初始化蓝牙模块(用openBluetoothAdapter 方法解决部分ios设备,授权蓝牙失败的问题)
                    Taro.openBluetoothAdapter({
                        success: () => {
                            // 开启蓝牙功能 =》 初始化拾果sdk
                            resolve(true);
                        },
                        fail: (openBlueFail) => {
                            if (openBlueFail.state === 3) {
                                // 说明微信应用蓝牙未授权
                                showModal({
                                    content: '检测到您未允许微信访问手机蓝牙权限,是否打开系统设置?',
                                    showCancel: false,
                                    confirmText: '前往设置',
                                    success: () => {
                                        // 跳转微信应用权限
                                        openAppAuthorizeSetting();
                                    },
                                });
                            } else if (openBlueFail.state === 4) {
                                // 说明系统蓝牙未开启
                                showModal({
                                    content:
                                        '蓝牙设置 - 小程序需要通过蓝牙搜索和连接设备,请确认手机蓝牙功能是否已开启?',
                                    showCancel: false,
                                    confirmText: '我已开启',
                                    success: async () => {
                                        const { bluetoothEnabled, platform } = getSystemInfoSync();
                                        if (bluetoothEnabled) {
                                            // 开启蓝牙功能
                                            resolve(true);
                                        } else {
                                            toast({ title: '手机蓝牙未开启' });
                                        }
                                    },
                                });
                            }
                        },
                    });
                });
            } else {
                return new Promise(function (resolve, reject) {
                    // andriod
                    if (!bluetoothEnabled) {
                        // 说明系统蓝牙未开启
                        showModal({
                            content: '蓝牙设置 - 小程序需要通过蓝牙搜索和连接设备,请确认手机蓝牙功能是否已开启?',
                            showCancel: false,
                            confirmText: '我已开启',
                            success: async () => {
                                const { bluetoothEnabled, platform } = getSystemInfoSync();
                                if (bluetoothEnabled) {
                                    // 开启蓝牙功能
                                    resolve(true);
                                } else {
                                    toast({ title: '手机蓝牙未开启' });
                                }
                            },
                        });
                    } else {
                        // 开启蓝牙功能
                        resolve(true);
                    }
                });
            }
        };

  • 23
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值