bleak icon indicating copy to clipboard operation
bleak copied to clipboard

No disconnect event when interface being removed

Open aryeko opened this issue 4 years ago • 6 comments

  • bleak version: 0.13.0
  • Python version: 3.7.3 and 3.9.8
  • Operating System: raspian
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.58

Description

I'm connecting to an Android device, after few seconds (up to 1 minute) I get a disconnection on my Android device but not on my python app (using bleak).

What I Did

  1. Started BLE scanning with BleakScanner
  2. Started advertising on my Android device (see mac address 76:2D:35:40:73:C7 in the attached log)
  3. BleakScanner found my Android device and connected successfully (see time in the log 2021-11-22 09:11:10.550)
  4. At 2021-11-22 09:11:42,537 (attached log time) Bleak received "InterfaceRemoved" event without disconnection event

The Android device (the peripheral) got a disconnection event with reason 0x13 - "remote user terminated connection".

Bleak didn't called disconnect.

I called disconnect from app side at 2021-11-22 09:11:56.127 (attached log time) and Bleak raised an exception that the device is not connected.

The property client.is_connected returns True before calling disconnect.

log.log

aryeko avatar Nov 22 '21 10:11 aryeko

Sometimes, BlueZ will disconnect if it receives an unexpected packet. For example, I have a device that sends a write response even when doing a write without response and this causes BlueZ to disconnect the device. So you might try logging the Bluetooth packets with Wireshark to see if there is anything unusual there.

dlech avatar Nov 22 '21 16:11 dlech

@dlech thank you for the quick response and for the info. I’ll check bluez logs - can you fire a disconnect event for my case? The device was disconnected after all. I can implement and push PR with some instructions from you. thanks again!

aryeko avatar Nov 22 '21 16:11 aryeko

can you fire a disconnect event for my case? The device was disconnected after all.

Yes, that should be working in Bleak already. I've tested it many times using the disconnection example.

dlech avatar Nov 22 '21 16:11 dlech

I don’t get a disconnection event from Bleak - this is the reason I submitted the issue. you can see it in the attached log.

aryeko avatar Nov 22 '21 16:11 aryeko

Can you supply the code to reproduce the problem?

dlech avatar Nov 22 '21 17:11 dlech

well it is just a UART peripheral using your code example for Bleak as central: uart_service

For the peripheral I'm using Android app - for example nRF connect.

Note that the issue is not reproducing on all the phones, so far I reproduced it with high reproduction rate using Samsung Galaxy S8 or Huawei P40.

Isn't the attached log describe the issue enough? I pinned out the important timesamps in it as I see it.

aryeko avatar Nov 22 '21 18:11 aryeko

The log starts with:

2021-11-22 09:10:51.741 | INFO     | __main__:main:114 - started main - version=0.0.50

Which is clearly not from the uart_service.py example. Please provide a minimal reproducible test case and reopen the issue if this is still a problem.

dlech avatar Sep 03 '22 01:09 dlech