javascript - UWP BTLE : Cannot read device advertisements

标签 javascript windows visual-studio uwp

我是这个 UWP 的新手,请耐心等待。我修改了以下MS GitHub中的代码:Link创建一个可以“观看”BTLE 广告的 Windows Phone 应用程序。

但它无法读取任何广告。我的手机确实支持 BTLE,我可以在 Windows BT 设置中看到设备,因此该设备也在宣传它。请帮我找出我错在哪里以及为什么。

这是我的 JS 代码:

var watcher = new Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher();
//watcher.signalStrengthFilter.inRangeThresholdInDBm = -70;
//watcher.signalStrengthFilter.outOfRangeThresholdInDBm = -75;
//watcher.signalStrengthFilter.outOfRangeTimeout = 2000;

$(document).ready(function () {
    console.log("HERE: ready");
    watcher.onreceived = onAdvertisementReceived;
    $("button#start").unbind('click').on('click', function (e) {
        console.log('CLICKED >');
        e.preventDefault();
        watcher.start();
    });
    $("button#stop").unbind('click').on('click', function (e) {
        console.log('CLICKED <');
        e.preventDefault();
        watcher.stop();
    });
});
function onAdvertisementReceived(eventArgs) {
    console.log("HERE: function watcher", eventArgs);
    var timestamp = eventArgs.timestamp;
    var advertisementType = eventArgs.advertisementType;
    var rssi = eventArgs.rawSignalStrengthInDBm;
    var localName = eventArgs.advertisement.localName;
    $("div#list > ul").append("<li> Timestamp: <strong>" + timestamp.getHours() + ":" + timestamp.getMinutes() +
        ":" + timestamp.getSeconds() + "</strong> Type:" + advertisementType.toString() + " RSSI:" + rssi.toString() + " Name:" +
        localName + "</li>");
}

这是我的 HTML 代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Bluetooth LE Smart Watch</title>
    <link href="css/default.css" rel="stylesheet" />
    <link href="css/materialize.min.css" rel="stylesheet" />
</head>
<body class="container">
    <div class="row">
        <h4>List of BTLE Devices</h4>
        <button class="btn" id="start">Start Watcher</button><button class="btn" id="stop">Stop Watcher</button>
        <div id="list" class="col m12 s12">
            <ul>

            </ul>
        </div>
    </div>
    <script src="js/jquery-2.2.4.min.js"></script>
    <script src="js/materialize.min.js"></script>
    <script src="js/main.js"></script>
</body>
</html>

最佳答案

But it is not able to read it any advertisements. My phone does support BTLE, I am able to see the devices in Windows BT Settings so the device is advertising it too. Please help me find where I am wrong and why.

要使 BTLE 工作,您需要在 package.appxmannifest 中启用蓝牙功能。 。您可以通过以下方式实现此目的:

在VS2015中->双击package.appxmannifest ->功能->检查蓝牙功能。

或者您可以打开package.appxmannifest在代码 View 中添加 <DeviceCapability Name="bluetooth" />在功能标签中:

<Capabilities>
   <Capability Name="internetClient" />
   <DeviceCapability Name="bluetooth" />
</Capabilities>

关于javascript - UWP BTLE : Cannot read device advertisements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234788/

相关文章:

Windows 7 组策略编辑器 - 关闭批处理文件未执行

.net - 为什么 Visual Studio/CLR 会生成 "bad"可执行文件?

javascript - 带有 $state.go 和 $scope 的参数

javascript - 如何在 jQuery 中的每个单击事件中 append 文本 x 次?

javascript - 为什么这个 getter 返回未定义?

c++ - 打开cmd并读写

windows - 静态类和 "this"关键字

javascript - Apps 脚本 - 无法在 html 模板的 js 函数中使用 google.script.run?

c# - Visual Studio 中的本地化文件路径

c# - 像VS那样实现一个窗口管理器就可以了