lsongdev / lsongdev/node-bluetooth
Detecting bluetooth device disconnects/crashes
还没有人认领这个 Issue。
- 主要语言
- C++
- 星标
- 205
- 派生
- 56
- PR 合并指标
- 30 天内没有已合并 PR
描述
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!
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 lib/connection.js 开始,检查 read callback,包括零长度缓冲区和读取错误的发出方式。重现 issue 中描述的 Bluetooth 关闭行为,并确定预期的断开连接事件及其发生时机。在不假定有效连接期间不可能出现零长度缓冲区的前提下,定义并覆盖断开连接行为,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- embedded-iot, networking
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 30/100