通过HC-05和node.js将控制器连接到智能手机



我正在构建一个android应用程序,作为IOT控制器的接口。我已经有了芯片的代码,它连接到HC-05蓝牙模块。我试过使用应用商店的蓝牙终端,我的手机成功连接到HC-05。我现在正在构建移动应用程序,以便从芯片发送/接收数据。所以我需要直接从node.js连接到HC-05,而这正是我遇到的问题。

我一直在寻找可以帮助我的npm模块,到目前为止,我已经找到了网络蓝牙终端,蓝牙终端,串口,蓝牙串口和johnny五。问题是,我不确定这两者之间的区别是什么,也不确定哪一个能真正用于HC-05。据我所知,johnny five是为了编写控制器本身的代码,而不是连接到蓝牙模块,我不确定网络蓝牙终端是否可以连接到9600 baudrate的HC-05,不同的网站会说不同的事情。我怎样才能做到这一点?

我可能回答有点晚,但我正在将带有HC-05模块的arduino连接到NodeJS应用程序,我偶然发现了Node蓝牙库。通过使用它,我可以通过以下操作连接HC-05:

router.post('/connect', function (req, res) {
res.render('connect');
const bluetooth = require('node-bluetooth');
const device = new bluetooth.DeviceINQ();
// Find devices
device
.on('finished', console.log.bind(console, 'finished'))
.on('found', function found(address, name) {
console.log('Found: ' + address + ' with name ' + name);
// We know our Arduino bluetooth module is called 'HC-05', so we only want to connect to that.
if (name === 'HC-05') {
// find serial port channel
device.findSerialPortChannel(address, function (channel) {
console.log('Found channel for serial port on %s: ', name, channel);
// make bluetooth connect to remote device
bluetooth.connect(address, channel, function (err, connection) {
if (err) return console.error(err);
// This is some example code from the library for writing, customize as you wish.
connection.delimiter = Buffer.from('/n', 'utf-8');
connection.on('data', (buffer) => {
console.log('received message: ', buffer.toString());
});
// This is some example code from the library for writing, customize as you wish.
connection.write(new Buffer('hello', 'utf-8'), () => {
console.log('wrote');
});
});
});
}
});
device.scan();
});

我知道检查"HC-05"字符串可能不是很好的做法,但它可以用于测试目的。

最新更新