public void StartScanning(Func <MWDevice, bool> whenFound)
        {
            StopScanning();

            CrossBleAdapter.Current.WhenStatusChanged().Subscribe(status =>
            {
                if (status == AdapterStatus.PoweredOn)
                {
                    scan = CrossBleAdapter.Current.ScanOrListen().Subscribe(scanResult =>
                    {
                        var devGuid = scanResult.Device.Uuid;
                        if (!seenDevices.ContainsKey(devGuid))
                        {
                            // TODO: Verify this is actually a MetaWear device
                            //var serviceGuid = config.ServiceUuids.First();
                            //var service = await scanResult.Device.GetKnownService(serviceGuid).FirstOrDefaultAsync();
                            if (scanResult.Device.Name == "MetaWear")
                            {
                                MWDevice mwdevice = new MWDevice(scanResult.Device);
                                seenDevices.Add(devGuid, mwdevice);
                                if (whenFound(mwdevice))
                                {
                                    StopScanning();
                                }
                            }
                        }
                    });
                }
            });
        }
Beispiel #2
0
 void StartScanning(Func <MWDevice, bool> whenFound)
 {
     StopScanning();
     scan = CrossBleAdapter.Current.ScanOrListen().Subscribe(scanResult =>
     {
         var name    = scanResult.Device.Name;
         var devGuid = scanResult.Device.Uuid;
         if (!seenDevices.ContainsKey(devGuid))
         //if (!seenDevices.Contains(scanResult.Device))
         //if (config.ServiceUuids.Aggregate(true, (acc, e) => acc & scanResult.AdvertisementData.ServiceUuids.Contains(e)))
         {
             // TODO: Verify this is actually a MetaWear device
             var uuids = scanResult.AdvertisementData.ServiceUuids;
             foreach (Guid uuid in uuids)
             {
                 var st = uuid.ToString();
             }
             var s = scanResult.AdvertisementData.ToString();
             //var serviceGuid = config.ServiceUuids.First();
             //var service = await scanResult.Device.GetKnownService(serviceGuid).FirstOrDefaultAsync();
             //if (service != null)
             {
                 MWDevice mwdevice = new MWDevice(scanResult.Device);
                 seenDevices.Add(devGuid, mwdevice);
                 if (whenFound(mwdevice))
                 {
                     StopScanning();
                 }
             }
         }
     });
 }
        /// <summary>
        /// Instantiates an <see cref="IMetaWearBoard"/> object corresponding to the BluetoothLE device
        /// </summary>
        /// <param name="device">BluetoothLE device object corresponding to the target MetaWear board</param>
        /// <returns><see cref="IMetaWearBoard"/> object</returns>
        public static IMetaWearBoard GetMetaWearBoard(MWDevice device)
        {
            if (btleDevices.TryGetValue(device.Name, out var board))
            {
                return(board);
            }

            board = new MetaWearBoard(new BLEBridge(device), new IO(device.Name));
            btleDevices.Add(device.Name, board);
            return(board);
        }
        //private Dictionary<Guid, IGattCharacteristic> characteristics;

        public BLEBridge(MWDevice mwdevice)
        {
            device = mwdevice.device;

            device.WhenStatusChanged().Subscribe(status =>
            {
                switch (status)
                {
                case ConnectionStatus.Connecting:
                    System.Diagnostics.Debug.WriteLine("MetaWear connecting!");
                    break;

                case ConnectionStatus.Connected:
                    System.Diagnostics.Debug.WriteLine("MetaWear connected!");
                    break;

                case ConnectionStatus.Disconnecting:
                    System.Diagnostics.Debug.WriteLine("MetaWear disconnecting!");
                    break;

                case ConnectionStatus.Disconnected:
                    System.Diagnostics.Debug.WriteLine("MetaWear disconnected!");
                    _notification?.Dispose();
                    _notification = null;
                    if (dcTaskSource != null)
                    {
                        dcTaskSource.SetResult(true);
                        if (OnDisconnect != null)
                        {
                            OnDisconnect(false);
                        }
                    }
                    else
                    {
                        if (OnDisconnect != null)
                        {
                            OnDisconnect(true);
                        }
                    }
                    break;
                }
                ;
            });

            Connect();
            //connection = device.Connect().Wait();

            // Cache all characteristics?
            //characteristics = new Dictionary<Guid, IGattCharacteristic>();
            //device.WhenAnyCharacteristicDiscovered().Subscribe(ch =>
            //{
            //    characteristics.Add(ch.Uuid, ch);
            //});
        }
 public void AddDevice(MWDevice device)
 {
     seenDevices.Add(device.Uuid, device);
 }