示例#1
0
        /// <summary>
        /// Associate the current Pebble device by connecting to it and getting the current location
        /// </summary>
        /// <returns></returns>
        public async Task <bool> AssociatePebble(PebbleDevice _newDevice)
        {
            try
            {
                _newDevice.StoreAsAssociateDevice();

                int Token = await Connect(-1);

                if (IsConnected)
                {
                    Geolocator _geoLocater = new Geolocator();
                    _geoLocater.DesiredAccuracy = PositionAccuracy.High;
                    Geoposition _pos = await _geoLocater.GetGeopositionAsync();

                    Disconnect(Token);

                    return(true);
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("AssociatePebble: " + exp.Message);
                throw exp;
            }

            return(false);
        }
示例#2
0
        private void UndoAssociation(object obj)
        {
            PebbleDevice.RemoveAssociation();

            NotifyPropertyChanged("IsDeviceAssociated");
            NotifyPropertyChanged("AssociatedDeviceFirmware");
            NotifyPropertyChanged("AssociatedDeviceBoard");
            NotifyPropertyChanged("AssociatedDeviceName");
            NotifyPropertyChanged("AssociatedDeviceId");

            MoreInfo = false;
        }
示例#3
0
        public async Task <PebbleDevice> GetCandidatePebble()
        {
            try
            {
                PebbleDevice PebbleCandidate = await P3bble.P3bble.FindPebble();

                return(PebbleCandidate);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("GetCandidatePebble: " + exp.Message);
            }

            return(null);
        }
示例#4
0
        /// <summary>
        /// Returns true if an existing paired Pebble device has been associated with this app
        /// </summary>
        /// <returns></returns>
        public async Task <bool> IsPebbleAssociated()
        {
            try
            {
                PebbleDevice AssociatedDevice = PebbleDevice.LoadAssociatedDevice();

                if (AssociatedDevice != null)
                {
                    var _pebble = await P3bble.P3bble.FindPebble(AssociatedDevice.ServiceId);

                    if (_pebble != null)
                    {
                        return(_pebble.Name == AssociatedDevice.Name);
                    }
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("IsPebbleAssociated: " + exp.Message);
            }

            return(false);
        }
示例#5
0
        public async Task StartBackgroundTask(Initiator InitiatedBy)
        {
            /* PebbleConnector _pc = PebbleConnector.GetInstance();
             * int Handler = await _pc.Connect(-1);
             *
             * return;*/
            var result = await BackgroundExecutionManager.RequestAccessAsync();

            if (result == BackgroundAccessStatus.Denied)
            {
                return;
            }

            PebbleConnector.SetBackgroundTaskRunningStatus(InitiatedBy);

            if (!await IsBackgroundTaskRunning())
            {
                backgroundSyncTaskRegistration = FindSyncTask();
                while (backgroundSyncTaskRegistration != null)
                {
                    backgroundSyncTaskRegistration.Unregister(true);

                    backgroundSyncTaskRegistration = FindSyncTask();
                }

                //if (backgroundSyncTaskRegistration == null)
                // {
                syncBackgroundTaskTrigger = new DeviceUseTrigger();

                // Create background task to write
                var backgroundTaskBuilder = new BackgroundTaskBuilder();

                backgroundTaskBuilder.Name           = Constants.BackgroundCommunicationTaskName;
                backgroundTaskBuilder.TaskEntryPoint = Constants.BackgroundCommunicationTaskEntry;
                backgroundTaskBuilder.SetTrigger(syncBackgroundTaskTrigger);
                backgroundSyncTaskRegistration = backgroundTaskBuilder.Register();


                // }


                try
                {
                    PebbleDevice _AssociatedDevice = PebbleDevice.LoadAssociatedDevice();
                    if (_AssociatedDevice != null)
                    {
                        var _device = await BluetoothDevice.FromIdAsync(_AssociatedDevice.ServiceId);

                        //var device = (await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(new Guid(Constants.PebbleGuid))))).FirstOrDefault(y => y.Name.ToLower().Contains("pebble"));

                        if (_device == null)
                        {
                            throw new OperationCanceledException("Is bluetooth enabled and the Pebble Time paired?");
                        }

                        //DeviceTriggerResult x = await syncBackgroundTaskTrigger.RequestAsync(device.Id);

                        var abc = syncBackgroundTaskTrigger.RequestAsync(_device.DeviceId).AsTask();
                        var x   = await abc;

                        System.Diagnostics.Debug.WriteLine("DeviceTriggerResult: " + x.ToString());

                        if (x != DeviceTriggerResult.Allowed)
                        {
                            throw new Exception(x.ToString());
                        }
                    }
                }
                catch (Exception exc)
                {
                    if (exc.GetType() == typeof(System.OperationCanceledException))
                    {
                        throw exc;
                    }
                    if (exc.GetType() != typeof(System.InvalidOperationException))
                    {
                        throw new Exception("Background communication task can't be started: " + exc.Message);
                    }
                    throw new Exception("Unexpected error: " + exc.Message);
                }
            }
        }
示例#6
0
        /// <summary>
        /// Connect to the pebble time watch
        /// </summary>
        /// <returns></returns>
        public async Task <int> Connect(int Token)
        {
            LastError = "";
            if (Token != -1 && _connections.Contains(Token) && IsConnected)
            {
                return(Token);
            }

            int newToken = CreateConnectionToken();

            try
            {
                if (!IsConnected)
                {
                    //await RegisterBackgroundBluetoothTask();

                    //return false;
                    P3bble.P3bble.IsMusicControlEnabled = true;
#if DEBUG
                    P3bble.P3bble.IsLoggingEnabled = true;
#else
                    P3bble.P3bble.IsLoggingEnabled = false;
#endif
                    PebbleDevice _AssociatedDevice = PebbleDevice.LoadAssociatedDevice();
                    if (_AssociatedDevice != null)
                    {
                        var _device = await BluetoothDevice.FromIdAsync(_AssociatedDevice.ServiceId);

                        _pebble            = new P3bble.P3bble(_device);
                        _pebble.WatchItems = this.WatchItems;
                        bool Result = await _pebble.ConnectAsync();

                        if (Result)
                        {
                            LastConnectedDevice = _AssociatedDevice;

                            await _pebble.WatchItems.Load();

                            return(newToken);
                        }

                        LastError = "Connect to Pebble Time failed; is it already connected?";
                        if (_pebble.LastError.Length > 0)
                        {
                            LastError        += " Error: ";
                            LastError        += _pebble.LastError;
                            _pebble.LastError = "";
                        }
                        // _pebble._protocol.StartRun();
                    }
                    else
                    {
                        //No Pebble associated
                        LastError = "No Pebble device associated.";
                    }
                }
                else
                {
                    return(newToken);
                }
            }
            catch (Exception exp)
            {
                LastError = "Exception: " + exp.Message;
            }

            ReleaseConnectionToken(newToken);

            return(-1);
        }