[Raspberry Pi 3B+ Review] Try Bluetooth with pybluez examples[Copy link]
This post was last edited by icefire2012 on 2018-9-21 23:31 This week I learned about the Bluetooth module on 3B+. 1. Understanding the Bluetooth hardware part I found the schematic diagram of 3B+ on the official website of Raspberry Pi (see reference link 1), but only part of it, the Bluetooth chip and the connection interface with the CPU are not shown. I found on another website (see reference link 2) that the Bluetooth of 3B+ uses Cypress's CYW43455. CYW43455 integrates high-performance 802.11ac, low-power Bluetooth 4.2 and classic Bluetooth. The structure diagram of the manual of CYW43455 (see reference link 3) is as follows:
In terms of hardware, the external interface of the Bluetooth part, in addition to a few control signals, is UART, I2S and PWM. I have seen on other websites (I don't remember the source) that the CPU is connected to the UART on the Bluetooth chip and the UART on the IO expansion port (see GPIO14 and GPIO15 in the figure below) are shared, and the two cannot be used at the same time. Later, I will try the UART on the IO expansion port to see if this is the case.
2. Preliminary and simple trial of Bluetooth The trial here is to establish a connection between the Raspberry Pi and the mobile phone through Bluetooth and transmit some characters to each other. I have only made Bluetooth hardware before and don't know the protocol stack. In order to establish a connection between the Raspberry Pi and the mobile phone, I referred to a more mature solution and used the python pybluez library to operate the Bluetooth on the Raspberry Pi. pybluez also provides some simple test routines on github, which can be used directly. 3. Test steps 1. Install pybluez pybluez depends on the software protocol stack bluez under linux and the library file libbluetooth-dev.
sudo apt-get install bluez
复制代码
sudo apt-get install libbluetooth-dev
复制代码
sudo pip install pybluez
复制代码
For php3, it is
sudo pip3 install pybluez
复制代码
The following shows the versions of the various packages I currently have installed.
On my version of the system, bluez comes with the system, and libbluetooth-dev is not installed. When I installed pybluez directly, the following error was reported. Installing libbluetooth-dev first solved it.
2. Here, the Raspberry Pi and the mobile phone communicate using the RFCOMM protocol in Bluetooth. pybluez has provided a simple test routine rfcomm-server.py (see reference link 4). This routine establishes a simple BluetoothSocket Server and then starts listening for connections from other devices. At this time, you can use an Android phone to connect to the Raspberry Pi. If everything goes well, a connection will be established. Next, the Raspberry Pi can receive the data sent by the phone and print it out. Finally, if the connection is disconnected on the phone, the Raspberry Pi server will handle the exception, close the socket, and end the program. Before running the test routine, you need to modify the bluetooth.service file:
The -E -C in the first line in the red box needs to be added. If not, an error bluetooth.btcommon.BluetoothError: (2, 'No such file or directory') will be reported when running the routine. The second line in the red box needs to be added manually. Without this sentence, it will always fail when connecting to the Raspberry Pi on the phone.
ExecStartPost=/usr/bin/sdptool add SP
复制代码
In addition, add a line of code to line 33 of the original rfcomm-server.py file:
Send the received characters back, so that the characters are transmitted to each other. To run the test routine in the terminal, you need to add sudo, otherwise an error will be reported if there is insufficient authority.
sudo python rfcomm-server.py
复制代码
Now you can connect the Raspberry Pi on your mobile phone.
3. Operation on the mobile phone I operated on an Android phone. Because the main protocol used is RFCOMM, I first need to install a Bluetooth serial port software. I found a few on App Store and felt that the following one is more useful. I will paste an icon here.
4. Summary Through the study and understanding of the past few days, a simple communication experiment was carried out between the Raspberry Pi and the Android phone through Bluetooth, and a preliminary understanding of the RFCOMM protocol was obtained. The communication here should still be based on the classic Bluetooth protocol. If there is enough time, I will try to make the Raspberry Pi communicate with a BLE device. 5. Reference link 1.3B+ schematic. https://www.raspberrypi.org/docu ...lus_1p0_reduced.pdf 2. Introduction to Raspberry Pi. http://shumeipai.nxez.com/2018/0 ... bplus-sale-now.html 3. Manual of CYW43455. http://www.cypress.com/file/358916/download 4. Download address of rfcomm-server.py.
The previous steps were successful, but the device created by pybluez cannot be searched using the Bluetooth SPP tool. What should I do?
You can use nrfconnect to see the device created by pybluez.
The version of SPP tool is 7.4.5
Details
Published on 2021-1-3 21:34