Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DisconnectDevice crashes app #34

Open
Yenthe666 opened this issue Apr 30, 2015 · 1 comment
Open

DisconnectDevice crashes app #34

Yenthe666 opened this issue Apr 30, 2015 · 1 comment

Comments

@Yenthe666
Copy link
Contributor

Hi guys,

First of all, great project! It works pretty good but I'm now having a problem when I want to disconnect my BLE device.

        public DeviceList (IAdapter adapter)
        {
            InitializeComponent ();
            StartUpJobs(adapter);
            MessagingCenter.Subscribe<CharacteristicDetail_TISensor>(this, "DisconnectDeviceAndRefreshDevices", (sender) =>
            {
                //This prints the name of the device I'm connected with.
                DisplayAlert("Props", "Name: " + currentDevice.Name, "ok");
                //Disconnect it
                adapter.DisconnectDevice(currentDevice);
                StartUpJobs(adapter);
            });
        }

The variable currentDevice gets filled in another method:

 void ServiceList(IAdapter adapter, IDevice device)
        {
this.serviceListadapter = adapter;
            this.serviceListdevice = device;
            this.services = new ObservableCollection<IService>();
            //listView.ItemsSource = services;

            if (services.Count == 0)
            {
                Debug.WriteLine("No services, attempting to connect to device");
                // start looking for the device
                adapter.ConnectToDevice(device);
                currentDevice = device;
            }
         }

When the user clicks on a device he goes to a detailpage. When the user then presses the back button I use the MessageCenter to notify my page to do something:
CharacteristicDetail_TISensor.xaml.cs:

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

When I execute my app it will crash on this line:

adapter.DisconnectDevice(currentDevice);

Error log:

[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__DisplayClass0.<.ctor>b__1 (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.9a29b4dc-78cd-4d75-abb3-df0633e117be (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__DisplayClass0.<.ctor>b__1 (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.9a29b4dc-78cd-4d75-abb3-df0633e117be (intptr,intptr) <0x0003b>
[MessageQueue-JNI] 
[MessageQueue-JNI]  at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.n_onBackPressed(Native Method)
[MessageQueue-JNI]  at md5530bd51e982e6e7b340b73e88efe666e.FormsApplicationActivity.onBackPressed(FormsApplicationActivity.java:94)
[MessageQueue-JNI]  at android.app.Activity.onKeyUp(Activity.java:2456)
[MessageQueue-JNI]  at android.view.KeyEvent.dispatch(KeyEvent.java:2643)
[MessageQueue-JNI]  at android.app.Activity.dispatchKeyEvent(Activity.java:2707)
[MessageQueue-JNI]  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
[MessageQueue-JNI]  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
[MessageQueue-JNI]  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
[MessageQueue-JNI]  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
[MessageQueue-JNI]  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
[MessageQueue-JNI]  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
[MessageQueue-JNI]  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)
[MessageQueue-JNI]  at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
[MessageQueue-JNI]  at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
[MessageQueue-JNI]  at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849)
[MessageQueue-JNI]  at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840)
[MessageQueue-JNI]  at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185)
[MessageQueue-JNI]  at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
[MessageQueue-JNI]  at android.os.MessageQueue.nativePollOnce(Native Method)
[MessageQueue-JNI]  at android.os.MessageQueue.next(MessageQueue.java:143)
[MessageQueue-JNI]  at android.os.Looper.loop(Looper.java:122)
[MessageQueue-JNI]  at android.app.ActivityThread.main(ActivityThread.java:5254)
[MessageQueue-JNI]  at 
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: Robotics.Mobile.BtLEExplorer.Droid, PID: 18171
[AndroidRuntime] java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
[AndroidRuntime]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
[AndroidRuntime] Caused by: java.lang.reflect.InvocationTargetException
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Method.java:372)
[AndroidRuntime]    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
[AndroidRuntime]    ... 1 more
[AndroidRuntime] Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
[AndroidRuntime] at Robotics.Mobile.Core.Bluetooth.LE.Device.Disconnect () <0x00018>
[AndroidRuntime] at Robotics.Mobile.Core.Bluetooth.LE.Adapter.DisconnectDevice (Robotics.Mobile.Core.Bluetooth.LE.IDevice) <0x0004b>
[AndroidRuntime] at Robotics.Mobile.BtLEExplorer.DeviceList/<>c__DisplayClass0.<.ctor>b__1 (Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor) <0x0007f>
[AndroidRuntime] at Xamarin.Forms.MessagingCenter/<>c__DisplayClass4`1<Robotics.Mobile.BtLEExplorer.CharacteristicDetail_TISensor>.<Subscribe>b__3 (object,object) <0x00097>
...
@Yenthe666
Copy link
Contributor Author

Well, turns out this was partially my issue. You cannot disconnect on the adapter from the DeviceList etc but only when you're in ServiceList. But still, this should be fixed I assume.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants