lsongdev / node-bluetooth

:large_blue_diamond:😬Bluetooth serial port communication for Node.js
https://npmjs.org/node-bluetooth
Other
197 stars 56 forks source link

connect problem #20

Closed andy6804tw closed 5 years ago

andy6804tw commented 5 years ago

Hi this is my error message. how can I solve it? thx!

Found RFCOMM channel for serial port on BRA-801:  1
2018-11-09 21:40:56.290 node[2668:117275] instantiateOnDevice for regular
/Users/ca/Desktop/bluetooth/node_modules/node-bluetooth/lib/connection.js:9
    const self = this;
                 ^

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
    at new Connection (/Users/cai/Desktop/bluetooth/node_modules/node-bluetooth/lib/connection.js:9:18)
    at /Users/cai/Desktop/bluetooth/node_modules/node-bluetooth/index.js:18:33
daksen commented 5 years ago

I also have the same problem :/

lsongdev commented 5 years ago

What's version of module and node ?

andy6804tw commented 5 years ago

@song940 v9.11.1

daksen commented 5 years ago

@andy6804tw node v10.13.0

lsongdev commented 5 years ago

Upgrade node-bluetooth to latest version.

andy6804tw commented 5 years ago

@song940 same problem. @daksen and you?

lsongdev commented 5 years ago

@andy6804tw pls copy your "lib/connection.js" file and paste here .

cat /Users/ca/Desktop/bluetooth/node_modules/node-bluetooth/lib/connection.js | pbcopy
andy6804tw commented 5 years ago

@song940

const EventEmitter = require('events');

/**
 * [function description]
 * @return {[type]} [description]
 */
class Connection extends EventEmitter {
  constructor(port, address) {
    const self = this;
    this.port = port;
    this.address = address;
    this.buffer = Buffer.alloc(0);
    const read = function () {
      process.nextTick(function () {
        if (self.isOpen()) {
          self.port.read(function (err, chunk) {
            if (err) return self.emit('error', err);
            if (self.delimiter) {
              let data = Buffer.concat([self.buffer, chunk]);
              let position;
              while ((position = data.indexOf(self.delimiter)) !== -1) {
                self.emit('data', data.slice(0, position));
                data = data.slice(position + self.delimiter.length);
              }
              self.buffer = data;
            } else {
              self.emit('data', chunk);
            }
            read();
          });
        }
      });
    }
    read();
  }
  /**
   * [write description]
   * @param  {[type]}   data     [description]
   * @param  {Function} callback [description]
   * @return {[type]}            [description]
   */
  write(data, callback){
    this.port.write(data, this.address, callback);
    return this;
  }
  /**
   * [close description]
   * @param  {Function} callback [description]
   * @return {[type]}            [description]
   */
  close(){
    try {
      this.port.close(this.address);
      this.port = undefined;
      callback && callback();
    } catch (e) {
      callback && callback(e);
    }
    return this;
  }
  isOpen(){
    return this.port !== undefined;
  }
}

module.exports = Connection;
lsongdev commented 5 years ago

fixed, pls install latest version

npm i  node-bluetooth@1.2.4
andy6804tw commented 5 years ago

@song940 Thank you for your help! But the device is not with me. I am testing the day after tomorrow.

lsongdev commented 5 years ago

Any questions?