private async void OnDeviceAdded(DeviceWatcher deviceWatcher, DeviceInformation deviceInfo)
 {
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
         DiscoveredDevices.Add(new DiscoveredDevice(deviceInfo));
     });
 }
예제 #2
0
 private void OnServiceAdded(object sender, ServiceAnnouncementEventArgs e)
 {
     ServiceAddedSemaphoreSlim.Wait();
     try
     {
         var txtValues = e.Announcement.Txt
                         .Select(i => i.Split('='))
                         .ToDictionary(y => y[0], y => y[1]);
         if (!txtValues.ContainsKey("fn"))
         {
             return;
         }
         var ip = e.Announcement.Addresses[0];
         Uri.TryCreate("https://" + ip, UriKind.Absolute, out Uri myUri);
         var chromecast = new ChromecastReceiver
         {
             DeviceUri        = myUri,
             Name             = txtValues["fn"],
             Model            = txtValues["md"],
             Version          = txtValues["ve"],
             ExtraInformation = txtValues,
             Status           = txtValues["rs"],
             Port             = e.Announcement.Port
         };
         ChromecastReceivedFound?.Invoke(this, chromecast);
         DiscoveredDevices.Add(chromecast);
     }
     finally
     {
         ServiceAddedSemaphoreSlim.Release();
     }
 }
예제 #3
0
        /// <summary>
        /// Raises the le scan event.
        /// </summary>
        /// <param name="bleDevice">The BLE device that was discovered.</param>
        /// <param name="rssi">Rssi.</param>
        /// <param name="scanRecord">Scan record.</param>
        public void OnLeScan(BluetoothDevice bleDevice, int rssi, byte[] scanRecord)
        {
            var deviceId = Device.DeviceIdFromAddress(bleDevice.Address);

            if (DiscoveredDevices.All(x => x.Id != deviceId))
            {
                var device = new Device(bleDevice, null, null, rssi, scanRecord);
                DiscoveredDevices.Add(device);
                DeviceDiscovered(this, new DeviceDiscoveredEventArgs(device));
            }
        }
예제 #4
0
        private void DiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
        {
            var deviceId = Device.DeviceIdentifierToGuid(e.Peripheral.Identifier);

            if (DiscoveredDevices.All(x => x.Id != deviceId))
            {
                var device = new Device(e.Peripheral);
                DiscoveredDevices.Add(device);
                DeviceDiscovered(this, new DeviceDiscoveredEventArgs(device));
            }
        }
        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);
            };
        }
예제 #6
0
        public void OnLeScan(BluetoothDevice bleDevice, int rssi, byte[] scanRecord)
        {
            Console.WriteLine("Adapter.LeScanCallback: " + bleDevice.Name);
            // TODO: for some reason, this doesn't work, even though they have the same pointer,
            // it thinks that the item doesn't exist. so i had to write my own implementation
            //			if(!_discoveredDevices.Contains(device) ) {
            //				_discoveredDevices.Add (device );
            //			}

            Device device = new Device(bleDevice, null, null, rssi);

            if (!DeviceExistsInDiscoveredList(bleDevice))
            {
                DiscoveredDevices.Add(device);

                // TODO: in the cross platform API, cache the RSSI
                // TODO: shouldn't i only raise this if it's not already in the list?
                DeviceDiscovered(this, new DeviceDiscoveredEventArgs {
                    Device = device
                });
            }
        }
예제 #7
0
        protected Adapter()
        {
            CentralManager = new CBCentralManager(DispatchQueue.CurrentQueue);

            CentralManager.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) =>
            {
                Console.WriteLine("DiscoveredPeripheral: " + e.Peripheral.Name);
                Device d = new Device(e.Peripheral);
                if (!ContainsDevice(DiscoveredDevices, e.Peripheral))
                {
                    DiscoveredDevices.Add(d);
                    DeviceDiscovered(this, new DeviceDiscoveredEventArgs()
                    {
                        Device = d
                    });
                }
            };

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

            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 (!ContainsDevice(ConnectedDevices, e.Peripheral))
                {
                    Device d = new Device(e.Peripheral);
                    ConnectedDevices.Add(new Device(e.Peripheral));
                    // raise our connected event
                    DeviceConnected(sender, new DeviceConnectionEventArgs()
                    {
                        Device = d
                    });
                }
            };

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

                // when a peripheral disconnects, remove it from our running list.
                IDevice foundDevice = null;
                foreach (var d in ConnectedDevices)
                {
                    if (d.ID == Guid.ParseExact(e.Peripheral.Identifier.AsString(), "d"))
                    {
                        foundDevice = d;
                    }
                }

                if (foundDevice != null)
                {
                    ConnectedDevices.Remove(foundDevice);
                }

                // raise our disconnected event
                DeviceDisconnected(sender, new DeviceConnectionEventArgs()
                {
                    Device = new Device(e.Peripheral)
                });
            };

            CentralManager.FailedToConnectPeripheral += (object sender, CBPeripheralErrorEventArgs e) =>
            {
                // raise the failed to connect event
                DeviceFailedToConnect(this, new DeviceConnectionEventArgs()
                {
                    Device       = new Device(e.Peripheral),
                    ErrorMessage = e.Error.Description
                });
            };
        }