我在墙上砸了我的头,因为我不明白为什么如果我使用脚本、python 甚至单行命令,只能在第一次运行时工作,然后在以下执行时无法再次连接到设备.
在 Raspberry Pi 4 上运行,带有最新的 Raspberry OS 和 bluez 堆栈。
手动执行
如果我手动输入 gatttool 交互,每次都可以正常工作。
pi@sp-pi002:~ $ sudo gatttool -i hci0 -b 02:10:32:20:00:AA -I <br>
[02:10:32:20:00:AA][LE]> connect <br>
Attempting to connect to 02:10:32:20:00:AA <br>
Connection successful <br>
[02:10:32:20:00:AA][LE]> char-write-req 0x0123 01126338221102234106DE <br>
Characteristic value was written successfully <br>
[02:10:32:20:00:AA][LE]> char-write-req 0x0123 0112633822110223410621 <br>
Characteristic value was written successfully <br>
[02:10:32:20:00:AA][LE]> disconnect <br>
[02:10:32:20:00:AA][LE]> exit <br>
pi@sp-pi002:~ $ <br>
“流水线”执行
非常第一次工作正常,但以下没有。即使我在脚本执行后尝试手动执行,也不行。
pi@sp-pi002:~ $ (sleep 1; echo "connect"; sleep 4; echo "char-write-req 0x0123 01126338221102234106DE"; sleep 10; echo "char-write-req 0x0123 0112633822110223410621"; sleep 1; echo "disconnect"; sleep 1; echo "exit"; echo "sudo hciconfig hci0 reset") | sudo gatttool -i hci0 -b 02:10:32:20:00:AAA -I
[02:10:32:20:00:AA][LE]> connect
Attempting to connect to 02:10:32:20:00:AA
Connection successful
[02:10:32:20:00:AA][LE]> char-write-req 0x0123 01126338221102234106DE
Characteristic value was written successfully
[02:10:32:20:00:AA][LE]> char-write-req 0x0123 0112633822110223410621
Characteristic value was written successfully
[02:10:32:20:00:AA][LE]> disconnect
[02:10:32:20:00:AA][LE]> exit
pi@sp-pi002:~ $ (sleep 1; echo "connect"; sleep 4; echo "char-write-req 0x0123 01126338221102234106DE"; sleep 10; echo "char-write-req 0x0123 0112633822110223410621"; sleep 1; echo "disconnect"; sleep 1; echo "exit"; echo "sudo hciconfig hci0 reset") | sudo gatttool -i hci0 -b 02:10:32:20:00:AA -I
[02:10:32:20:00:AA][LE]> connect
Attempting to connect to 01:02:03:04:05:AA
[02:10:32:20:00:AA][LE]> char-write-req 0x0123 01126338221102234106DE
Command Failed: Disconnected
[02:10:32:20:00:AA][LE]> char-write-req 0x0123 0112633822110223410621
Command Failed: Disconnected
[02:10:32:20:00:AA][LE]> disconnect
[02:10:32:20:00:AA][LE]> exit
pi@sp-pi002:~ $
Python中的预期
与“流水线”相同,第一次可以正常工作,但接下来的不行。即使我在脚本执行后尝试手动执行,也不行。
import pexpect
import time
device_no = "hci0"
mac_str = "02:10:32:20:00:AA"
cmd = pexpect.spawn('sudo gatttool -i ' + device_no + ' -b ' + mac_str + ' -I')
cmd.expect('\[LE\]>')
cmd.sendline('connect')
cmd.expect('Connection successful')
cmd.sendline('char-write-req 0x0123 01126338221102234106DE')
cmd.expect('Characteristic value was written successfully')
time.sleep(10)
cmd.sendline('char-write-req 0x0123 0112633822110223410621')
cmd.expect('Characteristic value was written successfully')
cmd.sendline('disconnect')
cmd.expect('\[LE\]>')
cmd.sendline('exit')
最后的话
我迷路了。我什至尝试在 scritps/python 上使用 bluetoothctl 而不是 gatttool,但结果始终相同:第一次工作,而不是下一次工作(直到 BLE 设备重置连接)。我很困惑为什么通过手动执行任务每次都可以工作,而不是脚本。我不是这些主题的专家,但也不是新手。任何帮助将不胜感激!
更新 1
Bluez 与 Python 中的 D-bus API
我仍然得到与其他选项相同的结果(第一次尝试有效,但下一次无效)
import pydbus
from gi.repository import GLib
from time import sleep
dev_id = '02:10:32:20:00:AA'
lock_uuid = '0000acbff2-0000-1000-8000-00815ffb3wfb'
bluez_service = 'org.bluez'
adapter_path = '/org/bluez/hci0'
device_path = f"{adapter_path}/dev_{dev_id.replace(':', '_')}"
bus = pydbus.SystemBus()
adapter = bus.get(bluez_service, adapter_path)
device = bus.get(bluez_service, device_path)
device.Connect()
mngr = bus.get(bluez_service, '/')
def get_characteristic_path(dev_path, uuid):
mng_objs = mngr.GetManagedObjects()
for path in mng_objs:
chr_uuid = mng_objs[path].get('org.bluez.GattCharacteristic1', {}).get('UUID')
if path.startswith(dev_path) and chr_uuid == uuid:
return path
lock_uuid_path = get_characteristic_path(device._path, lock_uuid)
lock = bus.get(bluez_service, lock_uuid_path)
new_value = bytearray([0x01, 0x20, 0x60, 0x08, 0x02, 0x01, 0x02, 0x02, 0x01, 0x06, 0xDE])
lock.WriteValue(new_value, {})
sleep(10)
new_value = bytearray([0x01, 0x20, 0x60, 0x08, 0x02, 0x01, 0x02, 0x02, 0x01, 0x06, 0x21])
lock.WriteValue(new_value, {})
device.Disconnect()
蓝牙
第一次执行显示连接活动,第二次不显示任何活动。
[bluetooth]#
[CHG] Device 02:10:32:20:00:AA Connected: yes
[CHG] Device 02:10:32:20:00:AA ServicesResolved: yes
[CHG] Device 02:10:32:20:00:AA ServicesResolved: no
[CHG] Device 02:10:32:20:00:AA Connected: no
[bluetooth]#
比特币
删除了一些与“LE Set Scan Enable”相关的部分,以减少讲座。
pi@sp-pi002:~ $ sudo btmon
Bluetooth monitor ver 5.50
= Note: Linux version 5.4.72-v7l+ (armv7l) 0.069236
= Note: Bluetooth subsystem version 2.22 0.069246
= New Index: DC:A6:32:0A:0D:AB (Primary,UART,hci0) [hci0] 0.069251
= Open Index: DC:A6:32:0A:0D:AB [hci0] 0.069255
= Index Info: DC:A6:32:0A:0D:AB (Cypress Semiconductor Corporation) [hci0] 0.069258
@ MGMT Open: bluetoothd (privileged) version 1.14 {0x0001} 0.069263
@ MGMT Open: btmon (privileged) version 1.14 {0x0002} 0.069757
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 #1 [hci0] 19.298233
Type: Passive (0x00)
Interval: 60.000 msec (0x0060)
Window: 30.000 msec (0x0030)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4 #2 [hci0] 19.298536
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 #3 [hci0] 19.298580
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4 #4 [hci0] 19.298942
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 40 #5 [hci0] 19.304392
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 02:10:32:20:00:AA (OUI 02-10-32)
Data length: 28
Company: CAEN RFID srl (170)
Data: 20321002
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Name (complete): Kino_BLE
16-bit Service UUIDs (partial): 1 entry
Unknown (0xf9f7)
TX power: 8 dBm
RSSI: -79 dBm (0xb1)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 #6 [hci0] 19.304433
Scanning: Disabled (0x00)
Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4 #7 [hci0] 19.305665
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
< HCI Command: LE Create Connection (0x08|0x000d) plen 25 #8 [hci0] 19.305700
Scan interval: 60.000 msec (0x0060)
Scan window: 60.000 msec (0x0060)
Filter policy: White list is not used (0x00)
Peer address type: Public (0x00)
Peer address: 02:10:32:20:00:AA (OUI 02-10-32)
Own address type: Public (0x00)
Min connection interval: 20.00 msec (0x0010)
Max connection interval: 40.00 msec (0x0020)
Connection latency: 0 (0x0000)
Supervision timeout: 6000 msec (0x0258)
Min connection length: 0.000 msec (0x0000)
Max connection length: 0.000 msec (0x0000)
> HCI Event: Command Status (0x0f) plen 4 #9 [hci0] 19.306212
LE Create Connection (0x08|0x000d) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 19 #10 [hci0] 19.413260
LE Connection Complete (0x01)
Status: Success (0x00)
Handle: 64
Role: Master (0x00)
Peer address type: Public (0x00)
Peer address: 02:10:32:20:00:AA (OUI 02-10-32)
Connection interval: 37.50 msec (0x001e)
Connection latency: 0 (0x0000)
Supervision timeout: 6000 msec (0x0258)
Master clock accuracy: 0x00
@ MGMT Event: Device Connected (0x000b) plen 41 {0x0002} [hci0] 19.413286
LE Address: 02:10:32:20:00:AA (OUI 02-10-32)
Flags: 0x00000000
Data length: 28
Company: CAEN RFID srl (170)
Data: 20321002
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Name (complete): Kino_BLE
16-bit Service UUIDs (partial): 1 entry
Unknown (0xf9f7)
TX power: 8 dBm
@ MGMT Event: Device Connected (0x000b) plen 41 {0x0001} [hci0] 19.413286
LE Address: 02:10:32:20:00:AA (OUI 02-10-32)
Flags: 0x00000000
Data length: 28
Company: CAEN RFID srl (170)
Data: 20321002
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Name (complete): Kino_BLE
16-bit Service UUIDs (partial): 1 entry
Unknown (0xf9f7)
TX power: 8 dBm
< HCI Command: LE Read Remote Used Features (0x08|0x0016) plen 2 #11 [hci0] 19.413395
Handle: 64
> HCI Event: Command Status (0x0f) plen 4 #12 [hci0] 19.415301
LE Read Remote Used Features (0x08|0x0016) ncmd 1
Status: Success (0x00)
> HCI Event: Command Complete (0x0e) plen 14 #13 [hci0] 19.415306
LE Read Remote Used Features (0x08|0x0016) ncmd 1
Status: Success (0x00)
00 00 00 00 00 00 00 00 00 00 ..........
> ACL Data RX: Handle 64 flags 0x02 dlen 16 #14 [hci0] 19.505778
LE L2CAP: Connection Parameter Update Request (0x12) ident 1 len 8
Min interval: 16
Max interval: 32
Slave latency: 0
Timeout multiplier: 600
> HCI Event: LE Meta Event (0x3e) plen 12 #15 [hci0] 19.542823
LE Read Remote Used Features (0x04)
Status: Success (0x00)
Handle: 64
Features: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
LE Encryption
< HCI Command: LE Start Encryption (0x08|0x0019) plen 28 #16 [hci0] 19.542873
Handle: 64
Random number: 0x94d43ce6d560a901
Encrypted diversifier: 0xed02
Long term key: d25d80004dd15ab09454000000000000
@ MGMT Event: New Connection Parameter (0x001c) plen 16 {0x0002} [hci0] 19.542899
Store hint: Reserved (0xaa)
LE Address: 01:02:10:32:20:00 (OUI 01-02-10)
Min connection interval: 16
Max connection interval: 32
Connection latency: 0 (0x0000)
Supervision timeout: 600
@ MGMT Event: New Connection Parameter (0x001c) plen 16 {0x0001} [hci0] 19.542899
Store hint: Reserved (0xaa)
LE Address: 01:02:10:32:20:00 (OUI 01-02-10)
Min connection interval: 16
Max connection interval: 32
Connection latency: 0 (0x0000)
Supervision timeout: 600
< ACL Data TX: Handle 64 flags 0x00 dlen 10 #17 [hci0] 19.542922
LE L2CAP: Connection Parameter Update Response (0x13) ident 1 len 2
Result: Connection Parameters accepted (0x0000)
> HCI Event: Command Status (0x0f) plen 4 #18 [hci0] 19.543653
LE Start Encryption (0x08|0x0019) ncmd 1
Status: Success (0x00)
< HCI Command: LE Connection Update (0x08|0x0013) plen 14 #19 [hci0] 19.543683
Handle: 64
Min connection interval: 20.00 msec (0x0010)
Max connection interval: 40.00 msec (0x0020)
Connection latency: 0 (0x0000)
Supervision timeout: 6000 msec (0x0258)
Min connection length: 0.000 msec (0x0000)
Max connection length: 0.000 msec (0x0000)
> HCI Event: Command Status (0x0f) plen 4 #20 [hci0] 19.544008
LE Connection Update (0x08|0x0013) ncmd 1
Status: Success (0x00)
> HCI Event: Encryption Change (0x08) plen 4 #21 [hci0] 19.842763
Status: Success (0x00)
Handle: 64
Encryption: Enabled with AES-CCM (0x01)
< HCI Command: Write Authenticated Payload Timeout (0x03|0x007c) plen 4 #22 [hci0] 19.842808
Handle: 64
Timeout: 30000 msec (0x0bb8)
< ACL Data TX: Handle 64 flags 0x00 dlen 7 #23 [hci0] 19.843152
ATT: Exchange MTU Request (0x02) len 2
Client RX MTU: 517
> HCI Event: Command Complete (0x0e) plen 6 #24 [hci0] 19.844110
Write Authenticated Payload Timeout (0x03|0x007c) ncmd 1
Status: Success (0x00)
Handle: 64
> HCI Event: Number of Completed Packets (0x13) plen 5 #25 [hci0] 19.955359
Num handles: 1
Handle: 64
Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 7 #26 [hci0] 19.992707
ATT: Exchange MTU Response (0x03) len 2
Server RX MTU: 23
< ACL Data TX: Handle 64 flags 0x00 dlen 7 #27 [hci0] 19.993242
ATT: Read Request (0x0a) len 2
Handle: 0x0007
> ACL Data RX: Handle 64 flags 0x02 dlen 13 #28 [hci0] 20.067764
ATT: Read Response (0x0b) len 8
Value: 4b696e6f5f424c45
< ACL Data TX: Handle 64 flags 0x00 dlen 7 #29 [hci0] 20.068282
ATT: Read Request (0x0a) len 2
Handle: 0x0009
> HCI Event: Number of Completed Packets (0x13) plen 5 #30 [hci0] 20.105371
Num handles: 1
Handle: 64
Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 7 #31 [hci0] 20.157731
ATT: Read Response (0x0b) len 2
Value: 0000
< ACL Data TX: Handle 64 flags 0x00 dlen 11 #32 [hci0] 20.158028
ATT: Read By Group Type Request (0x10) len 6
Handle range: 0x0001-0xffff
Attribute group type: Primary Service (0x2800)
> HCI Event: LE Meta Event (0x3e) plen 10 #33 [hci0] 20.158007
LE Connection Update Complete (0x03)
Status: Success (0x00)
Handle: 64
Connection interval: 37.50 msec (0x001e)
Connection latency: 0 (0x0000)
Supervision timeout: 6000 msec (0x0258)
> ACL Data RX: Handle 64 flags 0x02 dlen 24 #34 [hci0] 20.233300
ATT: Read By Group Type Response (0x11) len 19
Attribute data length: 6
Attribute group list: 3 entries
Handle range: 0x0001-0x0004
UUID: Generic Attribute Profile (0x1801)
Handle range: 0x0005-0x000b
UUID: Generic Access Profile (0x1800)
Handle range: 0x000c-0x0011
UUID: Unknown (0xf9f7)
< ACL Data TX: Handle 64 flags 0x00 dlen 11 #35 [hci0] 20.233587
ATT: Read By Group Type Request (0x10) len 6
Handle range: 0x0012-0xffff
Attribute group type: Primary Service (0x2800)
> HCI Event: Number of Completed Packets (0x13) plen 5 #36 [hci0] 20.270400
Num handles: 1
Handle: 64
Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 9 #37 [hci0] 20.307761
ATT: Error Response (0x01) len 4
Read By Group Type Request (0x10)
Handle: 0x0012
Error: Attribute Not Found (0x0a)
< ACL Data TX: Handle 64 flags 0x00 dlen 9 #38 [hci0] 20.321380
ATT: Write Request (0x12) len 4
Handle: 0x0004
Data: 0200
> ACL Data RX: Handle 64 flags 0x02 dlen 5 #39 [hci0] 20.382690
ATT: Write Response (0x13) len 0
< ACL Data TX: Handle 64 flags 0x00 dlen 18 #40 [hci0] 20.382970
ATT: Write Request (0x12) len 13
Handle: 0x0011
Data: 01106008000102000106de
> HCI Event: Number of Completed Packets (0x13) plen 5 #41 [hci0] 20.420453
Num handles: 1
Handle: 64
Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 5 #42 [hci0] 20.457713
ATT: Write Response (0x13) len 0
< ACL Data TX: Handle 64 flags 0x00 dlen 18 #43 [hci0] 30.974587
ATT: Write Request (0x12) len 13
Handle: 0x0011
Data: 0110600700010200010621
> ACL Data RX: Handle 64 flags 0x02 dlen 5 #44 [hci0] 31.032837
ATT: Write Response (0x13) len 0
> HCI Event: Number of Completed Packets (0x13) plen 5 #45 [hci0] 31.147088
Num handles: 1
Handle: 64
Count: 1
@ MGMT Command: Disconnect (0x0014) plen 7 {0x0001} [hci0] 33.220115
LE Address: 02:10:32:20:00:AA (OUI 02-10-32)
< HCI Command: Disconnect (0x01|0x0006) plen 3 #46 [hci0] 33.220187
Handle: 64
Reason: Remote User Terminated Connection (0x13)
> HCI Event: Command Status (0x0f) plen 4 #47 [hci0] 33.221091
Disconnect (0x01|0x0006) ncmd 1
Status: Success (0x00)
> HCI Event: Disconnect Complete (0x05) plen 4 #48 [hci0] 33.245957
Status: Success (0x00)
Handle: 64
Reason: Connection Terminated By Local Host (0x16)
@ MGMT Event: Command Complete (0x0001) plen 10 {0x0001} [hci0] 33.246009
Disconnect (0x0014) plen 7
Status: Success (0x00)
LE Address: 02:10:32:20:00:AA (OUI 02-10-32)
@ MGMT Event: Device Disconnected (0x000c) plen 8 {0x0002} [hci0] 33.246030
LE Address: 02:10:32:20:00:AA (OUI 02-10-32)
Reason: Connection terminated by local host (0x02)
....................... END FIRST RUN OF SCRIPT.
....................... START OF SECOND EXECUTION.
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 #49 [hci0] 56.180508
Type: Passive (0x00)
Interval: 60.000 msec (0x0060)
Window: 30.000 msec (0x0030)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4 #50 [hci0] 56.180811
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 #51 [hci0] 56.180838
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4 #52 [hci0] 56.181207
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 43 #53 [hci0] 56.204213
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 0D:48:77:25:AE:79 (Non-Resolvable)
Data length: 31
Company: Microsoft (6)
Data: 01092002c67df605ed810a2e6f7e147cffa153daafbc76cd6c9459
RSSI: -77 dBm (0xb3)
...
...
... Many others LE Advertising Report (0x02)
...
...
> HCI Event: LE Meta Event (0x3e) plen 26 #76 [hci0] 89.682320
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: A4:83:E7:03:6E:75 (Apple, Inc.)
Data length: 14
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 401c9671a8
RSSI: -91 dBm (0xa5)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 #77 [hci0] 96.907783
Scanning: Disabled (0x00)
Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4 #78 [hci0] 96.908745
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)