- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用头撞墙,因为我不明白为什么如果我使用脚本、python 甚至单行命令,只在第一次运行时有效,然后它无法再次连接到设备以下执行。
在 Raspberry Pi 4 上运行,配备最新的 Raspberry 操作系统和 bluez 堆栈。
如果我手动输入 gatttool interactive,每次都可以正常工作。
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:~ $
与“Pipelined”相同,第一次工作正常,但接下来的工作就不行了。即使我尝试在脚本执行后手动执行此操作,也不行。
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 设备重置连接)。我很困惑为什么通过手动执行任务每次都可以工作,而不是使用脚本。我不是这些主题的专业人士,但也不是新手。我们将不胜感激!
我仍然得到与其他选项相同的结果(在第一次尝试时有效,但在接下来的尝试中无效)
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)
最佳答案
您可以通过在不同的终端中打开 bluetoothctl
来获取更多调试信息。这将告诉您在脚本运行时蓝牙守护进程正在做什么。
service bluetooth status
经常会在报告底部的信息部分出现错误。
在运行脚本时,可以在单独的终端中运行 sudo btmon
查看非常低级别的报告信息。
gatttool
是 tools that has been deprecated 之一由 BlueZ 提供。
而且我不认为 bluetoothctl
旨在以这种方式使用。 BlueZ 开发人员经常更改 bluetoothctl
,如果您让它正常工作,这将破坏您的脚本。
BlueZ 为程序提供 D-Bus API 以与蓝牙守护进程交互。
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/gatt-api.txt
要使用此 API,您需要了解:
The BlueZ D-Bus service is 'org.bluez'
The adapter device has the D-Bus object path of '/org/bluez/hci0' typically
The DBus interface for the adapter is 'org.bluez.Adapter1'
下面问题的回复中有一个例子: https://raspberrypi.stackexchange.com/q/114150/121848
关于python - 如果在脚本或 python 上使用,蓝牙 BLE Gatttool 无法重复执行,但如果手动使用,它可以正常工作。 (树莓派 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64867253/
我是一名优秀的程序员,十分优秀!