2014-02-18 149 views
4

在Linux中,我曾经使用“hidd --connect mmac”来连接BT设备,但自Bluez5以来已经不存在了。 我可以使用bluetoothctl手动建立连接,但是我需要从我的应用程序使用这些命令,并且使用bluetoothctl会很困难。bluetoothctl to hcitool等价命令

什么是hcitool等效命令来做什么bluetoothctl呢?

例如,我会输入bluetoothctl:

select <cmac> 
scan on 
trust <mmac> 
pairable on 
pair <mmac> 
connect <mmac> 

我可以用“hcitool扫描”的扫描,但我还没有想出连接。 我试过使用“hcitool cc mmac”,后面跟着“hcitool auth mmac”,但没有任何作品。

或者hcitool可以做bluetoothctl做什么?

+0

我从来没有弄清楚hcitool,所以我只是最终使用bluetoothctl发送stdin并处理它的stdout。不优雅,但它的作品。 –

+0

这个问题似乎是脱离主题,因为它是关于一般的Linux使用,而不是任何类型的编程。请在unix.stackexchange.com或askubuntu.com询问。 –

+0

这与编程有关。我从一个Java应用程序发出这些命令(使用java.lang.Runtime.exec),这是一个前端应用程序连接到蓝牙设备。在我的其他评论中,我解释说我找到了一个使用bluetoothctl的解决方法,使用stdin/stdout,它涉及使用java.lang.ProcessBuilder。 –

回答

9

我使用bluetoothctl从脚本是这样的:

#!/bin/bash 
bluetoothctl << EOF 
power on 
EOF 

而且这是可能的,每行一个命令指定多个命令。

奇怪的是,它都这样做不工作对我来说:

echo "power on" | bluetoothctl 

(我用的bluez-5.21-R1 - 不知道这是否是取决于版本)

4

可以传递命令bluetoothctl这样的:

echo -e 'power on\nquit' | bluetoothctl 

你甚至可以使用Tab键自动:

echo -e 'power on\nconnect \t \nquit' | bluetoothctl 

我不会将此添加为对Jiri答案的评论,因此它更加明显。

+1

这并没有真正进行配对工作,虽然...开机和关机是几乎会做点实事唯一的命令......配对尝试,但通过没有跟上。打开代理不起作用某种glib错误 – Lukas1

+1

@ Lukas1 - 我想这是因为'bluetoothctl'工作异步。为了编写更复杂的脚本,您需要以某种方式等待每条命令完成。 – Jiri

0

另一种解决方案(我认为最好的)是使用期望的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" 

可以启动这个脚本,因为它./myExpectScript <MAC_addr> 如果你想看到的输出只需设置log_user值为1

0

我使用tmux解决了这个问题,即:

  1. 安装tmux

    apt install tmux

  2. 创建会话:

    tmux new-session -d -s ServerFault 'sudo bluetoothctl -a |& tee /run/shm/BLUETOOTH_OUTPUT'

  3. 然后你就可以发出如下命令:

    tmux send-keys -t ServerFault "pair AC:22:0B:9F:0C:D6" Enter