2014-02-18 153 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