android - 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
绝对有效,所以这里肯定还有另一个问题。
这里有经验的人吗?谢谢 :)
解决方案
推荐阅读
- python - Airflow - 任务中的并行执行
- mysql - MySQL选择仅具有值的列
- python - python traceback keyerror代码,用于在kaggle文本数据集上训练模型
- java - 偏移和限制的 SQLite 分页问题
- apache-kafka - kafka-reassign-partition.sh 不再接受 log_dirs 参数
- firebase - @ionic-native/firebase-dynamic-links 插件未公开 getDynamicLink 方法
- wordpress - 限制除特定域之外的所有人的文件下载访问权限
- javascript - 如何创建一个单独的具有公共 id 的 json 对象数组
- python - 如何将 python pandas 系列列附加到数据框?
- javascript - 如何在 Javascript 中查找儒略历上的当前日期