示例#1
0
        private async void StartScan(Double seconds)
        {
            if (IsScanning)
            {
                return;
            }

            if (!IsAdapterEnabled)
            {
                m_dialogs.Toast("Cannot start scan, Bluetooth is turned off");
                return;
            }

            StopScan();
            IsScanning     = true;
            seconds        = BleSampleAppUtils.ClampSeconds(seconds);
            m_scanCancel   = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
            m_scanStopTime = DateTime.UtcNow.AddSeconds(seconds);

            Log.Trace("Beginning device scan. timeout={0} seconds", seconds);

            RaisePropertyChanged(nameof(ScanTimeRemaining));

            Device.StartTimer(
                TimeSpan.FromSeconds(1),
                () =>
            {
                RaisePropertyChanged(nameof(ScanTimeRemaining));
                return(IsScanning);
            });

            await m_bleAdapter.ScanForBroadcasts(


                new ScanFilter().AddAdvertisedService("0000fe9a-0000-1000-8000-00805f9b34fb"),

                peripheral =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    var temp     = new BleDevice(peripheral);
                    var existing = FoundDevices.FirstOrDefault(d => d.Address == peripheral.Address.Select(b => b.EncodeToBase16String()).Join(":"));
                    if (existing != null)
                    {
                        existing.Update(temp.abs);
                    }
                    else
                    {
                        FoundDevices.Add(new BlePeripheralViewModel(temp));
                    }
                });
            },
                m_scanCancel.Token);

            IsScanning = false;
        }
示例#2
0
        private async void StartScan(Double seconds)
        {
            if (IsScanning)
            {
                return;
            }

            if (!IsAdapterEnabled)
            {
                m_dialogs.Toast("Cannot start scan, Bluetooth is turned off");
                return;
            }

            StopScan();
            IsScanning     = true;
            seconds        = BleSampleAppUtils.ClampSeconds(seconds);
            m_scanCancel   = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
            m_scanStopTime = DateTime.UtcNow.AddSeconds(seconds);

            Log.Trace("Beginning device scan. timeout={0} seconds", seconds);

            RaisePropertyChanged(nameof(ScanTimeRemaining));
            // RaisePropertyChanged of ScanTimeRemaining while scan is running
            Device.StartTimer(
                TimeSpan.FromSeconds(1),
                () =>
            {
                RaisePropertyChanged(nameof(ScanTimeRemaining));
                return(IsScanning);
            });

            await m_bleAdapter.ScanForBroadcasts(
                // NOTE: You can provide a scan filter to look for particular devices, e.g.:
                //new ScanFilter.Factory {AdvertisedManufacturerCompanyId = BleSampleAppUtils.COMPANY_ID_GOOGLE}.CreateFilter(),
                peripheral =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    var existing = FoundDevices.FirstOrDefault(d => d.Equals(peripheral));
                    if (existing != null)
                    {
                        existing.Update(peripheral);
                    }
                    else
                    {
                        FoundDevices.Add(new BlePeripheralViewModel(peripheral, m_connectionFunc));
                    }
                });
            },
                m_scanCancel.Token);

            IsScanning = false;
        }
        private async void StartScan(Double seconds)
        {
#if __ANDROID__
#endif
            if (IsScanning)
            {
                return;
            }

            if (!IsAdapterEnabled)
            {
                m_dialogs.Toast("Cannot start scan, Bluetooth is turned off");
                return;
            }

            StopScan();
            IsScanning     = true;
            seconds        = BleSampleAppUtils.ClampSeconds(seconds);
            m_scanCancel   = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
            m_scanStopTime = DateTime.UtcNow.AddSeconds(seconds);

            Log.Trace("Beginning device scan. timeout={0} seconds", seconds);

            RaisePropertyChanged(nameof(ScanTimeRemaining));
            // RaisePropertyChanged of ScanTimeRemaining while scan is running
            Device.StartTimer(
                TimeSpan.FromSeconds(1),
                () =>
            {
                RaisePropertyChanged(nameof(ScanTimeRemaining));
                return(IsScanning);
            });

            await m_bleAdapter.ScanForBroadcasts(
                // NOTE:
                //
                // You can provide a scan filter to look for particular devices. See Readme.md for more information
                // e.g.:
                //    new ScanFilter().SetAdvertisedManufacturerCompanyId( 224 /*Google*/ ),
                //
                // You can also specify additional scan settings like the amount of power to direct to the Bluetooth antenna:
                // e.g.:
                //    new ScanSettings()
                //    {
                //       Mode = ScanMode.LowPower,
                //       Filter = new ScanFilter().SetAdvertisedManufacturerCompanyId( 224 /*Google*/ )
                //    },
                peripheral =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    var existing = FoundDevices.FirstOrDefault(d => d.Equals(peripheral));
                    if (existing != null)
                    {
                        existing.Update(peripheral);
                    }
                    else
                    {
                        FoundDevices.Add(new BlePeripheralViewModel(peripheral, m_onSelectDevice));
                    }
                });
            },
                m_scanCancel.Token);

            IsScanning = false;
        }
示例#4
0
        public async void StartScan(Double seconds)
        {
            FoundDevices.Clear();
            notPairedDeviceTitle = "0";

            if (IsScanning)
            {
                return;
            }

            if (!IsAdapterEnabled)
            {
                m_dialogs.Toast("Cannot start scan, Bluetooth is turned off");
                return;
            }

            StopScan();
            IsScanning     = true;
            seconds        = BleSampleAppUtils.ClampSeconds(seconds);
            m_scanCancel   = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
            m_scanStopTime = DateTime.UtcNow.AddSeconds(seconds);



            Log.Trace("Beginning device scan. timeout={0} seconds", seconds);

            RaisePropertyChanged(nameof(ScanTimeRemaining));
            // RaisePropertyChanged of ScanTimeRemaining while scan is running
            Device.StartTimer(
                TimeSpan.FromSeconds(1),
                () =>
            {
                RaisePropertyChanged(nameof(ScanTimeRemaining));
                return(IsScanning);
            });

            await m_bleAdapter.ScanForBroadcasts(


                peripheral =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    var existing      = FoundDevices.FirstOrDefault(d => d.Equals(peripheral));
                    var addre         = peripheral.Address.Select(b => b.EncodeToBase16String()).Join(":");
                    var deviceModel   = App.Database.GetDevice(addre);
                    String deviceName = null;
                    if (App.Database.GetDevice(addre) != null)
                    {
                        deviceName = deviceModel.DeviceName;
                    }

                    if (existing != null)
                    {
                        existing.Update(peripheral);
                    }
                    else
                    {
                        if (peripheral.Advertisement.DeviceName != null && (deviceName != peripheral.Advertisement.DeviceName) && !IsExist(FoundDevices, addre))
                        {
                            FoundDevices.Add(new BlePeripheralViewModel(peripheral, m_onSelectDevice));
                            notPairedDeviceTitle = FoundDevices.Count.ToString();
                        }


                        if ((peripheral.Advertisement.DeviceName != null) && (deviceName == peripheral.Advertisement.DeviceName) && !IsExist(FoundConnectDevices, addre))

                        {
                            FoundConnectDevices.Add(new BlePeripheralViewModel(peripheral, m_onSelectDevice));


                            alreadyPairedDeviceTitle = FoundConnectDevices.Count.ToString();
                        }
                    }
                });
            },
                m_scanCancel.Token);

            //DeviceTitle = DeviceTitle + FoundConnectDevices.Count.ToString();

            IsScanning = false;
        }
        private async void StartScan(Double seconds)
        {
            if (IsScanning)
            {
                return;
            }

            if (!IsAdapterEnabled)
            {
                m_dialogs.Toast("Cannot start scan, Bluetooth is turned off");
                return;
            }

            StopScan();
            IsScanning     = true;
            seconds        = BleSampleAppUtils.ClampSeconds(seconds);
            m_scanCancel   = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
            m_scanStopTime = DateTime.UtcNow.AddSeconds(seconds);

            Log.Trace("Beginning device scan. timeout={0} seconds", seconds);

            RaisePropertyChanged(nameof(ScanTimeRemaining));
            // RaisePropertyChanged of ScanTimeRemaining while scan is running
            Device.StartTimer(
                TimeSpan.FromSeconds(1),
                () =>
            {
                RaisePropertyChanged(nameof(ScanTimeRemaining));
                return(IsScanning);
            });


            // 开始扫描附近的蓝牙 ble 设备.
            await m_bleAdapter.ScanForBroadcasts(
                // NOTE:
                //
                // You can provide a scan filter to look for particular devices. See Readme.md for more information
                // e.g.:
                //    new ScanFilter().SetAdvertisedManufacturerCompanyId( 224 /*Google*/ ),
                //
                // You can also specify additional scan settings like the amount of power to direct to the Bluetooth antenna:
                // e.g.:

                new ScanSettings()
            {
                Mode = ScanMode.LowPower,
                //Filter = new ScanFilter().SetAdvertisedManufacturerCompanyId(224 /*Google*/ )


                Filter = new ScanFilter()
                {
                    //只扫描 固定名字 的设备,但不知道怎么扫描 一个数组名字 的设备.
                    //AdvertisedDeviceName = "Cramer",
                },
                IgnoreRepeatBroadcasts = false
            },
                peripheral =>
            {
                Device.BeginInvokeOnMainThread(
                    () =>
                {
                    // 扫描到设备,如果已经有了的,就更新,否则就添加.
                    var existing = FoundDevices.FirstOrDefault(d => d.Equals(peripheral));
                    if (existing != null)
                    {
                        existing.Update(peripheral);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("peripheral Name:" + peripheral.Advertisement.DeviceName);

                        FoundDevices.Add(new BlePeripheralViewModel(peripheral, m_onSelectDevice));
                    }
                });
            },
                m_scanCancel.Token);

            IsScanning = false;
        }