node-ble icon indicating copy to clipboard operation
node-ble copied to clipboard

Scanning multiple times causes "MaxListenersExceededWarning: Possible EventEmitter memory leak detected."

Open Matebo opened this issue 1 year ago • 0 comments

const {createBluetooth} = require('node-ble')
const {bluetooth, destroy} = createBluetooth()

async function main(){
  const adapter = await bluetooth.defaultAdapter()
  async function search(){

    if (! await adapter.isDiscovering()){
      await adapter.startDiscovery()
    }
    const devices = await adapter.devices()
    
    devices.forEach(async mac => {
      const device = await adapter.waitDevice(mac)
      let rssi;
      try {
        rssi = await device.getRSSI()
      } catch (error) {
        //console.error(error)
      }
      
      console.log(mac, rssi)
      device.disconnect()
    });
    //destroy()
    //await device.disconnect()
  }

setInterval(() => {
  search()
}, 5000);

}
main()

When I run this code it works for a minute and prints mac addresses and their RSSI value Then i start getting warnings

(node:2234) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 {"path":"/org/bluez/hci0/dev_CC_98_8B_A7_XX_XX","interface":"org.freedesktop.DBus.Properties","member":"PropertiesChanged"} listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

After about 10 minutes it crashes with the following error message

/home/compulab/projects/newNode-ble/node_modules/node-ble/src/Adapter.js:158
        reject(new Error('operation timed out'))
               ^

Error: operation timed out
    at Timeout._onTimeout (/home/compulab/projects/newNode-ble/node_modules/node-ble/src/Adapter.js:158:16)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Node.js v18.15.0

I'm running Debian 11 with kernel version 5.15.5 on a board manufactured by Compulab Any suggestions?

Matebo avatar Mar 14 '23 17:03 Matebo