private async void DeviceFound(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs btAdv)
 {
     if (!ConnectedDevices.Contains(btAdv.Advertisement.LocalName) && _devices.Contains(btAdv.Advertisement.LocalName))
     {
         await Dispatcher.RunAsync(CoreDispatcherPriority.Low, async() =>
         {
             var device = await BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress);
             if (device.GattServices.Any())
             {
                 ConnectedDevices.Add(device.Name);
                 device.ConnectionStatusChanged += async(sender, args) =>
                 {
                     if (sender.ConnectionStatus == BluetoothConnectionStatus.Disconnected)
                     {
                         await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                         {
                             ConnectedDevices
                             .Remove(
                                 sender
                                 .Name);
                         });
                     }
                 };
                 SetupWaxStream(device);
             }
             else if (device.DeviceInformation.Pairing.CanPair && !device.DeviceInformation.Pairing.IsPaired)
             {
                 await device.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None);
             }
         });
     }
 }
        protected BluetoothLEManager()
        {
            CentralManager = new CBCentralManager(DispatchQueue.CurrentQueue);
            CentralManager.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) =>
            {
                Console.WriteLine("DiscoveredPeripheral: " + e.Peripheral.Name);
                DiscoveredDevices.Add(e.Peripheral);
                DeviceDiscovered(this, e);
            };

            CentralManager.UpdatedState += (object sender, EventArgs e) =>
            {
                Console.WriteLine("UpdatedState: " + CentralManager.State);
            };


            CentralManager.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) =>
            {
                Console.WriteLine("ConnectedPeripheral: " + e.Peripheral.Name);

                // When a peripheral gets connected, add that peripheral to our running list of
                // connected peripherals
                if (!ConnectedDevices.Contains(e.Peripheral))
                {
                    ConnectedDevices.Add(e.Peripheral);
                }

                // raise our connected event
                DeviceConnected(sender, e);
            };

            CentralManager.DisconnectedPeripheral += (object sender, CBPeripheralErrorEventArgs e) =>
            {
                Console.WriteLine("DisconnectedPeripheral: " + e.Peripheral.Name);

                // When a peripheral disconnects, remove it from our running list.
                if (ConnectedDevices.Contains(e.Peripheral))
                {
                    ConnectedDevices.Remove(e.Peripheral);
                }

                // Raise our disconnected event
                DeviceDisconnected(sender, e);
            };
        }
Exemple #3
0
        protected BluetoothLeManager()
        {
            CentralBleManager = new CBCentralManager(DispatchQueue.CurrentQueue);
            DiscoveredDevices = new List <CBPeripheral>();
            CentralBleManager.DiscoveredPeripheral += (sender, e) =>
            {
                Mvx.Trace("DiscoveredPeripheral: {0}", e.Peripheral.Name);
                DiscoveredDevices.Add(e.Peripheral);
                DeviceDiscovered(this, e);
            };

            CentralBleManager.UpdatedState +=
                (sender, e) => { Mvx.Trace("UpdatedState: {0}", CentralBleManager.State); };

            CentralBleManager.ConnectedPeripheral += (sender, e) =>
            {
                Mvx.Trace("ConnectedPeripheral: " + e.Peripheral.Name);

                // when a peripheral gets connected, add that peripheral to our running list of connected peripherals
                if (!ConnectedDevices.Contains(e.Peripheral))
                {
                    ConnectedDevices.Add(e.Peripheral);
                }

                // raise our connected event
                DeviceConnected(sender, e);
            };

            CentralBleManager.DisconnectedPeripheral += (sender, e) =>
            {
                Mvx.Trace("DisconnectedPeripheral: " + e.Peripheral.Name);

                // when a peripheral disconnects, remove it from our running list.
                if (ConnectedDevices.Contains(e.Peripheral))
                {
                    ConnectedDevices.Remove(e.Peripheral);
                }

                // raise our disconnected event
                DeviceDisconnected(sender, e);
            };
        }