我正在研究带有蓝牙的覆盆子pi 0 w。目前,我可以使用bluetoothctl手动执行我想要的操作,但是我正在尝试使它自动化,因此可以让rp独自呆几个星期来执行此任务。
目标是使rpi在蓝牙中可被发现,并让某人无需密码即可自动连接到它。建立连接后,我想将info命令发送到该配对的mac地址,以检索有关该设备的更多信息。最终,我想添加一些功能,以便能够将文本文件发送到设备(笔记本电脑,Android手机等),但这可能会在以后出现。最终,我想将信息(带有时间戳的mac和信息结果)记录在一个单独的文件中。
让我说一下,我对此的知识非常有限,但是我正在努力实现这一目标。如果您对如何进行操作有任何想法,那可能会容易些,我很高兴。目前,我一直在开发一个Expect脚本来完成这项工作。到目前为止,我有这个:
#!/usr/bin/expect -f
set prompt "#"
log_user 1
set timeout -1
send_user "Please enter bluetooth signal name: "
expect_user -re "(.*)\n"
set btname $expect_out(1,string)
send_user "BT signal will be $btname\n"
send_user "\nEnter Log Name: "
expect_user -re "(.*)\n"
set logname $expect_out(1,string)
send_user "You entered $logname\n"
log_file -a $logname
sleep 2
spawn sudo bluetoothctl
sleep 2
expect -re $prompt
send "system-alias $btname\r"
sleep 2
expect -re $prompt
send "scan on\r"
sleep 8
expect -re $prompt
send "discoverable on\r"
sleep 2
expect
所以我的问题是,当设备配对时,以下是信号:
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:EB:03:68:F1 Discoverable: yes
[NEW] Device F8:F1:B6:E2:BB:06 DROID RAZR M
[CHG] Device F8:F1:B6:E2:BB:06 Modalias: bluetooth:v0008pB02Dd0000
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001103-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001105-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001106-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001112-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001116-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000112d-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000112f-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001132-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 453994d5-d58b-96f9-6616-b37f586ba2ec
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 936da01f-9abd-4d9d-80c7-02af85c822a8
[CHG] Device F8:F1:B6:E2:BB:06 ServicesResolved: yes
[CHG] Device F8:F1:B6:E2:BB:06 Paired: yes
[CHG] Device F8:F1:B6:E2:BB:06 ServicesResolved: no
[CHG] Device F8:F1:B6:E2:BB:06 Connected: no
[bluetooth]# info F8:F1:B6:E2:BB:06
Device F8:F1:B6:E2:BB:06
Name: DROID RAZR M
Alias: DROID RAZR M
Class: 0x5a020c
Icon: phone
Paired: yes
Trusted: no
Blocked: no
Connected: no
LegacyPairing: no
UUID: Dialup Networking (00001103-0000-1000-8000-00805f9b34fb)
UUID: OBEX Object Push (00001105-0000-1000-8000-00805f9b34fb)
UUID: OBEX File Transfer (00001106-0000-1000-8000-00805f9b34fb)
UUID: Audio Source (0000110a-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Headset AG (00001112-0000-1000-8000-00805f9b34fb)
UUID: NAP (00001116-0000-1000-8000-00805f9b34fb)
UUID: Handsfree Audio Gateway (0000111f-0000-1000-8000-00805f9b34fb)
UUID: SIM Access (0000112d-0000-1000-8000-00805f9b34fb)
UUID: Phonebook Access Server (0000112f-0000-1000-8000-00805f9b34fb)
UUID: Message Access Server (00001132-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific (453994d5-d58b-96f9-6616-b37f586ba2ec)
UUID: Vendor specific (936da01f-9abd-4d9d-80c7-02af85c822a8)
Modalias: bluetooth:v0008pB02Dd0000
所以我的问题是-我如何进行“期望”以检查有效MAC的正则表达式和字符串“ Paired:yes”,并将该MAC从缓冲区中拉出。我可以期望“期望(正则表达式&&字符串“ Paired:是”){做}”吗?我对如何执行此操作并拉出实际的MAC将其保存到变量并在下一个info命令中使用它有些迷惑。我猜想它正在操纵Expect_out(buffer),但这超出了我的技能范围。
答案 0 :(得分:0)
您问如何从期望匹配的数据中提取一些信息。但是,您完全可以在脚本的第一部分中进行此操作,从用户那里获取输入。
现在可以使用gets更简单地完成该部分。但是,当从bluetoothctl接收数据时,使用Expect_out(#,string)挑选相关的部分效果很好。
此外,如果您只是期望正确的事情,那么就没有理由执行所有这些睡眠命令。
#!/usr/bin/expect -f
# Use command-line arguments if provided
lassign $argv btname logname
# Prompt for any missing arguments
if {$argc < 1} {
send_user "Please enter bluetooth signal name: "
gets stdin btname
}
send_user "BT signal will be $btname\n"
if {$argc < 2} {
send_user "\nEnter Log Name: "
gets stdin logname
}
log_file -a $logname
set prompt "#"
spawn bluetoothctl
expect -ex $prompt
send "system-alias $btname\r"
expect -ex "Changing $btname succeeded"
expect -ex $prompt
send "power on\r"
expect -ex "Changing power on succeeded"
expect -ex $prompt
send "scan on\r"
expect -ex $prompt
send "discoverable on\r"
expect {
-timeout 120
-re {Device ([0-9A-F:]+) Paired: yes} {
# Get info on newly paired device
expect -ex $prompt
send "info $expect_out(1,string)\r"
}
-re {Controller ([0-9A-F:]+) Discoverable: no} {
# Re-arm discoverability
send "discoverable on\r"
exp_continue
}
timeout {
exit 1
}
}
expect -ex $prompt
# Device info is now available in $expect_out(buffer)