Esempio n. 1
0
        static async Task <BluetoothDevice> PlatformRequestDevice(RequestDeviceOptions options)
        {
#if __IOS__
            EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.ManualReset);

            if (_manager.State != CBCentralManagerState.PoweredOn)
            {
                throw new InvalidOperationException();
            }

            BluetoothDevice selectedDevice = null;

            controller = UIAlertController.Create("Select a Bluetooth accessory", null, UIAlertControllerStyle.Alert);
            controller.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (a) => {
                handle.Set();
                System.Diagnostics.Debug.WriteLine(a == null ? "<null>" : a.ToString());
            }));

            CGRect rect = new CGRect(0, 0, 272, 272);
            var    tvc  = new UITableViewController(UITableViewStyle.Plain)
            {
                PreferredContentSize = rect.Size
            };
            controller.PreferredContentSize = rect.Size;
            var source = new InTheHand.Bluetooth.Platforms.Apple.BluetoothTableViewSource(options);
            source.DeviceSelected += (s, e) =>
            {
                selectedDevice = e;
                handle.Set();
                tvc.DismissViewController(true, null);
            };

            tvc.TableView.Delegate   = source;
            tvc.TableView.DataSource = source;

            tvc.TableView.UserInteractionEnabled = true;
            tvc.TableView.AllowsSelection        = true;
            //controller.AddChildViewController(contentController);
            controller.SetValueForKey(tvc, new Foundation.NSString("contentViewController"));

            UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
            while (currentController.PresentedViewController != null)
            {
                currentController = currentController.PresentedViewController;
            }

            currentController.PresentViewController(controller, true, null);

            return(await Task.Run(() =>
            {
                var s2 = source;
                handle.WaitOne();
                return selectedDevice;
            }));
#endif
            return(null);
        }
Esempio n. 2
0
        static Task <BluetoothDevice> PlatformRequestDevice(RequestDeviceOptions options)
        {
#if __IOS__
            TaskCompletionSource <BluetoothDevice> tcs = new TaskCompletionSource <BluetoothDevice>();

            if (_manager.State != CBCentralManagerState.PoweredOn)
            {
                throw new InvalidOperationException();
            }

            controller = UIAlertController.Create("Select a Bluetooth accessory", null, UIAlertControllerStyle.Alert);
            controller.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (a) => {
                tcs.SetResult(null);
                StopScanning();
                Debug.WriteLine(a == null ? "<null>" : a.ToString());
            }));

            CGRect rect = new CGRect(0, 0, 272, 272);
            var    tvc  = new UITableViewController(UITableViewStyle.Plain)
            {
                PreferredContentSize = rect.Size
            };
            controller.PreferredContentSize = rect.Size;
            var source = new InTheHand.Bluetooth.Platforms.Apple.BluetoothTableViewSource(options);
            source.DeviceSelected += (s, e) =>
            {
                tvc.DismissViewController(true, null);
                tcs.SetResult(e);
            };

            tvc.TableView.Delegate   = source;
            tvc.TableView.DataSource = source;

            tvc.TableView.UserInteractionEnabled = true;
            tvc.TableView.AllowsSelection        = true;
            controller.SetValueForKey(tvc, new Foundation.NSString("contentViewController"));

            UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
            while (currentController.PresentedViewController != null)
            {
                currentController = currentController.PresentedViewController;
            }

            currentController.PresentViewController(controller, true, null);

            return(tcs.Task);
#endif
            return(null);
        }