每个人都在第一篇文章中说“我是新人”并试图找出一个复杂性。
我正在使用带有蓝牙的Raspberry pi3,Raspbian OS和nodejs v7.4.0
我选择wifi-control来使用wifi网络,它可以运行。有一件事 - 我应该用 sudo 运行 npm run 来获取所有网络,而不仅仅是当前网络;
然后我尝试通过bluetooth-serial-port lib使用蓝牙。 首先,我做了所有用文档编写的准备工作。
var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
btSerial.inquire();
它什么也没做。至少我没有看到任何效果 - 我的手机没有在可用的蓝牙设备列表中“看到”Raspberry;
我认为我的Raspberry有蓝牙问题,但后来我运行
bluetoothctl - >开机 - >
可以发现Raspberry出现在手机上。
我应该怎么做才能“开启”蓝牙控制并将我的Raspberry添加到可用的蓝牙设备列表中?
和平!
答案 0 :(得分:0)
得到完全相同的问题。我的解决方案(可能适合或不适合您的需求)是事先通过bluetoothctl
配对手机。 (这已经是你为什么这个解决方案有点糟糕了:第二天你不能来使用不同的手机/ pi,当换到不同的pi时,东西完全搞砸了:D)
哦,主脚本应该以root身份运行,否则所有这些都无法正常工作。
1)配对您的设备
$ bluetoothctl
[bluetoothctl] agent on
[bluetoothctl] discoverable on
[bluetoothctl] pairable on
[bluetoothctl] scan on
(现在你打开手机上的蓝牙并搜索设备,在bluetoothctl中你会看到你的设备和mac显示)
[bluetoothctl] pair XX:XX...(MAC of your phone)
(手机会显示"哟这个设备想要配对" -dialog和bluetoothctl也希望你确认配对)
现在你可以通过蓝牙连接到pi,如果它是可发现的。 (我使用Serial Bluetooth Terminal)
2)设备之间的实际通话
NONE 的npm软件包应该与蓝牙配合使用。不是一个人。所以最后我使用rfcomm
并且能够在连接时启动程序。与serialport一起,我让rfcomm
运行node myscript.js
,这样就建立了实际的串行连接:
2.1)rfcomm
等待连接
const rfcommProc = spawn(
'rfcomm',
['watch', 'hci0', '1', 'node', `${__dirname}/myscript.js`]
);
2.2)myscript.js
打开端口
const port = new SerialPort('/dev/rfcomm0', spError => {
if (spError) {
process.send(spError);
}
})
在npmjs页面上查看如何接收和发送内容。(: 希望这会给你一些想法和/或帮助。
3)注意
rc.local
似乎已经足够晚了#34;让它在启动时运行和监听。