【问题标题】:Can't get notify to work in Web Bluetooth API无法通知在 Web 蓝牙 API 中工作
【发布时间】:2017-02-24 20:04:33
【问题描述】:

我正在开发一个软件,它将使用 Web 蓝牙 API 连接到 BT 到串行适配器。它似乎支持写入和通知。但我不能让nofity工作。

该事件永远不会被触发。现在我正在 Mac 上的 Canary 中进行测试。

谢谢 安德斯

我的搜索/配对和添加事件代码:

var readCharacteristic;
var writeCharacteristic;
var serialBLEService = 'ba920001-d666-4d59-b141-05960b3a4ff7';
var txChar = 'ba920002-d666-4d59-b141-05960b3a4ff7';
var rxChar = 'ba920003-d666-4d59-b141-05960b3a4ff7';

$scope.writeToSerial = function (valueToWrite) {
                var tmpValue = $('input').val();

                valueToWrite = utf8AbFromStr(tmpValue);

                writeCharacteristic.writeValue(valueToWrite)
                    .then( a => {
                        alert("Written: " + valueToWrite);
                    })
                    .catch(function (error) {
                        // And of course: error handling!
                        console.error('Something went wrong!', error);
                    });
            }

function handleCharacteristicValueChanged(event) {
                var value = event.target.value;
                console.log('Received ' + value);
            }

$scope.searchForBTDevices = function() {
                navigator.bluetooth.requestDevice({
                        filters: [{ services: [serialBLEService] }],
                        optionalServices: [
                            serialBLEService, rxChar, txChar, configChar
                        ]

                    })
                    .then(device => {
                        return device.gatt.connect();

                    })
                    .then(server => {
                        return server.getPrimaryService(serialBLEService);
                    })
                    .then(service => {
                        return service.getCharacteristics();
                    })
                    .then(characteristics => {
                        $scope.btResult += '>> Found Characteristics!\n';
                        $scope.$apply();
                        var queue = Promise.resolve();
                        characteristics.forEach(characteristic => {
                            switch (characteristic.uuid) {

                            case rxChar:

                                readCharacteristic = characteristic;
                                readCharacteristic.addEventListener('characteristicvaluechanged',
                                    handleCharacteristicValueChanged);

                                break;

                            case txChar:
                                writeCharacteristic = characteristic;
                                break;


                            }
                        });

                    })
                    .catch(error => {
                        $scope.btResult = '>> Error:  ' + error;
                        $scope.$digest();
                    });
            };

【问题讨论】:

    标签: javascript bluetooth-lowenergy web-bluetooth


    【解决方案1】:

    根据https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#receive_gatt_notifications,看来您需要调用characteristic.startNotifications() 让浏览器知道您想要接收GATT 通知:

    navigator.bluetooth.requestDevice({ filters: [{ services: ['heart_rate'] }] })
    .then(device => device.gatt.connect())
    .then(server => server.getPrimaryService('heart_rate'))
    .then(service => service.getCharacteristic('heart_rate_measurement'))
    .then(characteristic => characteristic.startNotifications())
    .then(characteristic => {
      characteristic.addEventListener('characteristicvaluechanged',
                                      handleCharacteristicValueChanged);
      console.log('Notifications have been started.');
    })
    .catch(error => { console.log(error); });
    
    function handleCharacteristicValueChanged(event) {
      var value = event.target.value;
      console.log('Received ' + value);
      // TODO: Parse Heart Rate Measurement value.
      // See https://github.com/WebBluetoothCG/demos/blob/gh-pages/heart-rate-sensor/heartRateSensor.js
    }
    

    【讨论】:

      【解决方案2】:

      是的,你完全正确。我的串行到 BT 适配器的波特率首先出现 2 个错误,这使得我没有发送我认为的内容。另一个是 startnotifications 没有被调用。

      谢谢 安德斯

      【讨论】:

        猜你喜欢
        • 2015-08-19
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        • 2011-07-20
        • 1970-01-01
        • 2022-10-04
        • 1970-01-01
        • 2018-05-24
        相关资源
        最近更新 更多