<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="js/mui.min.js"></script>
<script src="js/sbbluetooth.js"></script>
<link href="css/mui.min.css" rel="stylesheet" />
</head>
<body>
<header class="mui-bar mui-bar-nav">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">附近蓝牙</h1>
</header>
<div class="mui-content">
</div>
</body>
<script type="text/javascript">
mui.init()
var CBCentralManager = plus.ios.import("CBCentralManager");
var manager = new CBCentralManager();
mui.plusReady(function() {
searchIOS()
})
function searchIOS() {
var delegate = plus.ios.implements("CBCentralManagerDelegate", {
"centralManagerDidUpdateState:": centralManagerDidUpdateState,
"centralManager:didDiscoverPeripheral:advertisementData:RSSI:": didDiscoverPeripheral
});
var delegate2 = plus.ios.implements("CBPeripheralDelegate",{
})
manager.initWithDelegatequeue(delegate, null);
}
function centralManagerDidUpdateState(central) {
//console.log(central);
var state = central.plusGetAttribute('state');
console.log(state);
if(state == 4) {
console.log('请开启蓝牙');
}
if(state == 5) {
console.log("中央外设管理器状态 state= " + state);
manager.scanForPeripheralsWithServicesoptions(null, null);
}
}
function didDiscoverPeripheral(central, peripheral, advertisementData, RSSI) {
console.log(JSON.stringify(central))
var name = peripheral.plusGetAttribute("name");
var uuid = peripheral.plusGetAttribute("identifier").plusGetAttribute("UUIDString");
var rssi = RSSI.plusGetAttribute("intValue");
console.log("蓝牙设备:"+ name + " " + uuid + " " + rssi + " " + dist + "米");
}
function calcDistByRSSI(rssi, a, n) {
var rs = Math.abs(rssi);
var power = (rs - a) / (10 * n);
return Math.pow(10, parseFloat(power)).toFixed(2);
}
</script>
</html>
didDiscoverPeripheral 这个方法始终不走,请问有大神知道吗
2 个回复
4***@qq.com
求解
DCloud_heavensoft
更新:5+已提供蓝牙,参考http://www.html5plus.org/doc/zh_cn/bluetooth.html 注意需要最新版HBuilderX 1.3