简体   繁体   English

Android蓝牙 - 确定配对设备是否“打开”

[英]Android Bluetooth - Determine if a paired device is 'on'

I want to determine if a paired device is currently active. 我想确定配对设备当前是否处于活动状态。 I don't really need to connect to it, just determine if it has a heartbeat. 我真的不需要连接它,只是确定它是否有心跳。

I'm fighting a series of IO errors while trying to connect a bluetooth socket, but I'm not sure I really need to. 我在尝试连接蓝牙插座时遇到了一系列IO错误,但我不确定是否真的需要。

假设您正在使用Android如果您有此设备的BluetoothDevice对象,您可以注册以侦听广播操作 - ACL_CONNECTED或ACL_DISCONNECTED并跟踪连接状态。

If you want to find out wether a BT-Headset is currently actively connected (audio being routed to it), do the following: 如果您想了解BT-Headset当前是否正在连接(音频被路由到它),请执行以下操作:

Declare the following intent-filter 声明以下intent-filter

        <intent-filter >
            <action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
        </intent-filter>

and in your Receiver in onReceive check for: 并在您的Receiver in onReceive中检查:

if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
  headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}

and save the int as a static variable. 并将int保存为静态变量。 Access it anytime you want to know if BT audio is connected(1) / disconnected(0). 如果您想知道BT音频是否已连接(1)/断开连接(0),请随时访问它。 Not pretty, but gets the job done. 不漂亮,但完成工作。

Also check out: https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java 另请查看: https//github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM