详情描述: 最近DCloud 团队开发一个蓝牙模块,而我刚刚需要蓝牙模块的开发。IPone 6p j将文档的(http://www.html5plus.org/doc/zh_cn/bluetooth.html相关代码复制过来进行相关测试;发现无法正常跟 苹果6p进行相关链接。 报一个( TypeError: null is not an object (evaluating 't.status')file name:index.htmlline no:2)
[内容]
1 、
将文档的代码进行复制、
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bluetooth Example</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<script type="text/javascript" >
// 扩展API加载完毕后调用onPlusReady回调函数
document.addEventListener('plusready', onPlusReady, false);
// 扩展API加载完毕,现在可以正常调用扩展API
function onPlusReady(){
}
// 蓝牙设备id,可通过onBluetoothDeviceFound方法获取
var deviceId = '';
// 蓝牙服务id,可通过getBLEDeviceServices方法获取
var serviceId = '';
// 蓝牙特征值id,可通过getBLEDeviceCharacteristics方法获取
var characteristicId = '';
// 要写入的数据
var value = new ArrayBuffer(8);
var iv = new Int32Array(value);
iv[0] = 120, iv[2]=100;
// 连接蓝牙设备
// 打开蓝牙模块
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 listenerStateChange(){
plus.bluetooth.onBluetoothAdapterStateChange(function(e){
console.log('state changed: '+JSON.stringify(e));
});
}
function getServices(){
plus.bluetooth.getBLEDeviceServices({
deviceId:deviceId,
success:function(e){
var services = e.services;
console.log('get services success: '+services.length);
for(var i in services){
console.log(i+': '+JSON.stringify(services[i]));
}
},
fail:function(e){
console.log('get services failed: '+JSON.stringify(e));
}
});
}
// 开始搜索蓝牙
function startBluetoothDiscovery(){
plus.bluetooth.startBluetoothDevicesDiscovery({
success:function(e){
console.log('start discovery success: '+JSON.stringify(e));
},
fail:function(e){
console.log('start discovery failed: '+JSON.stringify(e));
}
});
}
// 结束搜索蓝牙
function stopBluetoothDiscovery(){
plus.bluetooth.stopBluetoothDevicesDiscovery({
success:function(e){
console.log('stop discovery success: '+JSON.stringify(e));
},
fail:function(e){
console.log('stop discovery 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));
}
});
}
function createConnection(){
plus.bluetooth.createBLEConnection({
deviceId:deviceId,
success:function(e){
console.log('create connection success: '+JSON.stringify(e));
},
fail:function(e){
console.log('create connection failed: '+JSON.stringify(e));
}
});
}
// 写入低功耗蓝牙设备的特征值
function writeCharacteristics(){
plus.bluetooth.writeBLECharacteristicValue({
deviceId:deviceId,
serviceId:serviceId,
characteristicId:characteristicId,
value:value,
success:function(e){
console.log('write characteristics success: '+JSON.stringify(e));
},
fail:function(e){
console.log('write characteristics failed: '+JSON.stringify(e));
}
});
}
function getBluetoothDevices(){
plus.bluetooth.getBluetoothDevices({
success:function(e){
console.log(e)
// var devices = e.devices;
// console.log('get devices success: '+e.length);
// for(var i in devices){
// console.log(i+': '+JSON.stringify(devices[i]));
// }
},
fail:function(e){
console.log('get devices failed: '+JSON.stringify(e));
}
});
}
// 断开蓝牙设备
function closeConnection(){
plus.bluetooth.closeBLEConnection({
deviceId:deviceId,
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="listenerStateChange()">监听状态变化</button>
<br/>
<button onclick="startBluetoothDiscovery()">开始搜索蓝牙</button>
<br/>
<button onclick="getBluetoothDevices()">获取搜索到蓝牙</button>
<br/>
<button onclick="stopBluetoothDiscovery()">结束搜索蓝牙</button>
<br/>
<button onclick="closeBluetoothAdapter()">关闭蓝牙模块</button>
<button onclick="createConnection()">连接蓝牙设备</button>
<br/>
<button onclick="listenerCharacteristicsChange()">监听特征值变化</button>
<br/>
<button onclick="writeCharacteristics()">写入特征值</button>
<br/>
<button onclick="closeConnection()">断开蓝牙设备</button>
</body>
</html>
运行
[结果]
[ERROR] : TypeError: null is not an object (evaluating 't.status')file name:index.htmlline no:2
[期望]
IDE运行环境说明
HBuilderX 1.3
[Android版本号]
正常
[iOS版本号]
会报错
[手机型号]
ipone 6p
[模拟器型号]
附件
[IDE问题请提供HBuilderX运行日志。菜单帮助-查看运行日志,点右键打开文件所在目录,将log文件压缩成zip包上传]
[App问题请提供可重现问题的代码片段,你补充的细一点,问题就解决的快一点]
[安装包]
联系方式
[QQ] 2486077960
3***@qq.com (作者)
离线 太麻烦了
2018-12-20 11:58