Android/或替代方案中蓝牙耳机的粘性广播意图

问题描述 投票:0回答:2

我正在开发一个应用程序,我需要知道是否连接了蓝牙耳机。我创建了一个广播接收器,如果我们在应用程序打开时连接到蓝牙耳机,它会接收广播意图。另一方面,如果连接到蓝牙设备然后启动应用程序,我不会收到任何有关蓝牙设备连接的广播。我想知道应用程序启动时是否连接了任何蓝牙耳机。以下是我的广播接收器和清单。

public class BluetoothDeviceConnectionReciver extends BroadcastReceiver {


    static OnBluetoothDeviceStateChangeLisener onBluetoothStateChangedListener;

    @Override
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    //int headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);

    //Toast.makeText(context, "New Test", Toast.LENGTH_SHORT).show();

    //BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.i("headsetAudioState","in broadcast");


    if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
        int bluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
                BluetoothHeadset.STATE_DISCONNECTED);
        Log.i("headsetAudioState",bluetoothHeadsetState+" ");
        //Device found
        if (bluetoothHeadsetState == BluetoothHeadset.STATE_CONNECTED) {
            if (onBluetoothStateChangedListener != null)
                onBluetoothStateChangedListener.OnBluetoothHeadsetConnected();
            Log.i("headsetAudioState",bluetoothHeadsetState+" connected");
        }
        if(bluetoothHeadsetState == BluetoothHeadset.STATE_DISCONNECTED){
            if (onBluetoothStateChangedListener != null)
                onBluetoothStateChangedListener.OnBluetoothHeadsetDisconnected();
            Log.i("headsetAudioState",bluetoothHeadsetState+" disconnected");
        }
    }



    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        //Device is now connected
        if (onBluetoothStateChangedListener != null)
            onBluetoothStateChangedListener.OnBluetoothHeadsetConnected();
    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        //Done searching
    } else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
        //Device is about to disconnect
    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
        //Device has disconnected
        if (onBluetoothStateChangedListener != null)
            onBluetoothStateChangedListener.OnBluetoothHeadsetDisconnected();

    } 


    }
    public void setOnBluetoothStateChangeListener(OnBluetoothDeviceStateChangeLisener listener){
    onBluetoothStateChangedListener = listener;
    }

    public interface OnBluetoothDeviceStateChangeLisener{
    void OnBluetoothHeadsetConnected();
    void OnBluetoothHeadsetDisconnected();
    }
}

manifest.xml

   .....
   .....
   .....

   <receiver android:name=".BluetoothDeviceConnectionReciver">
        <intent-filter>
            <action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"/>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
        </intent-filter>

    </receiver>

   .....
   .....

我想知道是否还有其他我应该倾听的意图。或者请让我知道我还有哪些其他选择来实现预期目标。

P.S:连接的设备应该是耳机,因为我打算使用蓝牙耳机的麦克风。

android bluetooth android-broadcast headset sticky-broadcast
2个回答
3
投票

嗯,我挖了很多,但无法找到任何粘性广播,以便在应用程序启动时我可以知道手机是否连接到问题中提到的蓝牙耳机。

我实现的工作方法是获取蓝牙适配器并检查蓝牙配置文件的连接状态,如下面的代码所示

    public boolean isBluetoothHeadsetConnected() {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean isConnected = mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
                && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
        Log.i("bluetoothConnected",isConnected+"");
        return  isConnected;
    }

我在应用程序的

onCreate()
SplashScreenActivity
中调用此代码并设置相应的标志。应用程序启动后蓝牙状态的变化由我在问题中实现的广播监听器负责。这段代码很好地达到了我的目的。


0
投票

除了配置文件连接检查之外, 蓝牙可以通过MediaRouter检查

fun isBluetoothConnected() {
    val mediaRouter = context.getSystemService(Context.MEDIA_ROUTER_SERVICE) as MediaRouter
    return mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO).deviceType == MediaRouter.RouteInfo.DEVICE_TYPE_BLUETOOTH
}
© www.soinside.com 2019 - 2024. All rights reserved.