fwyc
fwyc
  • 发布:2019-03-13 12:54
  • 更新:2019-11-21 11:15
  • 阅读:2532

Bluetooth模块为什么demo示例无法运行

分类:HBuilder

我使用的是demo代码,但是总是报错 ,这是没什么呢?(小白一枚,求指导)
09:08:40.734 Uncaught TypeError: Cannot read property 'openBluetoothAdapter' of undefined at index.html:14
09:08:44.703 Uncaught TypeError: Cannot read property 'getBluetoothAdapterState' of undefined at index.html:25
09:08:45.516 Uncaught TypeError: Cannot read property 'closeBluetoothAdapter' of undefined at index.html:36
09:08:46.828 Uncaught TypeError: Cannot read property 'openBluetoothAdapter' of undefined at index.html:14
代码如下:

<!DOCTYPE html>  
<html>  
    <head>  
    <meta charset="utf-8">  
    <title>Bluetooth Example</title>  
    <script type="text/javascript" >  
// 扩展API加载完毕后调用onPlusReady回调函数   
document.addEventListener('plusready', onPlusReady, false);  
// 扩展API加载完毕,现在可以正常调用扩展API  
function onPlusReady(){  
}  
// 打开蓝牙模块  
function openBluetoothAdapter(){  
    plus.Bluetooth.openBluetoothAdapter({  
        success:function(e){  
            console.log('open success: '+JSON.stringify(e));  
        },  
        fail:function(e){  
            console.log('open failed: '+JSON.stringify(e));  
        }  
    });  
}  
// 获取蓝牙状态  
function getBluetoothState(){  
    plus.Bluetooth.getBluetoothAdapterState({  
        success:function(e){  
            console.log('state success: '+JSON.stringify(e));  
        },  
        fail:function(e){  
            console.log('state failed: '+JSON.stringify(e));  
        }  
    });  
}  
// 关闭蓝牙模块  
function closeBluetoothAdapter(){  
    plus.Bluetooth.closeBluetoothAdapter({  
        success:function(e){                                              
            console.log('close success: '+JSON.stringify(e));  
        },  
        fail:function(e){  
            console.log('close failed: '+JSON.stringify(e));  
        }  
    });  
}  

    </script>  
    </head>  
    <body >  
        <button onclick="openBluetoothAdapter()">打开蓝牙模块</button>  
        <br/>  
        <button onclick="getBluetoothState()">获取蓝牙状态</button>  
        <br/>  
        <button onclick="closeBluetoothAdapter()">关闭蓝牙模块</button>  
    </body>  
</html>
2019-03-13 12:54 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com

小老弟,我也是这种情况

  • z***@gmail.com

    是不是你用的不是HBuilderX?要新版才有这个api

    2019-03-28 06:38

  • z***@gmail.com

    用X就好了。

    2019-04-19 03:11

z***@gmail.com

z***@gmail.com - 40不惑

建议新建一个本地Hello H5+项目看demo,把demo运行到您的真机上看看你要学习的模块是否正常。 很多情况下新手分析不出来是什么问题会比较打击,这样入手也算是一个逆向思维了。

happy coding!

z***@gmail.com

z***@gmail.com - 40不惑

是不是你用的不是HBuilderX?要新版才有这个api

1***@qq.com

1***@qq.com

这个问题解决了吗

阿啰

阿啰

我也遇到这个问题了,在模拟器上上输出的是fail里面的报错,未初始化,请问解决了吗

该问题目前已经被锁定, 无法添加新回复