How to use BluetoothHeadset class via reflection

前端 未结 2 1816
渐次进展
渐次进展 2021-01-14 19:55

I want to use methods of BluetoothHeadset class in Android 2.0+. Here is my code:

    Class bluetoothHeadsetClass = Class.forName(\"android.bluetooth.Bluetoo         


        
2条回答
  •  孤独总比滥情好
    2021-01-14 20:15

    Also tried invoking this by reflection. Ultimately gave up and used a broadcastReceiver instead.

    Declare the following 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

提交回复
热议问题