Adam-Langley / pybleno

A direct port of the Bleno bluetooth LE peripheral role library to Python2/3
MIT License
68 stars 34 forks source link

Error in the characteristic #12

Closed Buzda closed 6 years ago

Buzda commented 6 years ago

Hi Adam, I have looking for a library that has the peripheral role, and finally found your library. I have tried the battery example, but the when I ran it I got the error below which I think requires lower level processing:

Hit to disconnect Exception in thread HCISocketPoller: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 754, in run self.target(*self.args, **self.__kwargs) File "/home/pi/.local/lib/python2.7/site-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 182, in _socket_poller self._socket_on_started() File "/home/pi/.local/lib/python2.7/site-packages/pybleno/hci_socket/Hci.py", line 572, in on_socket_started self.setEventMask() File "/home/pi/.local/lib/python2.7/site-packages/pybleno/hci_socket/Hci.py", line 78, in setEventMask self.write(cmd) File "/home/pi/.local/lib/python2.7/site-packages/pybleno/hci_socket/Hci.py", line 330, in write self._socket.write(pkt) File "/home/pi/.local/lib/python2.7/site-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 273, in write self.hci.write_buffer(data) File "/home/pi/.local/lib/python2.7/site-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 163, in write_buffer self._socket.send(data) error: [Errno 1] Operation not permitted

Adam-Langley commented 6 years ago

Hi Buzda, can you please try running with sudo? It looks like a a socket permission issue. If that doesn't work, try the same sample in the bleno library (nodejs). That should give us more information to work off of.

ghost commented 6 years ago

Hi Adam, I use virtualenv to run as root, so sudo is not expected to use. I install pyleno in virtualenv too. And I met the same problem. With sudo, it can work, but my customer expect it running in virtualenv Exception in thread HCISocketPoller: Traceback (most recent call last): File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner self.run() File "/usr/lib/python3.4/threading.py", line 868, in run self._target(*self._args, **self._kwargs) File "/srv/cloud/venv-cloud/lib/python3.4/site-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 182, in _socket_poller self._socket_on_started() File "/srv/cloud/venv-cloud/lib/python3.4/site-packages/pybleno/hci_socket/Hci.py", line 572, in on_socket_started self.setEventMask() File "/srv/cloud/venv-cloud/lib/python3.4/site-packages/pybleno/hci_socket/Hci.py", line 78, in setEventMask self.write(cmd) File "/srv/cloud/venv-cloud/lib/python3.4/site-packages/pybleno/hci_socket/Hci.py", line 330, in write self._socket.write(pkt) File "/srv/cloud/venv-cloud/lib/python3.4/site-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 273, in write self.hci.write_buffer(data) File "/srv/cloud/venv-cloud/lib/python3.4/site-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 163, in write_buffer self._socket.send(data) PermissionError: [Errno 1] Operation not permitted

Adam-Langley commented 6 years ago

Hi everyone,

The bluetooth-socket code was lifted from Mike Ryans project work: https://github.com/mjg59/python-decora

That page will be the best location for you to begin your investigations.

Adam-Langley commented 6 years ago

Hi again, After doing a lot os searching - it simply appears that SOCK_RAW requires superuser privileges. This is just a limitation of the library as it stands. Feel free to submit an alternative bluetooth subsystem. One option would be to take a look at the bluetooth socket API changes made in Python3, and utilise those instead:

http://blog.kevindoran.co/bluetooth-programming-with-python-3/

Thanks,

ghost commented 6 years ago

Hi Adam, I just found that if :

  1. bluetooth Service is enabled and start, 2.also remove experimental mode from bluetooth Service,
  2. make sure hci0 piscan and up pybleno runs without socket timeout error. It is totally opposite with what bleno setting and bluez installation said , but works
hpsaturn commented 6 years ago

Hi, I had the same issue on RaspberryPi3 running raspbian stretch, the gatt service starting well but when I run read o subscriptions actions from my Android Device (nRF Connect app), the python3 service go to crash:

(master) pi@raspimc:~/pybleno/examples/batteryservice $ sudo python3 main.py 
Hit <ENTER> to disconnect
on -> stateChange: poweredOn
on -> advertisingStart: success
setServices: success
Exception in thread HCISocketPoller:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/BluetoothHCI/BluetoothHCI.py", line 197, in _socket_poller
    self._socket_on_data_user_callback(bytearray(data))
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Hci.py", line 417, in onSocketData
    self.emit('aclDataPkt', [handle, cid, pktData])
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Emit.py", line 15, in emit
    handler(*arguments)
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Bindings.py", line 167, in onAclDataPkt
    self._aclStream.push(cid, data)
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/AclStream.py", line 18, in push
    self.emit('data', [cid, data])
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Emit.py", line 15, in emit
    handler(*arguments)
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Gatt.py", line 255, in onAclStreamData
    self.handleRequest(data)
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Gatt.py", line 299, in handleRequest
    response = self.handleReadOrReadBlobRequest(request)
  File "/usr/local/lib/python3.5/dist-packages/pybleno/hci_socket/Gatt.py", line 647, in handleReadOrReadBlobRequest
    handle = self._handles[valueHandle]
KeyError: 48

^CTraceback (most recent call last):
  File "main.py", line 34, in <module>
    input()
KeyboardInterrupt

The solution from @AndyChoon not works for me. Thanks.

ghost commented 6 years ago

@hpsaturn It is recommended to use LightBlue Explore on Android or BLE scanner on IOS. I don't know your error, it seems different.

Adam-Langley commented 6 years ago

Hi @hpsaturn, I have commited a few fixes to Gatt (0d95210477b0721151e169e75add2f179db0d816) which should resolve your issue. Please pull head, and let me know how this goes. Thanks

ArMouReR commented 4 years ago

@AndyChoon Can you please elaborate how did you run pybleno without root ? I am trying to repeat your suggestions, but without too much success for now: My system: RaspberyPi 3a. I have started Bluetoth service with: sudo service bluetooth start sudo service bluetooth status: `● bluetooth.service - Bluetooth service Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-03-31 19:51:32 EDT; 4s ago Docs: man:bluetoothd(8) Main PID: 6378 (bluetoothd) Status: "Running" Tasks: 1 (limit: 860) Memory: 704.0K CGroup: /system.slice/bluetooth.service └─6378 /usr/lib/bluetooth/bluetoothd

Mar 31 19:51:32 raspberrypi3 systemd[1]: Starting Bluetooth service... Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: Bluetooth daemon 5.50 Mar 31 19:51:32 raspberrypi3 systemd[1]: Started Bluetooth service. Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: Starting SDP server Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: Bluetooth management interface 1.14 initialized Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: Sap driver initialization failed. Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: sap-server: Operation not permitted (1) Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: Endpoint registered: sender=:1.32 path=/org/bluez/hci0/A2DP/SBC/Source/1 Mar 31 19:51:32 raspberrypi3 bluetoothd[6378]: Endpoint registered: sender=:1.32 path=/org/bluez/hci0/A2DP/SBC/Source/2`

Also started HC0 with: sudo hciconfig hci0 piscan

hci0: Type: Primary Bus: UART BD Address: B8:XX:XX:XX:XX:XX ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING PSCAN ISCAN RX bytes:4117 acl:0 sco:0 events:281 errors:0 TX bytes:17097 acl:0 sco:0 commands:281 errors:0

But I am still getting the same error as you and topic starter.

Any help will be really appreciated....

ghost commented 4 years ago

@ArMouReR Hi It is quite long time ago. Currently, I use the bluetooth --experimental in bluetooth Service, it makes "just bond" works. I suggest you to add that feature. I run the pybleno in virtual environment with root access. That is like sudo virtual_env/cloud/bin/python3 xxxxxxperipheral.py in which the peripheral.py used pybleno. I managed this call to script in supervisorctl since device start.

ArMouReR commented 4 years ago

@AndyChoon Thanks a lot for your reply ! I have done the same: starting python with full path to virtual env and sudo and it do works.

Many many thanks for your clarification !!!