在Linux中,我曾经使用"hidd --connect mmac"来连接BT设备,但是现在它已经从Bluez5开始消失了.我可以使用bluetoothctl手动建立连接但我需要从我的应用程序使用这些命令并使用bluetoothctl将是困难的.
什么是hcitool等效命令来做bluetoothctl做的事情?
例如,我输入bluetoothctl:
select <cmac>
scan on
trust <mmac>
pairable on
pair <mmac>
connect <mmac>
Run Code Online (Sandbox Code Playgroud)
我可以使用"hcitool scan"进行扫描,但我还没想到要连接.我尝试使用"hcitool cc mmac"后跟"hcitool auth mmac",但没有任何效果.
或者hcitool可以做bluetoothctl的工作吗?
小智 15
我在这样的脚本中使用bluetoothctl:
#!/bin/bash
bluetoothctl << EOF
power on
EOF
Run Code Online (Sandbox Code Playgroud)
并且可以将多个命令指定为每行一个命令.
奇怪的是,它对我来说不是这样的:
echo "power on" | bluetoothctl
Run Code Online (Sandbox Code Playgroud)
(我使用的是bluez-5.21-r1 - 不确定这是否依赖于版本)
小智 10
您可以将命令传递给bluetoothctl,如下所示:
echo -e 'power on\nquit' | bluetoothctl
Run Code Online (Sandbox Code Playgroud)
您甚至可以使用tab来自动完成:
echo -e 'power on\nconnect \t \nquit' | bluetoothctl
Run Code Online (Sandbox Code Playgroud)
我不是将此作为对Jiri答案的评论添加,因此更加明显.
另一个解决方案(我认为最好的解决方案)是将期望的TCL脚本与bluetoothctl一起使用。
我使用它来自动使用bluetoothctl连接到蓝牙设备,而无需与之交互。
例如,连接到由其MAC地址标识的设备
#!/usr/bin/expect -f
set address [lindex $argv 0]
set prompt "#"
log_user 0
spawn bluetoothctl
expect $prompt
send -- "remove $address\r"
expect $prompt
send -- "scan on\r"
expect "Discovery started"
sleep 10
send -- "scan off\r"
expect "Discovery stopped"
expect $prompt
send -- "trust $address\r"
expect "trust succeeded"
expect $prompt
send -- "pair $address\r"
expect "Pairing successful"
expect "Device $address Connected: no"
expect $prompt
send -- "connect $address\r"
expect "Connection successful"
expect $prompt
send "quit\r"
expect "eof"
Run Code Online (Sandbox Code Playgroud)
您可以按原样启动此脚本。./myExpectScript <MAC_addr>
如果要查看输出,只需将log_user值设置为1