简体   繁体   中英

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.

假设您正在使用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:

Declare the following intent-filter

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

and in your Receiver in onReceive check for:

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. Access it anytime you want to know if BT audio is connected(1) / disconnected(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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