Procházet zdrojové kódy

MOBILE - prevention of errors / better error handling

Nick-PRSDigital@bitbucket.org před 2 roky
rodič
revize
9f2e593ab7

+ 13 - 9
InABox.Mobile/Shared/Bluetooth.cs

@@ -27,7 +27,7 @@ namespace InABox.Mobile
         public List<string> SavedBlueToothMACAddresses { get; set; }
 
         private Dictionary<String, IDevice> _devicemap = new Dictionary<string, IDevice>();
-        
+
         IBluetoothLE bluetooth = null;
         private bool bScanning = false;
 
@@ -74,20 +74,20 @@ namespace InABox.Mobile
             if (!_disabled)
                 throw new Exception("BT Scanning must be disabled!");
 
-            _devicemap.TryGetValue(macaddress,out IDevice device);
+            _devicemap.TryGetValue(macaddress, out IDevice device);
             if (device == null)
                 throw new Exception("Device not Found!");
-            
+
             try
             {
                 await bluetooth.Adapter.ConnectToDeviceAsync(device);
                 if (!bluetooth.Adapter.ConnectedDevices.Contains(device))
                     throw new Exception("Cannot Connect to Device!");
-                
+
                 var service = await device.GetServiceAsync(serviceid);
                 if (service == null)
                     throw new Exception("Service Not Found!");
-                
+
                 var characteristic = await service.GetCharacteristicAsync(characteristicid);
                 if (characteristic == null)
                     throw new Exception("Characteristic Not Found!");
@@ -104,7 +104,7 @@ namespace InABox.Mobile
                 throw;
             }
         }
-        
+
         public bool IsScanning { get; private set; }
 
         private async Task<bool> UnlockDevice(IDevice idevice, String key)
@@ -158,8 +158,12 @@ namespace InABox.Mobile
                 {
                     if (string.IsNullOrWhiteSpace(e.Device.NativeDevice.ToString()))
                         return;
-                    _devicemap[e.Device.NativeDevice.ToString()] = e.Device;
-                    
+
+                    if (_devicemap.ContainsKey(e.Device.NativeDevice.ToString()))
+                        _devicemap[e.Device.NativeDevice.ToString()] = e.Device;
+                    else
+                        return;
+
                     if (KnownBlueToothMACAddresses.Count == 0)
                         return;
 
@@ -222,7 +226,7 @@ namespace InABox.Mobile
         {
             if (_disabled)
                 return;
-            
+
             try
             {
                 if (bluetooth.Adapter.IsScanning)

+ 4 - 0
InABox.Mobile/Shared/Geolocation.cs

@@ -140,6 +140,10 @@ namespace InABox.Mobile
                                     }
 
                                 }
+                                catch (Java.IO.IOException ioex)
+                                {
+                                    return;
+                                }
                                 catch (Exception ee2)
                                 {
                                     sErr = ee2.Message;

+ 16 - 9
InABox.Mobile/Shared/MobileUtils.cs

@@ -146,15 +146,22 @@ namespace InABox.Mobile
 
         public static async Task<bool> IsPermitted(PermissionType permissiontype)
         {
-            Permission permission = (Permission)permissiontype;
-            
-            PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
-            if (status == PermissionStatus.Granted)
-                return true;
-            Dictionary<Permission,PermissionStatus> statuses = await CrossPermissions.Current.RequestPermissionsAsync(permission);
-            if (statuses[permission] == PermissionStatus.Granted)
-                return true;
-            return false;
+            try
+            {
+                Permission permission = (Permission)permissiontype;
+
+                PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
+                if (status == PermissionStatus.Granted)
+                    return true;
+                Dictionary<Permission, PermissionStatus> statuses = await CrossPermissions.Current.RequestPermissionsAsync(permission);
+                if (statuses[permission] == PermissionStatus.Granted)
+                    return true;
+                return false;
+            }
+            catch (System.Threading.Tasks.TaskCanceledException ex)
+            {
+                return false;
+            }            
 
             //request.Wait();
             //var statuses = request.Result;