protected override bool WriteRequested(GattSession session, GattWriteRequest request)
        {
            byte[] receivedWrite;
            CryptographicBuffer.CopyToByteArray(request.Value, out receivedWrite);
            var receivedWriteHexString = Helpers.ToHexString(request.Value);

            QuickLockMainService parent = (QuickLockMainService)this.ParentService;

            //expected: 0066612666 (pcap), or 006661266600000000 (also correct)
            if ((request.Value.Length != 5) && (request.Value.Length != 9))
            {
                Debug.WriteLine($"Smart lock auth WriteRequested, but invalid format: {receivedWriteHexString} ({request.Value.Length})");
                //do not update Value, return
                OnPropertyChanged(new PropertyChangedEventArgs("InvalidRequest"));
                return(true);
            }

            //check just the first byte and password, ignore rest if provided
            if ((receivedWrite[0] != 0x00) || (receivedWrite[1] != password[0]) || (receivedWrite[2] != password[1]) || (receivedWrite[3] != password[2]) || (receivedWrite[4] != password[3]))
            {
                Debug.WriteLine($"Smart lock auth WriteRequested: INVALID PASSWORD: {receivedWriteHexString} ({request.Value.Length})");
                //do not update Value, return
                OnPropertyChanged(new PropertyChangedEventArgs("InvalidPin"));
                return(true);
            }

            parent.UpdateAuthenticationState(true);
            OnPropertyChanged(new PropertyChangedEventArgs("AuthenticationState"));

            return(true);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        protected override bool WriteRequested(GattSession session, GattWriteRequest request)
        {
            byte[] receivedWrite;
            CryptographicBuffer.CopyToByteArray(request.Value, out receivedWrite);
            var receivedWriteHexString = Helpers.ToHexString(request.Value);

            QuickLockMainService parent = (QuickLockMainService)this.ParentService;

            if (!parent.isAuthenticated)
            {
                Debug.WriteLine($"Quicklock cmd WriteRequested, but not authenticated {receivedWriteHexString} ({request.Value.Length})");
                //do not update Value, return
                OnPropertyChanged(new PropertyChangedEventArgs("Unauthenticated"));
                return(true);
            }

            if ((request.Value.Length != 1) || ((receivedWrite[0] != 0x00) && (receivedWrite[0] != 0x01)))
            {
                Debug.WriteLine($"Quicklock cmd WriteRequested, but invalid format {receivedWriteHexString} ({request.Value.Length})");
                //do not update Value, return
                OnPropertyChanged(new PropertyChangedEventArgs("InvalidRequest"));
                return(true);
            }

            Debug.WriteLine($"Quicklock cmd received: {receivedWriteHexString} ({request.Value.Length})");

            if (receivedWrite[0] == 00) // lock
            {
                Debug.WriteLine($"Locking!");
                parent.UpdateUnlockState(false);
            }
            else if (receivedWrite[0] == 01) // unlock
            {
                Debug.WriteLine($"Unlocking!");
                parent.UpdateUnlockState(true);
            }

            OnPropertyChanged(new PropertyChangedEventArgs("LockState"));

            return(true);
        }
Esempio n. 4
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);
        }