Task <BluetoothDevice> DoRequestDevice(RequestDeviceOptions options) { #if __IOS__ EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.AutoReset); 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); tvc.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(Task.Run(() => { handle.WaitOne(); return selectedDevice; })); #endif return(Task.FromResult((BluetoothDevice)null)); }
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); 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); }
async Task <BluetoothDevice> DoRequestDevice(RequestDeviceOptions options) { DevicePicker picker = new DevicePicker(); picker.Appearance.AccentColor = Windows.UI.Color.FromArgb(0xff, 0xff, 0xff, 0xff); picker.Appearance.ForegroundColor = Windows.UI.Color.FromArgb(0xff, 0xff, 0xff, 0xff); picker.Appearance.Title = Windows.ApplicationModel.Package.Current.DisplayName + " wants to pair"; if (!options.AcceptAllDevices) { foreach (var filter in options.Filters) { if (!string.IsNullOrEmpty(filter.Name)) { picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromDeviceName(filter.Name)); } foreach (var service in filter.Services) { picker.Filter.SupportedDeviceSelectors.Add(GattDeviceService.GetDeviceSelectorFromUuid(service)); } } } if (picker.Filter.SupportedDeviceSelectors.Count == 0) { //picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelector()); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(true)); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(false)); } var deviceInfo = await picker.PickSingleDeviceAsync(Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds); if (deviceInfo == null) { return(null); } var device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id); return(new BluetoothDevice(device)); }
public static Task <IReadOnlyCollection <BluetoothDevice> > ScanForDevicesAsync(RequestDeviceOptions options = null) { return(PlatformScanForDevices(options)); }
static Task <IReadOnlyCollection <BluetoothDevice> > PlatformScanForDevices(RequestDeviceOptions options) { return(Task.FromResult((IReadOnlyCollection <BluetoothDevice>) new List <BluetoothDevice>().AsReadOnly())); }
/// <summary> /// Performs a device lookup and prompts the user for permission if required. /// </summary> /// <param name="options"></param> /// <returns>A BluetoothDevice or null if unsuccessful.</returns> public static Task <BluetoothDevice> RequestDeviceAsync(RequestDeviceOptions options = null) { return(PlatformRequestDevice(options)); }
static async Task <IReadOnlyCollection <BluetoothDevice> > PlatformScanForDevices(RequestDeviceOptions options) { List <ScanFilter> filters = new List <ScanFilter>(); foreach (var f in options.Filters) { foreach (var u in f.Services) { ScanFilter.Builder b = new ScanFilter.Builder(); b.SetServiceUuid(ParcelUuid.FromString(u.Value.ToString())); filters.Add(b.Build()); } } ScanSettings.Builder sb = new ScanSettings.Builder(); sb.SetScanMode(Android.Bluetooth.LE.ScanMode.Balanced); var settings = sb.Build(); var callback = new DevicesCallback(); _manager.Adapter.BluetoothLeScanner.StartScan(callback); await Task.Delay(30000); return(callback.Devices.AsReadOnly()); }
private static Task <IReadOnlyCollection <BluetoothDevice> > PlatformScanForDevices(RequestDeviceOptions options) { return(Task.FromException <IReadOnlyCollection <BluetoothDevice> >(new PlatformNotSupportedException())); }
/// <summary> /// Performs a device lookup and prompts the user for permission if required. /// </summary> /// <param name="options"></param> /// <returns>A BluetoothDevice or null if unsuccessful.</returns> public static Task <BluetoothDevice> RequestDeviceAsync(RequestDeviceOptions options = null) { ThrowOnInvalidOptions(options); return(PlatformRequestDevice(options)); }
static Task <BluetoothDevice> PlatformRequestDevice(RequestDeviceOptions options) { return(Task.FromResult((BluetoothDevice)null)); }
private static Task <BluetoothDevice> PlatformRequestDevice(RequestDeviceOptions options) { return(Task.FromException <BluetoothDevice>(new PlatformNotSupportedException())); }
Task <BluetoothDevice> DoRequestDevice(RequestDeviceOptions options) { return(Task.FromResult((BluetoothDevice)null)); }
static async Task <BluetoothDevice> PlatformRequestDevice(RequestDeviceOptions options) { DevicePicker picker = new DevicePicker(); Rect bounds = Rect.Empty; //picker.Appearance.AccentColor = Windows.UI.Colors.Green; //picker.Appearance.ForegroundColor = Windows.UI.Colors.White; //picker.Appearance.BackgroundColor = Windows.UI.Colors.DarkGray; #if !UAP uint len = 64; byte[] buffer = new byte[len]; int hasPackage = GetCurrentPackageId(ref len, buffer); if (hasPackage == 0x3d54) { foreach (var attr in System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute))) { picker.Appearance.Title = ((AssemblyProductAttribute)attr).Product + " wants to pair"; break; } } else { picker.Appearance.Title = Windows.ApplicationModel.Package.Current.DisplayName + " wants to pair"; } #else picker.Appearance.Title = Windows.ApplicationModel.Package.Current.DisplayName + " wants to pair"; bounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds; picker.Appearance.SelectedAccentColor = (Color)Windows.UI.Xaml.Application.Current.Resources["SystemAccentColor"]; #endif if (!options.AcceptAllDevices) { foreach (var filter in options.Filters) { if (!string.IsNullOrEmpty(filter.Name)) { picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromDeviceName(filter.Name)); } foreach (var service in filter.Services) { picker.Filter.SupportedDeviceSelectors.Add(GattDeviceService.GetDeviceSelectorFromUuid(service)); } } } if (picker.Filter.SupportedDeviceSelectors.Count == 0) { //picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelector()); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(true)); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(false)); } var deviceInfo = await picker.PickSingleDeviceAsync(bounds); if (deviceInfo == null) { return(null); } var device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id); var access = await device.RequestAccessAsync(); return(new BluetoothDevice(device)); }
static async Task <IReadOnlyCollection <BluetoothDevice> > PlatformScanForDevices(RequestDeviceOptions options) { List <BluetoothDevice> devices = new List <BluetoothDevice>(); foreach (var device in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromPairingState(false))) { devices.Add(await BluetoothLEDevice.FromIdAsync(device.Id)); } return(devices.AsReadOnly()); }
/// <summary> /// Performs a device lookup and prompts the user for permission if required. /// </summary> /// <param name="options"></param> /// <returns>A BluetoothDevice or null if unsuccessful.</returns> public async Task <BluetoothDevice> RequestDevice(RequestDeviceOptions options) { return(await DoRequestDevice(options)); }
static async Task <BluetoothDevice> PlatformRequestDevice(RequestDeviceOptions options) { DevicePicker picker = new DevicePicker(); Rect bounds = Rect.Empty; #if !UAP uint len = 64; byte[] buffer = new byte[len]; IntPtr hwnd = IntPtr.Zero; try { // a console app will return a non-null string for title if (!string.IsNullOrEmpty(Console.Title)) { bounds = new Rect(0, 0, 480, 480); hwnd = GetConsoleWindow(); // set console host window as parent for picker ((IInitializeWithWindow)(object)picker).Initialize(hwnd); } } catch { } int hasPackage = GetCurrentPackageId(ref len, buffer); if (hasPackage == 0x3d54) { foreach (var attr in System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute))) { picker.Appearance.Title = ((AssemblyProductAttribute)attr).Product + " wants to pair"; break; } } else { picker.Appearance.Title = Windows.ApplicationModel.Package.Current.DisplayName + " wants to pair"; } #else picker.Appearance.Title = Windows.ApplicationModel.Package.Current.DisplayName + " wants to pair"; bounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds; picker.Appearance.SelectedAccentColor = (Color)Windows.UI.Xaml.Application.Current.Resources["SystemAccentColor"]; #endif if (options != null && !options.AcceptAllDevices) { foreach (var filter in options.Filters) { if (!string.IsNullOrEmpty(filter.Name)) { picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromDeviceName(filter.Name)); } foreach (var service in filter.Services) { picker.Filter.SupportedDeviceSelectors.Add(GattDeviceService.GetDeviceSelectorFromUuid(service)); } } } if (picker.Filter.SupportedDeviceSelectors.Count == 0) { //picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelector()); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(true)); picker.Filter.SupportedDeviceSelectors.Add(BluetoothLEDevice.GetDeviceSelectorFromPairingState(false)); } try { var deviceInfo = await picker.PickSingleDeviceAsync(bounds); if (deviceInfo == null) { return(null); } var device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id); var access = await device.RequestAccessAsync(); return(new BluetoothDevice(device)); } catch (Exception ex) { if (ex.HResult == -2147023496) { throw new PlatformNotSupportedException("RequestDevice cannot be called from a Console application."); } return(null); } }
static async Task <IReadOnlyCollection <BluetoothDevice> > PlatformScanForDevices(RequestDeviceOptions options) { List <BluetoothDevice> devices = new List <BluetoothDevice>(); // None of the build in selectors do a scan and return both paired and unpaired devices so here is the raw AQS string foreach (var device in await DeviceInformation.FindAllAsync("System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" AND (System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False OR System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#True)")) { try { devices.Add(await BluetoothLEDevice.FromIdAsync(device.Id)); } catch (System.ArgumentException) { } } return(devices.AsReadOnly()); }