2017-02-23 53 views
0

我正在開發一個軟件,他將使用Web藍牙API連接到BT到串行適配器。它似乎支持寫入和通知。但我不能工作。無法通知在Web藍牙API中工作

事件永遠不會被解僱。現在我正在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(); 
        }); 
      }; 

回答

2

https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#receive_gatt_notifications,它看起來像你需要調用characteristic.startNotifications()讓瀏覽器知道你要接受關貿總協定通知:

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 
} 
0

是的,你是絕對正確的。在我的串口到BT適配器的波特率上出現了2個錯誤,這使我沒有發送我認爲的我。另一個是沒有調用startnotifications。

謝謝 Anders