断开BLE设备时,Xamarin应用程序崩溃[DisconnectDevice]

时间:2015-04-30 15:02:24

标签: c# xamarin bluetooth-lowenergy nullreferenceexception xamarin.forms

我有一个Xamarin表单应用程序,它使用Monkey.Robotics.Core连接到BLE设备并从中读取数据。 我有一个发现设备的xaml.cs页面(在方法StartUpJobs中):

public partial class DeviceList : ContentPage
{   
    IAdapter adapter;
    ObservableCollection<IDevice> devices;

    IAdapter serviceListadapter;
    IDevice serviceListdevice;
    ObservableCollection<IService> services;

    IAdapter adapterCharacteristics;
    IDevice deviceCharacteristics;
    IService serviceCharacteristics;
    ObservableCollection<ICharacteristic> characteristics;

    IDevice currentDevice;

    public DeviceList (IAdapter adapter)
    {
        InitializeComponent ();

        StartUpJobs(adapter);
        MessagingCenter.Subscribe<CharacteristicDetail_TISensor>(this, "DisconnectDeviceAndRefreshDevices", (sender) =>
        {
            //Prints the connected device name - just to check for myself.
            DisplayAlert("Props", "Name: " + currentDevice.Name, "ok"); 
            adapter.DisconnectDevice(currentDevice);
            StartUpJobs(adapter);
        });
    }

    public void StartUpJobs(IAdapter adapter)
    {
        List<string> FoundDevicesList = new List<string>();
        this.adapter = adapter;
        this.devices = new ObservableCollection<IDevice>();
        listView.ItemsSource = devices;
        //Start the scan on startup.
        StartScanning(0x180D.UuidFromPartial());
        adapter.DeviceDiscovered += (object sender, DeviceDiscoveredEventArgs e) => Device.BeginInvokeOnMainThread(() => {
            if (!FoundDevicesList.Contains(e.Device.Name))
            {
                devices.Add(e.Device);
            }
            FoundDevicesList.Add(e.Device.Name);
        }
    );

启动它时,它将启动查找所有设备的StartUpJobs(适配器)方法,它们将列在ListView中。当用户点击某个项目时,将获取所有详细信息:

public void OnItemSelected (object sender, SelectedItemChangedEventArgs e) 
{
        if (((ListView)sender).SelectedItem == null) {
            return;
        }
        var device = e.SelectedItem as IDevice;
        ((ListView)sender).SelectedItem = null; // clear selection
        ServiceList(adapter, device);
}

此功能将用户发送到另一个视图,在那里他可以查看实时同步数据等。当用户现在点击回来时我想要断开BLE设备,用户被重定向到第一页和{ {1}}数据再次被填充。当用户单击Characteristics_Detail.xaml.cs中的后退按钮时,将触发以下代码:

ListView

MessagingCenter会将我发回DeviceList.xaml.cs中的main方法:

    protected override bool OnBackButtonPressed()
    {
        base.OnBackButtonPressed();
        Xamarin.Forms.MessagingCenter.Send<CharacteristicDetail_TISensor>(this, "DisconnectDeviceAndRefreshDevices");
        Navigation.PopModalAsync();
        return false;
    }

然后导致 public DeviceList (IAdapter adapter) { InitializeComponent (); StartUpJobs(adapter); MessagingCenter.Subscribe<CharacteristicDetail_TISensor>(this, "DisconnectDeviceAndRefreshDevices", (sender) => { //Prints the connected device name - just to check for myself. DisplayAlert("Props", "Name: " + currentDevice.Name, "ok"); //This line will crash: adapter.DisconnectDevice(currentDevice); StartUpJobs(adapter); }); } 崩溃。打印错误:

adapter.DisconnectDevice

我只是不明白为什么[MonoDroid] UNHANDLED EXCEPTION: [MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object [MonoDroid] at Robotics.Mobile.Core.Bluetooth.LE.Device.Disconnect () <0x00018> [MonoDroid] at Robotics.Mobile.Core.Bluetooth.LE.Adapter.DisconnectDevice (Robotics.Mobile.Core.Bluetooth.LE.IDevice) <0x0004b> [MonoDroid] at Robotics.Mobile.BtLEExplorer.DeviceList/<>c__DisplayClass2.<.ctor>b__0 (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor) <0x0007f> [MonoDroid] at Xamarin.Forms.MessagingCenter/<>c__DisplayClass4`1<Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor>.<Subscribe>b__3 (object,object) <0x00097> [MonoDroid] at Xamarin.Forms.MessagingCenter.InnerSend (string,System.Type,System.Type,object,object) <0x0013b> [MonoDroid] at Xamarin.Forms.MessagingCenter.Send<Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor> (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor,string) <0x00047> [MonoDroid] at Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor.OnBackButtonPressed () <0x0002b> [MonoDroid] at Xamarin.Forms.Page.SendBackButtonPressed () <0x0001f> [MonoDroid] at Xamarin.Forms.NavigationPage.OnBackButtonPressed () <0x0001f> [MonoDroid] at Xamarin.Forms.Page.SendBackButtonPressed () <0x0001f> [MonoDroid] at Xamarin.Forms.Platform.Android.Platform.HandleBackPressed (object,System.EventArgs) <0x0005f> [MonoDroid] at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnBackPressed () <0x0004b> [MonoDroid] at Android.App.Activity.n_OnBackPressed (intptr,intptr) <0x00037> [MonoDroid] at (wrapper dynamic-method) object.2c89333f-7791-415b-86be-ca6a8458701f (intptr,intptr) <0x0003b> [art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable [InputEventSender] Exception dispatching finished signal. [MessageQueue-JNI] Exception in MessageQueue callback: handleReceiveCallback [MessageQueue-JNI] md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object [MessageQueue-JNI] at Robotics.Mobile.Core.Bluetooth.LE.Device.Disconnect () <0x00018> [MessageQueue-JNI] at Robotics.Mobile.Core.Bluetooth.LE.Adapter.DisconnectDevice (Robotics.Mobile.Core.Bluetooth.LE.IDevice) <0x0004b> [MessageQueue-JNI] at Robotics.Mobile.BtLEExplorer.DeviceList/<>c__DisplayClass2.<.ctor>b__0 (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor) <0x0007f> [MessageQueue-JNI] at Xamarin.Forms.MessagingCenter/<>c__DisplayClass4`1<Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor>.<Subscribe>b__3 (object,object) <0x00097> [MessageQueue-JNI] at Xamarin.Forms.MessagingCenter.InnerSend (string,System.Type,System.Type,object,object) <0x0013b> [MessageQueue-JNI] at Xamarin.Forms.MessagingCenter.Send<Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor> (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor,string) <0x00047> [MessageQueue-JNI] at Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor.OnBackButtonPressed () <0x0002b> [MessageQueue-JNI] at Xamarin.Forms.Page.SendBackButtonPressed () <0x0001f> [MessageQueue-JNI] at Xamarin.Forms.NavigationPage.OnBackButtonPressed () <0x0001f> [MessageQueue-JNI] at Xamarin.Forms.Page.SendBackButtonPressed () <0x0001f> [MessageQueue-JNI] at Xamarin.Forms.Platform.Android.Platform.HandleBackPressed (object,System.EventArgs) <0x0005f> [MessageQueue-JNI] at Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnBackPressed () <0x0004b> [MessageQueue-JNI] at Android.App.Activity.n_OnBackPressed (intptr,intptr) <0x00037> [MessageQueue-JNI] at (wrapper dynamic-method) object.2c89333f-7791-415b-86be-ca6a8458701f (intptr,intptr) <0x0003b> 确实以预期的currentDevice格式包含设备的详细信息。我的IDevice证明了(正确的)设备在变量中。那么我做错了什么,为什么会崩溃?

谢谢, Yenthe

0 个答案:

没有答案