簡體   English   中英

Node.js 中的 Noble BLE - 有時有效,有時無效

[英]Noble BLE in Node.js - sometimes works, sometimes doesn't

我有以下代碼正在嘗試運行,每次運行時都會得到不同的結果。 有時我只是得到“關機”。 有時我會加上“開機”。 有時我會得到以上所有內容以及“開始掃描”。 有時它一直運行,我得到一堆“仍在搜索......”,它最終擊中 BLE 設備並返回廣告。

我不知道為什么這有時會起作用,而有時卻不起作用。

var async = require('async');
var noble = require('../index');

var peripheralIdOrAddress = "8cf681a590f3";

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning();
    console.log("Powered On")
  } else {
    noble.stopScanning();
    console.log("Powered Off")
  }
});

noble.on('scanStart', ()=>console.log("Scanning started"))
noble.on('scanStop', ()=>console.log("Scanning stopped"))

noble.on('discover', function(peripheral) {
  console.log("Discovered something!");
  if (peripheral.id === peripheralIdOrAddress || peripheral.address === peripheralIdOrAddress) 
  {
    noble.stopScanning();
    console.log('peripheral with ID ' + peripheral.id + ' found');
    var advertisement = peripheral.advertisement;
    console.log(JSON.stringify(advertisement));

    peripheral.on('disconnect', function() {
      console.log("Peripheral disconnected");
      process.exit(0);
    });

    peripheral.on('connect', ()=>console.log("Peripheral connected"));
  
    peripheral.connect(function(error) {
      peripheral.discoverServices("a0521000-2a63-479e-9d0a-09dfa7c8fd98",()=>console.log("Service discovered"));

    });
  }
  else{console.log("Not the one.  still searching...")}
});

這個問題的瘋狂補充。 我嘗試了很多關閉東西和拔掉東西的東西。

最后我發現它是電源端口。 如果我插入牆上的電源,我會得到大約 10 次有效的運行。 如果我拔掉牆上的電源並關閉電池,它每次都可以工作。

太奇怪了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM