コード例 #1
0
        //Method is responsible for listening if a bluetoothdevice is in range
        private async void StartWatcher()

        {
            string[] request = { "System.Devices.Aep.DeviceAddress" };

            /*3 Filters(AQS - Advanced Query Syntax) are set:
             * 1. Pairingstate should be false (so only non paired devices discovery);
             * 2. The Device should have a device Adress (no Devices with name "Unknown Device");
             * 3. AEP for better Perfomance on Device
             *
             */


            DeviceWatcher watcher = DeviceInformation.CreateWatcher(
                //BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                BluetoothLEDevice.GetDeviceSelectorFromDeviceName("ambiotex"),
                request,
                DeviceInformationKind.AssociationEndpoint);

            //what happens if a device is recognized or not
            watcher.Added   += DeviceWatcher_Added;
            watcher.Removed += DeviceWatcher_Removed;


            watcher.Start();
        }
コード例 #2
0
        private async static void DevicesWithNameAsync(string name)
        {
            List <GattDevice>           returnDevices = new List <GattDevice>();
            string                      filter        = BluetoothLEDevice.GetDeviceSelectorFromDeviceName(name);
            DeviceInformationCollection infos         = await DeviceInformation.FindAllAsync(filter);

            if (infos.Count > 0)
            {
                Debug.Log("Found " + infos.Count + " Devices");
                foreach (DeviceInformation info in infos)
                {
                    string deviceID = info.Id;
                    Debug.Log("Device Name: " + info.Name);
                    try
                    {
                        BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceID);

                        GattDevice d = GattDevice.Create(device);
                        returnDevices.Add(d);
                    }
                    catch { }
                }
            }

            OnDevicesAcquired?.Invoke(returnDevices);
        }
コード例 #3
0
        public PairedNuimoManager()
        {
            _deviceWatcher =
                DeviceInformation.CreateWatcher(
                    BluetoothLEDevice.GetDeviceSelectorFromDeviceName("Nuimo"),
                    null,
                    DeviceInformationKind.AssociationEndpoint);

            _deviceWatcher.Added   += _deviceWatcher_Added;
            _deviceWatcher.Removed += _deviceWatcher_Removed;
        }
コード例 #4
0
 static void QueryForDevices()
 {
     watcher = DeviceInformation.CreateWatcher(
         BluetoothLEDevice.GetDeviceSelectorFromDeviceName(deviceName),
         requestedProperties,
         DeviceInformationKind.AssociationEndpoint);
     watcher.Added   += DeviceAdded;
     watcher.Updated += DeviceUpdated;
     watcher.EnumerationCompleted += DeviceEnumerationComplete;
     watcher.Stopped += DeviceStopped;
     watcher.Start();
 }
コード例 #5
0
        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));
        }
コード例 #6
0
        public void When_GetSelector()
        {
            string testSelector;


            testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)";
            Assert.AreEqual(testSelector, BluetoothLEDevice.GetDeviceSelector());

            testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)";
            Assert.AreEqual(testSelector, BluetoothLEDevice.GetDeviceSelectorFromPairingState(true));
            testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False OR " + _deviceSelectorIssueInquiry + "#True)";
            Assert.AreEqual(testSelector, BluetoothLEDevice.GetDeviceSelectorFromPairingState(false));


            testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)";
            Assert.AreEqual(testSelector, BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected));
            testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#False OR " + _deviceSelectorIssueInquiry + "#True)";
            Assert.AreEqual(testSelector, BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Disconnected));

            string deviceName = "TESTNAME";

            testSelector = _deviceSelectorPrefix + "(System.ItemNameDisplay:=\"" + deviceName + "\" OR " + _deviceSelectorIssueInquiry + "#True)";
            Assert.AreEqual(testSelector, BluetoothLEDevice.GetDeviceSelectorFromDeviceName(deviceName));
        }
コード例 #7
0
        /// <summary>
        /// When the user presses the request button, ask for permission to use the given device. This
        /// may take 30 seconds.
        /// </summary>
        /// <param name="sender">Instance that triggered the event.</param>
        /// <param name="e">Event data describing the conditions that led to the event.</param>
        private async void RequestButton_Click(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser("Getting permission to device..", NotifyType.StatusMessage);

            // Find the device
            string deviceSelector = BluetoothLEDevice.GetDeviceSelectorFromDeviceName(this.peripheralDeviceName);
            var    devices        = await DeviceInformation.FindAllAsync(deviceSelector);

            // Return if we don't find a device - this will happen if the device we are searching for is not paired
            if (devices.Count == 0)
            {
                rootPage.NotifyUser("Device not found. Make sure it is paired.", NotifyType.ErrorMessage);
                return;
            }

            // Try to access the device to trigger the permissions dialogue. Immediately dispose the device so it is not
            // connected to us and we can view advertisements from it in the background.
            DeviceInformation firstDevice = devices[0];
            BluetoothLEDevice device      = await BluetoothLEDevice.FromIdAsync(firstDevice.Id);

            device.Dispose();

            rootPage.NotifyUser("Received permission to device.", NotifyType.StatusMessage);
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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);
            }
        }