コード例 #1
0
 void StopScan()
 {
     _scanning = false;
     try
     {
         _scanner.StopScan(_scanCallback);
         _scanner.StopScan(_scanCallbackBatch);
     }
     catch (Exception err)
     {
         _logger.LogError(err, "StopScan Crashed");
     }
 }
コード例 #2
0
 public void OnAnimationStart(Animator animation)
 {
     LbStatus.Text = "Se cauta dispozitive...";
     if (_bluetoothManager == null)
     {
         return;
     }
     _bluetoothAdapter = _bluetoothManager.Adapter;
     if (!_bluetoothAdapter.IsEnabled)
     {
         StartActivityForResult(new Intent(BluetoothAdapter.ActionRequestEnable), 11);
     }
     else
     {
         BluetoothScanner = _bluetoothAdapter.BluetoothLeScanner;
         BluetoothScanner.StartScan(ScanCallback);
         handler.PostDelayed(() => {
             //mScanning = false;
             if (!isDeviceConnected)
             {
                 Log.Error("ScanTest", "Timeout");
                 BluetoothScanner.StopScan(ScanCallback);
                 LbStatus.Text = "Nu s-au gasit dispozitive";
                 _animationView.CancelAnimation();
             }
         }, SCAN_PERIOD);
         //_scanButton.Enabled = false;
         _dataContainer.Visibility = ViewStates.Gone;
     }
 }
コード例 #3
0
 protected override void OnPause()
 {
     base.OnPause();
     if (_bluetoothAdapter != null)
     {
         BluetoothScanner?.StopScan(ScanCallback);
     }
 }
コード例 #4
0
        public void StopScan()
        {
            if (!IsScanningBeacon)
            {
                return;
            }

            _bleScanner.StopScan(_scanCallback);
            IsScanningBeacon = false;
        }
コード例 #5
0
 private void Scan()
 {
     if (_bluetoothManager.Adapter.State != State.Disconnecting)
     {
         Console.WriteLine("StartScan");
         _textView.Text = string.Empty;
         _bluetoothLeScanner.StartScan(Scanner.Instance);
         _handler.PostDelayed(() =>
         {
             Console.WriteLine("StopScan");
             _bluetoothLeScanner.StopScan(Scanner.Instance);
         }, 10000);
     }
 }
コード例 #6
0
ファイル: MainActivity.cs プロジェクト: ytabuchi/BleSample
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            // Bluetooth Low Energyがサポートされているかのチェック。
            if (!PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureBluetoothLe))
            {
                Toast.MakeText(this, Resource.String.ble_not_supported, ToastLength.Short).Show();
                Finish();
            }

            // BluetoothManager,BluetoothAdapter,BluetoothLeScannerをインスタンス化。
            BluetoothManager manager = (BluetoothManager)GetSystemService(BluetoothService);
            BluetoothAdapter adapter = manager.Adapter;
            scanner = adapter.BluetoothLeScanner;

            // BluetoothのAdapterが取得できているか=Bluetoothがサポートされているかのチェック。
            if (adapter == null)
            {
                Toast.MakeText(this, Resource.String.error_bluetooth_not_supported, ToastLength.Short).Show();
                Finish();
                return;
            }

            var scanButton = FindViewById<Button>(Resource.Id.scanButton);
            scanButton.Click += (sender, e) =>
            {
                bleDevices = new List<BleDeviceData>();

                scanCallback.ScanResultEvent += ScanCallback_ScanResultEvent;
                scanner.StartScan(scanCallback);
                Thread.Sleep(5000);
                scanner.StopScan(scanCallback);
            };

            listView = FindViewById<ListView>(Resource.Id.deviceList);
            listView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
            {
                var intent = new Intent(this, typeof(ServiceListActivity));
                var sendData = new string[] { bleDevices[e.Position].Name, bleDevices[e.Position].Id };
                intent.PutExtra("data", sendData);
                StartActivity(intent);
            };
        }
コード例 #7
0
        public static void stopScan(ScanCallback callback)
        {
            BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;

            if (adapter == null)
            {
                Log.Debug(tag, "BluetoothAdapter is null");
            }

            BluetoothLeScanner scaner = adapter.BluetoothLeScanner;

            if (scaner == null)
            {
                Log.Debug(tag, "BluetoothLeScanner is null");
            }

            scaner.StopScan(callback);
        }
コード例 #8
0
        public void StopScan()
        {
            if (!IsScanning)
            {
                return;
            }

            if (OS_VER < BuildVersionCodes.Lollipop)
            {
                _btAdapter.StopLeScan(_scanCallbackOld);
            }
            else
            {
                _bleScanner.StopScan(_scanCallbackNew);
            }

            IsScanning = false;
        }
コード例 #9
0
        public void StopScan()
        {
            if (!IsScanning)
            {
                return;
            }

            if (OS_VER < BuildVersionCodes.Lollipop)
            {
                #pragma warning disable CS0618 // Type or member is obsolete
                _btAdapter.StopLeScan(_scanCallbackOld);
                #pragma warning disable CS0618 // Type or member is obsolete
            }
            else
            {
                _bleScanner.StopScan(_scanCallbackNew);
            }

            IsScanning = false;
        }
コード例 #10
0
        public void StopScanner()
        {
            bluetoothLeScanner = mBluetoothAdapter.BluetoothLeScanner;
            if (mScanning)
            {
                bluetoothLeScanner.StopScan(this);
            }
            mScanning = false;

            /*
             * Handler mHandler = new Handler();
             * mHandler.PostDelayed(new Action(delegate {
             *  mScanning = false;
             *  bluetoothLeScanner.StopScan(this);
             *  if (ScanResultEvent != null) {
             *      ScanResultEvent(mLeDevices);
             *  }
             * }), SCAN_PERIOD);
             */
        }
コード例 #11
0
        public void StopScanning()
        {
            if (IsScanning == false)
            {
                return;
            }


            if (_sdkInt >= Android.OS.BuildVersionCodes.Lollipop) // 21
            {
                _bleScanner.StopScan(_scanCallback);
            }
            else
            {
#pragma warning disable 0618
                _adapter.StopLeScan(_leScanCallback);
#pragma warning restore 0618
            }

            IsScanning = false;
        }
コード例 #12
0
        public Task <IEnumerable <WaterBoilerMatDevice> > ScanAsync(int timeoutMilliseconds)
        {
            logger.Log($"Call ScanAsync({timeoutMilliseconds})", Category.Debug, Priority.Medium);
            var tcs = new TaskCompletionSource <IEnumerable <WaterBoilerMatDevice> >();

            // check if BluetoothLE APIs are available
            if (Android.App.Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureBluetoothLe) != true ||
                leScanner == null)
            {
                logger.Log($"BluetoothLE APIs are not available", Category.Warn, Priority.High);
                return(Task.FromException <IEnumerable <WaterBoilerMatDevice> >(new BluetoothLEException("BluetoothLE APIs are not available")));
            }

            var result       = new List <BluetoothDevice>();
            var scanCallback = new LE.ScanCallback((device, rssi, record) =>
            {
                if (result.Any(D => D.Address.Equals(device.Address)))
                {
                    return;
                }

                result.Add(device);
            });

            // Start the Enumeration
            leScanner.StartScan(scanCallback);
            logger.Log($"Start the BluetoothLE device Enumeration", Category.Info, Priority.High);

            handler.PostDelayed(() =>
            {
                leScanner.StopScan(scanCallback);
                logger.Log($"Stop the BluetoothLE device Enumeration. Found {result.Count} devices.", Category.Info, Priority.High);

                tcs.SetResult(result.Where(D => WaterBoilerMatDevice.IsNavienDevice(D.Address))
                              .Select(D => new WaterBoilerMatDeviceAndroid(D, bluetoothAdapter)));
            }, timeoutMilliseconds);

            return(tcs.Task);
        }
コード例 #13
0
ファイル: BLEAdapter.cs プロジェクト: poz1/Poz1.BLE
        public Task <IList <IBLEDevice> > ScanForDevicesAsync()
        {
            _scanForDevicesTCS = new TaskCompletionSource <IList <IBLEDevice> >();
            _discoveredDevices = new List <IBLEDevice>();

            IsScanning = true;

            _adapter.StartScan(this);

            Task.Run(async() =>
            {
                await Task.Delay((int)ScanTimeout.TotalMilliseconds);
                if (IsScanning)
                {
                    IsScanning = false;
                    _adapter.StopScan(this);

                    _scanForDevicesTCS.SetResult(_discoveredDevices);
                }
            });

            return(_scanForDevicesTCS.Task);
        }
コード例 #14
0
        public bool StartScanning()
        {
            if (isScanning)
            {
                return(true);
            }

            if (IsEnabled)
            {
                // Stops scanning after a pre-defined scan period.
                //scanHandler.PostDelayed(new Runnable(() =>
                //{
                //    isScanning = false;

                //    scanner.StopScan(scanCallback);
                //}), 10000);

                isScanning = true;
                ScanFilter.Builder builder = new ScanFilter.Builder();
                builder.SetServiceUuid(DottedUUID);
                ScanSettings.Builder sBuilder = new ScanSettings.Builder();
                sBuilder.SetScanMode(Android.Bluetooth.LE.ScanMode.LowPower);
                scanner.StartScan(new List <ScanFilter> {
                    builder.Build()
                },
                                  sBuilder.Build(),
                                  scanCallback);
                return(true);
            }
            else
            {
                isScanning = false;
                scanner.StopScan(scanCallback);
                return(false);
            }
        }
コード例 #15
0
        protected override void OnDestroy()
        {
            base.OnDestroy();

            _Scanner.StopScan(_ScanCallback);
        }
コード例 #16
0
 protected override void OnPause()
 {
     base.OnPause();
     _scanner?.StopScan(_scanCallback);
 }
コード例 #17
0
 public void StopScanningForDevices()
 {
     UpdateTextView("BluetoothLEManager: Stopping the scan for devices.");
     _isScanning = false;
     _scanner.StopScan(_scannerCallback);
 }