Read/parse 来自 BTMON 的原生流式输出 bash?
Read/parse streamed output from BTMON in native bash?
我正在尝试实时解析来自 btmon
的流输出。 lescan
进程在后台 运行:sudo hcitool lescan &
。
经过大量测试后,我不知所措,因为以下脚本 在第一个会话中由 root 执行时:
while read -r line; do
echo "$line"
done < <(btmon 2>&1)
不打印与以下内容相同的行:
sudo btmon 2>&1
从单独的 shell 会话执行时。从第一行开始打印的行数较少,输出有时会滞后几分钟。
这是怎么回事?有什么意见或建议吗?
编辑:示例输出。所有三个会话都是同时进行的,预期结果是相同的输出。所有人同时停了下来。
sudo hcitool lescan
LE Scan ...
49:D3:E5:61:AE:36 (unknown)
49:D3:E5:61:AE:36 (unknown)
28:39:5E:49:DB:23 (unknown)
28:39:5E:49:DB:23 [TV] Samsung 9 Series (65)
5D:C2:ED:93:59:1F (unknown)
6A:46:64:F2:25:57 (unknown)
6A:46:64:F2:25:57 (unknown)
78:D0:8A:A5:8B:43 (unknown)
78:D0:8A:A5:8B:43 (unknown)
33:CA:22:86:F5:4D (unknown)
C8:69:CD:6A:89:2A (unknown)
6A:4E:F6:F6:BB:8C (unknown)
6A:4E:F6:F6:BB:8C (unknown)
C8:69:CD:6A:89:2A (unknown)
5D:C2:ED:93:59:1F (unknown)
7C:D1:C3:DA:91:14 (unknown)
7C:D1:C3:DA:91:14 (unknown)
6D:D2:11:18:37:F1 (unknown)
6D:D2:11:18:37:F1 (unknown)
F8:77:B8:8A:01:C1 (unknown)
FF:FF:C0:22:6B:36 (unknown)
sudo btmon 2>&1
Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l) 0.563610
= Note: Bluetooth subsystem version 2.22 0.563625
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0) [hci0] 0.563632
= Open Index: B8:27:EB:C8:9E:A6 [hci0] 0.563634
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation) [hci0] 0.563638
@ MGMT Open: bluetoothd (privileged) version 1.14 {0x0001} 0.563644
@ MGMT Open: btmon (privileged) version 1.14 {0x0002} 0.563788
@ MGMT Open: btmon (privileged) version 1.14 {0x0003} 2.149663
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} 11.269034
@ RAW Close: hcitool {0x0004} 11.270311
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} [hci0] 11.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 [hci0] 11.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 11.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 [hci0] 11.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 11.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35 [hci0] 11.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43 [hci0] 11.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40 [hci0] 11.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 11.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34 [hci0] 11.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 11.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27 [hci0] 11.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23 [hci0] 11.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32 [hci0] 11.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.585662
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 0
RSSI: -82 dBm (0xae)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.855660
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 0
RSSI: -90 dBm (0xa6)
> HCI Event: LE Meta Event (0x3e) plen 23 [hci0] 11.999393
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
Data length: 11
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0b08
RSSI: -97 dBm (0x9f)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 12.191914
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 12.772156
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6D:D2:11:18:37:F1 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0198b24d86
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 12.773961
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6D:D2:11:18:37:F1 (Resolvable)
Data length: 0
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 40 [hci0] 13.347879
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Public (0x00)
Address: F8:77:B8:8A:01:C1 (Samsung Electronics Co.,Ltd)
Data length: 28
Company: Samsung Electronics Co. Ltd. (117)
Data: 4204018060f877b88a01c1fa77b88a01c001000000000000
RSSI: -100 dBm (0x9c)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 14.016707
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: FF:FF:C0:22:6B:36 (OUI FF-FF-C0)
Data length: 14
Flags: 0x05
LE Limited Discoverable Mode
BR/EDR Not Supported
TX power: 0 dBm
Appearance: Keyboard (0x03c1)
16-bit Service UUIDs (partial): 1 entry
Unknown (0xffe0)
RSSI: -90 dBm (0xa6)
最后,脚本:
Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l) 0.149535
= Note: Bluetooth subsystem version 2.22 0.149551
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0) [hci0] 0.149556
= Open Index: B8:27:EB:C8:9E:A6 [hci0] 0.149559
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation) [hci0] 0.149563
@ MGMT Open: btmon (privileged) version 1.14 {0x0002} 0.149569
@ MGMT Open: bluetoothd (privileged) version 1.14 {0x0001} 0.149573
@ MGMT Open: btmon (privileged) version 1.14 {0x0003} 0.149663
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} 9.269034
@ RAW Close: hcitool {0x0004} 9.270311
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} [hci0] 9.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 [hci0] 9.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 9.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 [hci0] 9.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 9.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35 [hci0] 9.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43 [hci0] 9.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40 [hci0] 9.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 9.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34 [hci0] 9.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 9.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27 [hci0] 9.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23 [hci0] 9.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32 [hci0] 9.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
如图所示,脚本严重滞后。如果我等到 hcitool
稳定下来,脚本就跟不上了。输出只是......停止。
我很困惑。
您似乎遇到了三个不同的问题:
- stdout 默认情况下是缓冲的——也就是说,内容由标准 C 库缓冲,并且仅在将其写入非 TTY 目标时以块形式刷新。 BashFAQ #9.
中对此进行了详细讨论
- 因为您没有在 运行
read
之前清除 IFS
,前导和尾随空格将被丢弃。 BashFAQ #1 中讨论了使用 while read
的最佳实践。
- 因为您使用的是
echo
,带有反斜杠的内容的行为是未定义的,并且会根据活动的(编译时和运行时)shell 配置而有所不同。 (当 posix
和 xpg_echo
标志都启用时,例如,echo '\t'
将发出文字制表符;没有它们,它发出双字符字符串 \t
;两者POSIX 规范允许这些)。请参阅 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html 中的上述规范,尤其是“应用程序使用”部分。
为了也显示为 TTY 而不是 btmon
的管道,下面还使用 unbuffer
(作为 expect
的一部分提供)而不是较低的-开销 GNU coreutils 工具 stdbuf -o0
:
改为考虑:
while IFS= read -r line; do
printf '%s\n' "$line"
done < <(unbuffer btmon 2>&1)
我正在尝试实时解析来自 btmon
的流输出。 lescan
进程在后台 运行:sudo hcitool lescan &
。
经过大量测试后,我不知所措,因为以下脚本 在第一个会话中由 root 执行时:
while read -r line; do
echo "$line"
done < <(btmon 2>&1)
不打印与以下内容相同的行:
sudo btmon 2>&1
从单独的 shell 会话执行时。从第一行开始打印的行数较少,输出有时会滞后几分钟。
这是怎么回事?有什么意见或建议吗?
编辑:示例输出。所有三个会话都是同时进行的,预期结果是相同的输出。所有人同时停了下来。
sudo hcitool lescan
LE Scan ...
49:D3:E5:61:AE:36 (unknown)
49:D3:E5:61:AE:36 (unknown)
28:39:5E:49:DB:23 (unknown)
28:39:5E:49:DB:23 [TV] Samsung 9 Series (65)
5D:C2:ED:93:59:1F (unknown)
6A:46:64:F2:25:57 (unknown)
6A:46:64:F2:25:57 (unknown)
78:D0:8A:A5:8B:43 (unknown)
78:D0:8A:A5:8B:43 (unknown)
33:CA:22:86:F5:4D (unknown)
C8:69:CD:6A:89:2A (unknown)
6A:4E:F6:F6:BB:8C (unknown)
6A:4E:F6:F6:BB:8C (unknown)
C8:69:CD:6A:89:2A (unknown)
5D:C2:ED:93:59:1F (unknown)
7C:D1:C3:DA:91:14 (unknown)
7C:D1:C3:DA:91:14 (unknown)
6D:D2:11:18:37:F1 (unknown)
6D:D2:11:18:37:F1 (unknown)
F8:77:B8:8A:01:C1 (unknown)
FF:FF:C0:22:6B:36 (unknown)
sudo btmon 2>&1
Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l) 0.563610
= Note: Bluetooth subsystem version 2.22 0.563625
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0) [hci0] 0.563632
= Open Index: B8:27:EB:C8:9E:A6 [hci0] 0.563634
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation) [hci0] 0.563638
@ MGMT Open: bluetoothd (privileged) version 1.14 {0x0001} 0.563644
@ MGMT Open: btmon (privileged) version 1.14 {0x0002} 0.563788
@ MGMT Open: btmon (privileged) version 1.14 {0x0003} 2.149663
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} 11.269034
@ RAW Close: hcitool {0x0004} 11.270311
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} [hci0] 11.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 [hci0] 11.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 11.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 [hci0] 11.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 11.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35 [hci0] 11.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43 [hci0] 11.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40 [hci0] 11.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 11.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34 [hci0] 11.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 11.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27 [hci0] 11.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23 [hci0] 11.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32 [hci0] 11.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.585662
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 0
RSSI: -82 dBm (0xae)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 11.855660
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 0
RSSI: -90 dBm (0xa6)
> HCI Event: LE Meta Event (0x3e) plen 23 [hci0] 11.999393
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
Data length: 11
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0b08
RSSI: -97 dBm (0x9f)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 12.191914
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 12.772156
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6D:D2:11:18:37:F1 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0198b24d86
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 12.773961
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6D:D2:11:18:37:F1 (Resolvable)
Data length: 0
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 40 [hci0] 13.347879
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Public (0x00)
Address: F8:77:B8:8A:01:C1 (Samsung Electronics Co.,Ltd)
Data length: 28
Company: Samsung Electronics Co. Ltd. (117)
Data: 4204018060f877b88a01c1fa77b88a01c001000000000000
RSSI: -100 dBm (0x9c)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 14.016707
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: FF:FF:C0:22:6B:36 (OUI FF-FF-C0)
Data length: 14
Flags: 0x05
LE Limited Discoverable Mode
BR/EDR Not Supported
TX power: 0 dBm
Appearance: Keyboard (0x03c1)
16-bit Service UUIDs (partial): 1 entry
Unknown (0xffe0)
RSSI: -90 dBm (0xa6)
最后,脚本:
Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l) 0.149535
= Note: Bluetooth subsystem version 2.22 0.149551
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0) [hci0] 0.149556
= Open Index: B8:27:EB:C8:9E:A6 [hci0] 0.149559
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation) [hci0] 0.149563
@ MGMT Open: btmon (privileged) version 1.14 {0x0002} 0.149569
@ MGMT Open: bluetoothd (privileged) version 1.14 {0x0001} 0.149573
@ MGMT Open: btmon (privileged) version 1.14 {0x0003} 0.149663
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} 9.269034
@ RAW Close: hcitool {0x0004} 9.270311
@ RAW Open: hcitool (privileged) version 2.22 {0x0004} [hci0] 9.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 [hci0] 9.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 9.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 [hci0] 9.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4 [hci0] 9.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35 [hci0] 9.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43 [hci0] 9.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40 [hci0] 9.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 9.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34 [hci0] 9.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26 [hci0] 9.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27 [hci0] 9.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23 [hci0] 9.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32 [hci0] 9.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 9.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
如图所示,脚本严重滞后。如果我等到 hcitool
稳定下来,脚本就跟不上了。输出只是......停止。
我很困惑。
您似乎遇到了三个不同的问题:
- stdout 默认情况下是缓冲的——也就是说,内容由标准 C 库缓冲,并且仅在将其写入非 TTY 目标时以块形式刷新。 BashFAQ #9. 中对此进行了详细讨论
- 因为您没有在 运行
read
之前清除IFS
,前导和尾随空格将被丢弃。 BashFAQ #1 中讨论了使用while read
的最佳实践。 - 因为您使用的是
echo
,带有反斜杠的内容的行为是未定义的,并且会根据活动的(编译时和运行时)shell 配置而有所不同。 (当posix
和xpg_echo
标志都启用时,例如,echo '\t'
将发出文字制表符;没有它们,它发出双字符字符串\t
;两者POSIX 规范允许这些)。请参阅 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html 中的上述规范,尤其是“应用程序使用”部分。
为了也显示为 TTY 而不是 btmon
的管道,下面还使用 unbuffer
(作为 expect
的一部分提供)而不是较低的-开销 GNU coreutils 工具 stdbuf -o0
:
改为考虑:
while IFS= read -r line; do
printf '%s\n' "$line"
done < <(unbuffer btmon 2>&1)