Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        public bool StopAdvertisingServices()
        {
            if (heartRateService != null)
            {
                heartRateService.Stop();
                heartRateService = null;
            }

            if (batteryService != null)
            {
                batteryService.Stop();
                batteryService = null;
            }

            if (currentTimeService != null)
            {
                currentTimeService.Stop();
                currentTimeService = null;
            }

            if (lightBulbService != null)
            {
                lightBulbService.Stop();
                lightBulbService = null;
            }

            if (quickLockMainService != null)
            {
                quickLockMainService.Stop();
                quickLockMainService = null;
            }

            if (quickLockHistoryService != null)
            {
                quickLockHistoryService.Stop();
                quickLockHistoryService = null;
            }

            return(true);
        }