【问题标题】:A sticky broadcast intent for a bluetooth headset in android/or an alternativeandroid/或替代蓝牙耳机的粘性广播意图
【发布时间】:2017-08-17 19:20:27
【问题描述】:

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

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


    【解决方案1】:

    好吧,我挖掘了很多,但找不到任何粘性广播,因此在应用程序启动时,我会知道手机是否已连接到问题中提到的蓝牙耳机。

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

        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;
        }
    

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

    【讨论】:

    • 使用此代码无法区分Android手表或蓝牙耳机,有什么方法可以区分这两者。
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 2011-11-04
    • 1970-01-01
    • 2012-11-20
    • 2011-05-16
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    相关资源
    最近更新 更多