开发者

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 wh开发者_如何学运维ile trying to connect a bluetooth socket, but I'm not sure I really need to.


Assuming you are using Android If you have a BluetoothDevice object to this device, you can register to listen for the Broadcast Actions - ACL_CONNECTED or ACL_DISCONNECTED and keep track of the connection state.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜