public iOSBluetoothDeviceProximityProbe() { _bluetoothCentralManagerDelegate = new iOSBluetoothDeviceProximityProbeCentralManagerDelegate(this); _bluetoothCentralManagerDelegate.DeviceIdEncountered += async(sender, bluetoothDeviceProximityDatum) => { // we have no cancellation token. thus, all that can happen here is that the datum is stored locally // and surveys are triggered (if any are defined). size- and force-commits will not result, and this // is important because we might be currently executing from the background on a bluetooth scan result. await StoreDatumAsync(bluetoothDeviceProximityDatum, null); }; }
protected override async Task ScanAsync(CancellationToken cancellationToken) { try { // start a fresh manager delegate to collect/read results _bluetoothCentralManagerDelegate = new iOSBluetoothDeviceProximityProbeCentralManagerDelegate(_deviceIdService, _deviceIdCharacteristic, this); // initialize manager, which starts scan. _bluetoothCentralManager = new CBCentralManager(_bluetoothCentralManagerDelegate, new DispatchQueue("ble"), NSDictionary.FromObjectAndKey(NSNumber.FromBoolean(false), CBCentralManager.OptionShowPowerAlertKey)); // the base class handles prompting using to turn on bluetooth and stops the probe if the user does not. TaskCompletionSource <bool> scanCompletionSource = new TaskCompletionSource <bool>(); cancellationToken.Register(() => { try { _bluetoothCentralManager.StopScan(); } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Exception while stopping scan: " + ex.Message, LoggingLevel.Normal, GetType()); } finally { _bluetoothCentralManager = null; scanCompletionSource.TrySetResult(true); } }); await scanCompletionSource.Task; } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Exception while scanning: " + ex.Message, LoggingLevel.Normal, GetType()); } }