Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xamarin表单--检测外部蓝牙';打开';和切换';开关';相应地,在应用程序中_C#_Xamarin_Bluetooth_Switch Statement_Toggle - Fatal编程技术网

C# xamarin表单--检测外部蓝牙';打开';和切换';开关';相应地,在应用程序中

C# xamarin表单--检测外部蓝牙';打开';和切换';开关';相应地,在应用程序中,c#,xamarin,bluetooth,switch-statement,toggle,C#,Xamarin,Bluetooth,Switch Statement,Toggle,是否有一种方法可以使应用程序从外部触发器读取蓝牙激活,例如,当用户向下滑动屏幕并打开它,并在应用程序中相应地切换开关时(即,如果用户从外部打开它,则我的开关应自动切换到打开,反之亦然)。 这是我用来在我的应用程序中打开和关闭蓝牙的xamarin C#代码。 MainActivity.cs void Enable(object sender, EventArgs e) { BluetoothManager _manager;

是否有一种方法可以使应用程序从外部触发器读取蓝牙激活,例如,当用户向下滑动屏幕并打开它,并在应用程序中相应地切换开关时(即,如果用户从外部打开它,则我的开关应自动切换到打开,反之亦然)。 这是我用来在我的应用程序中打开和关闭蓝牙的xamarin C#代码。
MainActivity.cs

void Enable(object sender, EventArgs e)
            {
            BluetoothManager _manager;
            _manager = (BluetoothManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.BluetoothService);
            bool on = _manager.Adapter.IsEnabled;
            if (on == true)
            {
                string error = "Bluetooth is Already on";
                Toast.MakeText(this, error, ToastLength.Long).Show();
                e.Equals(true);
            }
            else
            {
                string okay = "Bluetooth has been turned on";
                _manager.Adapter.Enable();
                Toast.MakeText(this, okay, ToastLength.Long).Show();

            }
        }
 void Disable(object sender, EventArgs e)
            {
            BluetoothManager _manager;
            _manager = (BluetoothManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.BluetoothService);
            bool on = _manager.Adapter.IsEnabled;
            if (on == false)
            {
                string error = "Bluetooth is Already off";
                Toast.MakeText(this, error, ToastLength.Long).Show();
                e.Equals(false);
               
            }
            else
            {
                string okay = "Bluetooth has been turned off";
                _manager.Adapter.Disable();
                Toast.MakeText(this, okay, ToastLength.Long).Show();

            }
        }
我在main活动中使用的订户消息中心就是这个

  BluetoothAndroid();
            MessagingCenter.Subscribe<object, bool>(this, "Bluetooth", (sender, isEnabled) =>
            {
                if (isEnabled)
                {
                    Enable(null, new EventArgs());
                }
                else
                {
                    Disable(null, new EventArgs());
                }
            });
BluetoothAndroid();
MessagingCenter.Subscribe(此为“蓝牙”),(发件人,已启用)=>
{
如果(已启用)
{
启用(null,新的EventArgs());
}
其他的
{
禁用(null,新的EventArgs());
}
});
MainPage.xaml.cs Subscriber.sender方法为

 private  void Enable(object sender, ToggledEventArgs e) {
            if (e.Value == true)
            {
                MessagingCenter.Send<object, bool>(this, "Bluetooth", true);
               // await DisplayAlert("Bluetooth Status", "Bluetooth has been turned on", "OK");

            }
            else
            {
                MessagingCenter.Send<object, bool>(this, "Bluetooth", false);
               // await DisplayAlert("Bluetooth Status", "Bluetooth has been turned off", "OK");
            }
        }
private void启用(对象发送器,ToggledEventArgs e){
如果(e.Value==true)
{
发送(此为“蓝牙”,正确);
//等待显示警报(“蓝牙状态”、“蓝牙已打开”、“确定”);
}
其他的
{
发送(此为“蓝牙”,错误);
//等待显示警报(“蓝牙状态”、“蓝牙已关闭”、“正常”);
}
}

我所需要的就是C sharp代码,当用户从外部活动触发状态时,可以进入艺术,导出状态并将开关的切换设置为打开或关闭,谢谢

您可以创建一个
广播接收器
来监听
android.bluetooth.adapter.action.STATE\u更改了
操作并相应地采取行动

[广播接收器]
[IntentFilter(新[]{BluetoothAdapter.ActionStateChanged}]
公共类Bluetooth状态更改接收器:BroadcastReceiver
{
公共覆盖void OnReceive(上下文、意图)
{
如果(intent.Extras==null)
返回;
var state=intent.GetIntExtra(BluetoothAdapter.ExtraState,-1);
开关(状态)
{
案例(int)State.On:
//蓝牙已打开
打破
case(int)State.Off:
//蓝牙已关闭
打破
}
}
}

你需要实例化并注册你的接收器,一个好地方就是你的主要活动。

非常感谢@CheeseBaron,让我来实现它,我会马上反馈。这个问题有任何更新吗?没有,他的类没有显示任何错误,但仍然无法从外部触发器打开蓝牙