Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 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);
        }