lsongdev / lsongdev/node-bluetooth

Detecting bluetooth device disconnects/crashes

Open
#57 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
205
Forks
56
PR merge metrics
No merged PRs in 30d

Description

Hi!
I couldn't find a way to detect if a connection has crashed or disconnected so I tried to implement my own.
In lib/connection.js, I noticed that - if I printed the chunks of the read method - they stop printing as soon as I turn off my bluetooth device; after ~20seconds of apparent frozen behavior, this method continued working, printing chunks of zero length buffers.
Thus, I added the following line
if(chunk.length == 0){ self.emit('error')}
in

(function read(){
      self.isOpen() && self.port.read((err, chunk) => {
        process.nextTick(read);
        if(err) return self.emit('error', err);
        self.emit('data', chunk);
        if(chunk.length == 0){ self.emit('error')}
      });
    })();

This allows me to fetch error events like so:

    connect(address, 1, (err, linkage) => {
      if (err) {
        console.log('connection attempt error');
      } else {
        linkage.on('error', (err) => {
          console.log('disconnected!);
        });
      }
    )}

However my approach assumes I won't be receiving zero length buffers when connected which I realize that might not always be true (or not for all devices);
Also, any thoughts on how to detect the disconnect before the ~20seconds pass?
Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/connection.js and inspect the read callback, including how zero-length buffers and read errors are emitted. Reproduce the Bluetooth shutdown behavior described in the issue and determine the expected disconnect event and timing. Done means the disconnect behavior is defined and covered without assuming zero-length buffers are impossible during a valid connection.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.