public async Task <bool> StartAdvertisingQuickLock() { isCurrentlyStarting = true; batteryService = new BLEServices.BatteryService(); //check if we can start service try { await batteryService.Init(); } catch // e.g. when Bluetooth adapter is off { Debug.WriteLine("Service start exception, probably Bluetooth adapter is off!"); return(false); } batteryService.IsConnectable = false; batteryService.IsDiscoverable = false; batteryService.Start(); currentTimeService = new BLEServices.CurrentTimeService(); await currentTimeService.Init(); currentTimeService.IsConnectable = false; currentTimeService.IsDiscoverable = false; currentTimeService.Start(); quickLockMainService = new BLEServices.QuickLockMainService(); await quickLockMainService.Init(); quickLockMainService.IsConnectable = true; // advertise in services list quickLockMainService.IsDiscoverable = true; quickLockMainService.Start(); quickLockHistoryService = new BLEServices.QuickLockHistoryService(); await quickLockHistoryService.Init(); quickLockHistoryService.IsConnectable = false; quickLockHistoryService.IsDiscoverable = false; quickLockHistoryService.Start(); // wait 0.5s for the service publishing callbacks to kick in await Task.Delay(500); // the service publishing callback can hit back with error a bit later // checking actual state if ((!batteryService.IsPublishing) || (!currentTimeService.IsPublishing) || (!quickLockMainService.IsPublishing) || (!quickLockHistoryService.IsPublishing)) { Debug.WriteLine("Service start exception, isPublishing = false (from service callback?), restart Bluetooth adapter?"); isCurrentlyStarting = false; return(false); } else { isCurrentlyStarting = false; return(true); } }
public async Task <bool> StartAdvertisingLightbulb() { isCurrentlyStarting = true; batteryService = new BLEServices.BatteryService(); //check if we can start service try { await batteryService.Init(); } catch // e.g. when Bluetooth adapter is off { Debug.WriteLine("Service start exception, probably Bluetooth adapter is off!"); return(false); } batteryService.IsConnectable = true; batteryService.IsDiscoverable = true; batteryService.Start(); // we could start first one, so the others should also go (no need for try/catch ?) heartRateService = new BLEServices.HeartRateService(); await heartRateService.Init(); heartRateService.IsConnectable = false; heartRateService.IsDiscoverable = false; heartRateService.Start(); lightBulbService = new BLEServices.LightBulbService(); await lightBulbService.Init(); lightBulbService.IsConnectable = false; lightBulbService.IsDiscoverable = false; lightBulbService.Start(); // wait 0.5s for the service publishing callbacks to kick in await Task.Delay(500); // the service publishing callback can hit back with error a bit later // checking actual state if ((!batteryService.IsPublishing) || (!heartRateService.IsPublishing) || (!lightBulbService.IsPublishing)) { Debug.WriteLine("Service start exception, isPublishing = false (from service callback?), restart Bluetooth adapter?"); isCurrentlyStarting = false; return(false); } else { isCurrentlyStarting = false; return(true); } }