0

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:

enter image description here

enter image description here

enter image description here

When pairing the pwd from the sending device was requested.

When I do ctrl-c I get the following message: enter image description here

Also: everything works fine when connecting with an Android phone and using the serial BT app

systemctl status bluetooth: enter image description here

sudo btmon: enter image description here

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)
mortpiedra
  • 687
  • 4
  • 13
  • Add the output of `systemctl status bluetooth`, and also before executing your python code, runs `sudo 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. – Mr. Panda Nov 22 '21 at 21:39
  • Thanx for your answer Mr Panda. I know nothing about BT nor Python, so please excuse my ignorance: I ran 'sudo btmon' then my code from Thonny. The output from 'sudo btmon' did not seem to change whilst my code ran. – mortpiedra Nov 23 '21 at 10:53

1 Answers1

0

I believe your main problem is you do not have a matching profile in your client with what your device (server) is offering.

BlueZ is the official Linux Bluetooth protocol stack which is based on D-Bus API. Hence, if you would like to build a solid python code, I suggest you to have a look at it.

The rfcoom tool is also deprecated, however it does still do the work.

The steps that work for me with rfcomm are:

bluetoothctl
scan on // wait until your device MAC appears
scan off
pair 00:07:80:E0:A4:FC

In this step it might ask for a pin, in my case I was using an Arduino as server, the default one was either 0000 or 1234. Then:

trust  00:07:80:E0:A4:FC // so you do not need to pair everytime
quit

Now we connect via:

sudo rfcomm connect hci0 00:07:80:E0:A4:FC

Replace hci0 with name of your own Bluetooth adapter.

For D-Bus example please see this discussion. @ukBaz provides nice explanation with an example code. You may need scanning if your device is not discovered, you can find that in my code and add it.

Mr. Panda
  • 485
  • 3
  • 14
  • I did not know that rfcomm is deprecated. There seem to be so many options out there and I am monkey-see-monkey-do trying around. I definitively want a solid solution and did take a quick look at your solution and the D-Bus API. For me as a monkey it seems as quite a beast though, so I will have to approach it step by step. – mortpiedra Nov 23 '21 at 10:58
  • I donot really understand 'I believe your main problem is you do not have a matching profile in your client with what your device (server) is offering'??? I am connected it seems - same result when I follow the steps you suggested above - and there is indeed a pwd pop-up where I enter `0000`. Running the command `sudo rfcomm -a show hci0` shows that rfcomm1 is connected. – mortpiedra Nov 23 '21 at 11:41
  • @mortpiedra, what is your device that acts as a server? Also please put those outputs as code, not as image. – Mr. Panda Nov 23 '21 at 14:02
  • the server is a RPi 3 (Model B Plus Rev 1.3). I have added the output as code above (in case this is not what you meant please get back, thanx). – mortpiedra Nov 23 '21 at 14:40
  • I could add the following info: in the screenshot above showing the device 'WT11i-A' the circled (in blue) transmission values (18.94b/s etc.) shoot up to values of several 100 b/s whenever I push the (physical) 'send' button on the device. – mortpiedra Nov 23 '21 at 16:20
  • @ukBaz Mr.Panda (and many others) refer to you as the authority on BT issues. Would you care to take a look at this problem? – mortpiedra Nov 24 '21 at 10:39