System.InvalidOperationException:BluetoothLowEnergyAdapter.Init只能调用一次

时间:2018-01-02 09:59:30

标签: c# xamarin bluetooth-lowenergy

我有一个appliaciton。这有一个MainActivity类。在Android上,如果我关闭我的应用程序,并再次打开此错误消息我得到。

这是我的MainActivity.cs:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        DependencyService.Register<ToastNotification>();
        DependencyService.Register<OpenAppAndroid>();

        Forms.Init(this, bundle);
        BluetoothLowEnergyAdapter.Init(this);
        ToastNotification.Init(this, new PlatformOptions() { SmallIconDrawable = Android.Resource.Drawable.IcDialogInfo });

        LoadApplication(new Spirocco.App(BluetoothLowEnergyAdapter.ObtainDefaultAdapter(ApplicationContext)));

        NotificationReceiver notificationReceiver = new NotificationReceiver();
        AlarmManager alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
        Intent notificationIntent = new Intent(Android.App.Application.Context, notificationReceiver.Class);
        PendingIntent pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, notificationIntent, 0);

        DateTime time = DateTime.Now.AddSeconds(10);
        DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        TimeSpan span = time.ToUniversalTime() - baseTime;
        long timer = (long)span.TotalMilliseconds;
        alarmManager.Set(AlarmType.RtcWakeup, timer, pendingIntent);
    }
}

}

BluetoothLowEnergyAdapter.Init(本);应用程序崩溃的行:System.InvalidOperationException:BluetoothLowEnergyAdapter.Init只能被调用一次。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我们知道@rokkerboci很难看,但是使用try-catch块。

try
        {
            BluetoothLowEnergyAdapter.Init(this);
        }
        catch (Exception)
        {
        }