nodeBlutTooth.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const bluetooth = require('node-bluetooth');
  2. //console.log(bluetooth)
  3. const device = new bluetooth.DeviceINQ();
  4. try {
  5. device
  6. .on('finished', console.log.bind(console, 'finished'))
  7. .on('found', function found(address, name){
  8. console.log('Found: ' + address + ' with name ' + name);
  9. // find serial port channel
  10. device.findSerialPortChannel(address, function(channel) {
  11. console.log('Found RFCOMM channel for serial port on %s: ', name, channel);
  12. // make bluetooth connect to remote device
  13. try {
  14. bluetooth.connect(address, channel, function (err, connection) {
  15. if (err) return console.error(err);
  16. connection.delimiter = Buffer.from('\n', 'utf8');
  17. connection.on('data', (buffer) => {
  18. console.log('received message:', buffer.toString());
  19. });
  20. connection.write(new Buffer('Hello!', 'utf-8'), () => {
  21. console.log('wrote');
  22. });
  23. });
  24. } catch (e) {
  25. console.log(123123)
  26. console.log(e)
  27. }
  28. });
  29. })
  30. device.scan();
  31. } catch (err) {
  32. console.log(err)
  33. }