简体   繁体   English

蓝牙连接问题。 Raspberry pi3和nodejs 7.4.0

[英]Issue with Bluetooth connection. Raspberry pi3 and nodejs 7.4.0

Ass everyone saying in their first posts "I'm new one" and trying to figure out one complexity. 屁股大家在他们的第一篇文章中说“我是新人”并试图找出一个复杂性。

  1. I want to scan and create list of available wifi networks in Raspberry area and send the list to the phone via bluetooth. 我想在Raspberry区域扫描并创建可用的wifi网络列表,并通过蓝牙将列表发送到手机。
  2. Next - select one of the networks in the list on the phone, enter a password to selected network and send back via bluetooth to Raspberry 下一步 - 选择手机列表中的一个网络,输入所选网络的密码,然后通过蓝牙发送回Raspberry

I'm using Raspberry pi3 with bluetooth on board, Raspbian OS and nodejs v7.4.0 我正在使用带有蓝牙的Raspberry pi3,Raspbian OS和nodejs v7.4.0

I choose wifi-control to work with wifi network and it works greate. 我选择wifi控制来使用wifi网络,它的工作原理很棒。 One thing - I should run npm run with sudo to get all networks, not just current one; 有一件事 - 我应该运行npm运行 sudo以获取所有网络,而不仅仅是当前的网络;

Then I'm trying to work with bluetooth via bluetooth-serial-port lib. 然后我试图通过bluetooth-serial-port lib使用蓝牙 First of all I did all preparations that was written in documentation. 首先,我做了所有用文档编写的准备工作。

 var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort(); btSerial.inquire(); 

And it does nothing. 它什么都不做。 At least I don't see any effect - my phone doesn't "see" Raspberry in available bluetooth devices list; 至少我没有看到任何效果 - 我的手机没有在可用的蓝牙设备列表中“看到”Raspberry;

I thought that my Raspberry has porblems with bluetooth, but then I run 我以为我的Raspberry有蓝牙问题,但后来我跑了

bluetoothctl -> power on -> discoverable on bluetoothctl - >开机 - >可发现

And Raspberry appeared on phone. 而Raspberry出现在手机上。

What should I do to "turn on" Bluetooth control and add my Raspberry to list of available bluetooth devices? 我应该怎么做才能“打开”蓝牙控制并将我的Raspberry添加到可用的蓝牙设备列表中?

Peace! 和平!

Got the exact same problem. 得到了完全相同的问题。 My solution (which might or might not be fitting for your needs) was to pair the phone via bluetoothctl beforehand. 我的解决方案(可能或可能不适合您的需求)是事先通过bluetoothctl配对手机。 (this already is you why this solution kinda sucks: you can't come and use a different phone/pi the next day, stuff got completely screwed when changing to a different pi :D) Oh and the main script should run as root, else all of this wont work. (这已经是你为什么这个解决方案有点糟透了:第二天你不能来使用不同的手机/ pi,当换到不同的pi时,东西完全搞砸了:D)哦,主脚本应该以root身份运行,否则所有这一切都无法奏效。

1) Pairing your device 1)配对您的设备

$ bluetoothctl
[bluetoothctl] agent on
[bluetoothctl] discoverable on
[bluetoothctl] pairable on
[bluetoothctl] scan on

(now you turn on your bluetooth on your phone and search for devices, in bluetoothctl you should see your device and mac to be shown) (现在你打开手机上的蓝牙并搜索设备,在bluetoothctl你应该看到你的设备和mac显示)

[bluetoothctl] pair XX:XX...(MAC of your phone)

(phone is going to show the "yo this device wants to pair"-dialog and bluetoothctl wants you to confirm the pairing too) (手机将显示“你的设备想要配对”-dialog和bluetoothctl也要你确认配对)

Now you can always connect to the pi via bluetooth, if it is discoverable or not. 现在你可以通过蓝牙连接到pi,如果它是可发现的。 (I'm using Serial Bluetooth Terminal ) (我正在使用串行蓝牙终端

2) Actual talking between devices 2)设备之间的实际通话

NONE of the npm packages supposed to work with bluetooth worked for me. 没有任何与蓝牙配​​合使用的npm软件包对我有用。 Not a single one. 不是一个人。 So in the end I used rfcomm and it's ability to start a program on connection. 所以最后我使用了rfcomm ,它能够在连接上启动程序。 Together with serialport I let rfcomm run node myscript.js , which establishes the actual serial connection like so: serialport一起,我让rfcomm运行node myscript.js ,它建立了实际的串行连接,如下所示:

2.1) rfcomm waiting for connections 2.1) rfcomm等待连接

const rfcommProc = spawn(
    'rfcomm',
    ['watch', 'hci0', '1', 'node', `${__dirname}/myscript.js`]
);

2.2) myscript.js opening the port 2.2) myscript.js打开端口

const port = new SerialPort('/dev/rfcomm0', spError => {
    if (spError) {
        process.send(spError);
    }
})

Look up on the npmjs-page how to receive and send stuff now.(: Hope this gives you some ideas and/or helped. 在npmjs页面上查看如何接收和发送内容。(:希望这会给你一些想法和/或帮助。

3) Note 3)注意

  • The bluetooth service starts quite late. 蓝牙服务启动很晚。 Making a service requiring it didn't do the job for me, but adding it to rc.local seems to be "late enough" to have it running and listening on startup. 提供需要它的服务对我来说不起作用,但是将它添加到rc.local似乎“足够晚”才能让它在启动时运行和监听。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM