cordova(ios android)蓝牙权限 手机蓝牙开关开启 跳到设置界面

用到的cordova插件:
app权限相关:cordova-diagnostic-plugin (github)
弹框插件:cordova-plugin-dialogs(github)

本人代码:

//处理蓝牙不可用,
function dealBluetoothIsNotOn(){

    console.log("getBluetoothState")
    cordova.plugins.diagnostic.getBluetoothState(function(state){
        console.log(state)
        if(state === cordova.plugins.diagnostic.bluetoothState.UNAUTHORIZED){
            console.log("没有授权")
            function onConfirm(buttonIndex) {
                if (buttonIndex == 2) {
                    console.log("苹果蓝牙授权")
                    console.log("跳到设置界面")
                    cordova.plugins.diagnostic.switchToSettings(function(){
                        console.log("Successfully switched to Settings app");
                    }, function(error){
                        console.error("The following error occurred: "+error);
                    });
                }
            }
            navigator.notification.confirm(
                Language.bluetoothAuthorization, // message
                onConfirm,            // callback to invoke with index of button pressed
                Language.prompt,           // title
                [Language.cancel, Language.done]     // buttonLabels
            );

        }
        if(state === cordova.plugins.diagnostic.bluetoothState.POWERED_OFF){
            console.log("蓝牙开关没有开")

            if (navigator.platform == "iPhone") {
                console.log("苹果蓝牙开关")
               navigator.notification.alert(Language.blueToothOff, function () { }, Language.prompt, Language.ok)
              } else {
                function onConfirm(buttonIndex) {
                    if (buttonIndex == 2) {
                            console.log("安卓")
                            cordova.plugins.diagnostic.switchToBluetoothSettings(); //ios不可用
                    }
                }
                navigator.notification.confirm(
                    Language.bluetoothSwitch, // message
                    onConfirm,            // callback to invoke with index of button pressed
                    Language.prompt,           // title
                    [Language.cancel, Language.done]     // buttonLabels
                );
              }

        }
    }, function(error){
        console.error(error);
    });

}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页