簡體   English   中英

如何以編程方式檢查Android中藍牙設備的連接狀態?

[英]How to programmatically check connection state of a Bluetooth device in Android?

我可以枚舉配對的Bluetooth設備,我需要在應用啟動時檢查它們當前的連接狀態。

但是,我發現無法獲得連接狀態?!

以下是我嘗試但失敗的一些努力:

  1. android.bluetooth.BluetoothDevice

它具有獲取遠程設備的綁定狀態的方法,並且綁定狀態的可能值為: BOND_NONEBOND_BONDINGBOND_BONDED 但這不是連接狀態。

  1. android.bluetooth.BluetoothProfile

該方法可用於獲取連接狀態,但必須首先獲取BluetoothProfile對象。

public abstract int getConnectionState (BluetoothDevice device); 

正如doc中所描述的:“ 客戶端應調用getProfileProxy(Context,BluetoothProfile.ServiceListener,int),以獲取配置文件代理。 ”,它只能在狀態更改時調用的ServiceListener檢索。

如果我在啟動時查詢狀態而沒有發生任何狀態更改,該怎么辦?

  1. android.bluetooth.BluetoothManager

它提供了一種方法:

public int getConnectionState (BluetoothDevice device, int profile);

但它不能用於檢測Bluetooth揚聲器的連接狀態,因為配置文件參數只接受GATTGATT_SERVER (對於低能耗設備),不支持其他配置文件( HEADSETHEALTHA2DP )。

  1. android.bluetooth.BluetoothAdapter

它提供了一種方法:

public int getProfileConnectionState (int profile);

此功能可用於檢查本地Bluetooth適配器是否已連接到特定配置文件的任何遠程設備。 它可用於檢查指定的配置文件。 但它不能用於檢查給定的設備。

有誰知道如何獲得給定Bluetooth設備的連接狀態?

提前致謝。

請參閱類似問題的答案中的注釋如何以編程方式判斷藍牙設備是否已連接? (Android 2.2)

  • 在應用程序啟動時無法檢索已連接設備的列表。 藍牙API不允許您進行查詢,而是允許您收聽更改。
  • 對上述問題的一個愚蠢的解決方法是檢索所有已知/配對設備的列表...然后嘗試連接到每個設備(以確定您是否已連接)。 或者,您可以讓后台服務觀看藍牙API並將設備狀態寫入磁盤,以便您的應用程序在以后使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM