I have connected a device by bluetooth to send data, but the code hangs at the statement:
client, address = server.accept()
This is the code
import bluetooth
#00:07:80:E0:A4:FC
host = ""
port = 1
server = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('Bluetooth Socket Created')
try:
server.bind((host, port))
print("Bluetooth Binding Completed")
except:
print("Bluetooth Binding Failed")
server.listen(1)
try:
client, address = server.accept()
print("Connected to: ", address)
print("Client: ", client)
except Exception as e:
print (str(e))
try:
while True:
data = client.recv(1)
print(data)
client.send(data)
except:
client.close()
server.close()
The device is connected:
When pairing the pwd from the sending device was requested.
When I do ctrl-c I get the following message:
Also: everything works fine when connecting with an Android phone and using the serial BT app
Content of /etc/bluetooth/rfcomm.conf is:
rfcomm1 {
# Automatically bind the device at startup
bind yes;
# Bluetooth address of the device
device 00:07:80:E0:A4:FC
#xx:xx:xx:xx:xx:xx;
# RFCOMM channel for the connection
channel 1;
# Description of the connection
comment "Soehnle Bluetooth Connection";
}
Also the content of /lib/systemd/system/bluetooth.service:
[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth
[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full
[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service
Bluetoothctl output as code:
WT11i-A]# info
Device 00:07:80:E0:A4:FC (public)
Name: WT11i-A
Alias: WT11i-A
Class: 0x00001f00
Paired: yes
Trusted: yes
Blocked: no
Connected: yes
LegacyPairing: no
UUID: Serial Port (00001101-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
Modalias: bluetooth:v0047pF000d0400
RFCOMM command output as code:
pi@raspberrypi:~ $ sudo rfcomm -a show hci0
rfcomm0: B8:27:EB:A6:D7:4F -> 00:07:80:E0:A4:FC channel 1 closed [reuse-dlc release-on-hup tty-attached]
rfcomm1: B8:27:EB:A6:D7:4F -> 00:07:80:E0:A4:FC channel 1 connected [reuse-dlc release-on-hup tty-attached]
ctrl-c output as code:
pi@raspberrypi:~ $ sudo python bluetoothtest4.py
Bluetooth Socket Created
Bluetooth Binding Completed
^CTraceback (most recent call last):
File "bluetoothtest4.py", line 18, in <module>
client, address = server.accept()
File "/usr/lib/python3/dist-packages/bluetooth/bluez.py", line 167, in accept
client, addr = self._sock.accept ()
KeyboardInterrupt
systemctl output as code:
pi@raspberrypi:~ $ systemctl status bluetooth
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-11-21 12:51:21 CET; 2 days ago
Docs: man:bluetoothd(8)
Main PID: 734 (bluetoothd)
Status: "Running"
Tasks: 1 (limit: 2059)
CGroup: /system.slice/bluetooth.service
└─734 /usr/lib/bluetooth/bluetoothd
Nov 21 12:51:21 raspberrypi bluetoothd[734]: Bluetooth daemon 5.50
Nov 21 12:51:21 raspberrypi systemd[1]: Started Bluetooth service.
Nov 21 12:51:21 raspberrypi bluetoothd[734]: Starting SDP server
Nov 21 12:51:21 raspberrypi bluetoothd[734]: Bluetooth management interface 1.18 initialized
Nov 21 12:51:21 raspberrypi bluetoothd[734]: Sap driver initialization failed.
Nov 21 12:51:21 raspberrypi bluetoothd[734]: sap-server: Operation not permitted (1)
Nov 21 12:51:21 raspberrypi bluetoothd[734]: Failed to set privacy: Rejected (0x0b)
Nov 21 12:51:23 raspberrypi bluetoothd[734]: Endpoint registered: sender=:1.28 path=/MediaEndpoint/A2DPSource
Nov 21 12:51:23 raspberrypi bluetoothd[734]: Endpoint registered: sender=:1.28 path=/MediaEndpoint/A2DPSink
Nov 23 11:36:42 raspberrypi systemd[1]: bluetooth.service: Current command vanished from the unit file, execution of the command list won't be resumed.
btmon output as code:
@ RAW Open: blueman-manager version 2.22 {0x0004} 986.383469
@ RAW Close: blueman-manager {0x0004} 986.383531
< HCI Command: Read RSSI (0x05|0x0005) plen 2 #5905 [hci0] 986.385681
Handle: 12
> HCI Event: Command Complete (0x0e) plen 7 #5906 [hci0] 986.386072
Read RSSI (0x05|0x0005) ncmd 1
Status: Success (0x00)
Handle: 12
RSSI: 0 dBm (0x00)
< HCI Command: Read Link Quality (0x05|0x0003) plen 2 #5907 [hci0] 986.386278
Handle: 12
> HCI Event: Command Complete (0x0e) plen 7 #5908 [hci0] 986.386591
Read Link Quality (0x05|0x0003) ncmd 1
Status: Success (0x00)
Handle: 12
Link quality: 0x8c
< HCI Command: Read Transmit Power Level (0x03|0x002d) plen 3 #5909 [hci0] 986.386755
Handle: 12
Type: Current Transmit Power Level (0x00)
> HCI Event: Command Complete (0x0e) plen 7 #5910 [hci0] 986.387069
Read Transmit Power Level (0x03|0x002d) ncmd 1
Status: Success (0x00)
Handle: 12
TX power: 12 dbm (0x0c)
systemctl status bluetooth
, and also before executing your python code, runssudo btmon
, so we can further debug. In the meantime, I will provide you an example that might help you get data without python, and provide you one example with DBUS.