首页 > 解决方案 > Android:获取是否有任何设备刚刚通过蓝牙连接到手机

问题描述

我正在开发一个音乐播放器应用程序。当我进入我的汽车并启动它时,汽车会Bluetooth自动连接,但是,它不会启动任何媒体播放。

我现在希望能够在我的应用程序中捕获是否有任何设备刚刚连接到我的手机Bluetooth以开始播放音乐。我在想的是:

1.)粘性服务(始终开启)

2.)广播接收器(处理传入的意图)

但我似乎找不到任何BroadcastReceiver只要“任何设备刚刚通过Bluetooth”连接就可以触发的东西。

这就是我尝试的BroadcastReceiver(尚未投入使用)

[BroadcastReceiver]
public class BecomingNoisyReceiver : BroadcastReceiver
{
     public override void OnReceive(Context context, Intent intent)
     {
        //tests
        if (BluetoothA2dp.ActionConnectionStateChanged.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "0", ToastLength.Short).Show();
        }

        if (BluetoothA2dp.ActionPlayingStateChanged.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "1", ToastLength.Short).Show();
        }

        if (BluetoothAdapter.ActionConnectionStateChanged.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "2", ToastLength.Short).Show();
        }

        if (BluetoothAdapter.ActionDiscoveryFinished.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "3", ToastLength.Short).Show();
        }

        if (BluetoothAdapter.ActionStateChanged.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "4", ToastLength.Short).Show();
        }

        if (BluetoothDevice.ActionFound.Equals(intent.Action))
        {        
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "5", ToastLength.Short).Show();
        }

        if (BluetoothDevice.ActionPairingRequest.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "6", ToastLength.Short).Show();
        }

        if (BluetoothDevice.ActionAclConnected.Equals(intent.Action))
        {
            Activity_Player.Instance.PlayOrPauseLogic();
            Toast.MakeText(context, "7", ToastLength.Short).Show();
        }


    }
}

您会看到我设置了 toasts 来查看,一旦我的汽车连接到我的手机,哪些意图会触发。不幸的是,没有人这样做。但是我知道,这BroadcastReceiver绝对有效,所以这里肯定还有另一个问题。

这里有经验的人吗?谢谢 :)

标签: androidbluetooth

解决方案


推荐阅读