chrvadala / node-ble

Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus
https://www.npmjs.com/package/node-ble
MIT License
308 stars 45 forks source link

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

Closed Matebo closed 4 months ago

Matebo commented 1 year ago
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?

chrvadala commented 4 months ago

Fixed with 1.10