private async Task Reconnect() { ReconnectDelay -= 500; if (ReconnectDelay <= 0) { Handler = await _pc.Connect(-1); if (_pc.IsConnected) { AddToLog("Reconnected with Pebble Time"); _pc.StartReceivingMessages(); } else { AddToLog("Reconnection failed with Pebble Time. Retrying in 5 minutes"); ReconnectDelay = 300000; #if DEBUG AddToLog("Debug mode. Retrying in 5 seconds"); ReconnectDelay = 300; #endif } } }
/// <summary> /// Connect to a pebble /// </summary> /// <returns></returns> public async Task Connect() { bool bNewConnection = false; bNewConnection = !_pc.IsConnected; //Connect to the watch _ConnectionToken = await _pc.Connect(_ConnectionToken); if (!_pc.IsConnected) { _Log.Add("No connection with Pebble Time."); _Log.Add("Already connected or not paired?."); throw new Exception("No connection with Pebble Time"); } if (bNewConnection) { _Log.Add("Connected"); } }
/// <summary> /// Main thread for communication with pebble on a background task. /// /// Reading the state of the process is possible via the local settings BackgroundIsRunning. Synchronization via /// Mutex is not possible due to heavy use of await statements. The mutex will be abandoned and releasing gives /// an exception. /// </summary> /// <param name="taskInstance"></param> public async void Run(IBackgroundTaskInstance taskInstance) { var def = taskInstance.GetDeferral(); var localSettings = ApplicationData.Current.LocalSettings; Handler = -1; localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.OK; try { System.Diagnostics.Debug.WriteLine("Start BackgroundCommunication"); //Connect _pc = PebbleConnector.GetInstance(); Handler = await _pc.Connect(-1); if (_pc.IsConnected) { AddToLog("Connection made with Pebble Time"); Log = new ObservableCollection <string>(); Log.CollectionChanged += Log_CollectionChanged; _pc.Pebble.Log = Log; _pc.StartReceivingMessages(); _pc.disconnectEventHandler += _pc_disconnectEventHandler; _pc.Pebble._protocol.MessageReceived += AppMessageReceived; bool Continue = true; //initialise settings while (Continue) { try { localSettings.Values[Constants.BackgroundCommunicatieIsRunning] = true; if (_pc.IsConnected) { await PaceHandler(); await TennisHandler(); //await Wipe(); await Synchronize(); await Select(); await Launch(); await AddItem(); await PebbleKitExecution(); } else { await Reconnect(); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message + e.StackTrace); } await ProcessDelay(); //Check if continue Continue = ((int)localSettings.Values[Constants.BackgroundCommunicatieContinue] != 0); } await PaceHandlerCleanup(); localSettings.Values[Constants.BackgroundTennis] = false; localSettings.Values[Constants.BackgroundPace] = false; _pc.Pebble._protocol.MessageReceived -= AppMessageReceived; } else { AddToLog("Connection with Pebble Time Failed."); localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.ConnectionFailed; if (_pc.LastError.Length > 0) { AddToLog(_pc.LastError); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception BackgroundCommunication: " + e.Message); localSettings.Values[Constants.BackgroundCommunicatieError] = (int)BCState.ExceptionOccurred; } finally { localSettings.Values[Constants.BackgroundCommunicatieIsRunning] = false; //Disconnect if (_pc.IsConnected) { _pc.Disconnect(Handler); AddToLog("Disconnected from Pebble Time"); } } System.Diagnostics.Debug.WriteLine("End BackgroundCommunication"); def.Complete(); }
public async Task sendAppMessage(ExpandoObject data) { PebbleConnector _pc = PebbleConnector.GetInstance(); int newToken = await _pc.Connect(-1); try { if (_pc.IsConnected) { System.Diagnostics.Debug.WriteLine(String.Format("sendAppMessage(data={0})", data.ToString())); P3bble.Messages.AppMessage _am = new P3bble.Messages.AppMessage(P3bble.Constants.Endpoint.ApplicationMessage); uint iKey = 0; _am.Content = new Dictionary <int, object>(data.Count()); _am.Command = P3bble.Messages.AppCommand.Push; _am.AppUuid = ParentItem.ID; _am.TransactionId = (byte)_pc.GetNextMessageIdentifier(); foreach (var element in data) { if (element.Value != null) { if (ParentItem.AppKeys.ContainsKey(element.Key)) { iKey = (uint)ParentItem.AppKeys[element.Key]; Type VariableType = element.Value.GetType(); System.Diagnostics.Debug.WriteLine(String.Format(" key: {0}-{3}, value: {1}, type: {2}", element.Key, element.Value, VariableType.ToString(), iKey)); if (VariableType == typeof(String)) { String Value = (String)element.Value; //_am.AddTuple(iKey, P3bble.Messages.AppMessageTupleDataType.String, System.Text.Encoding.UTF8.GetBytes(Value)); _am.Content.Add((int)iKey, Value); } if (VariableType == typeof(Double)) { double dValue = (double)element.Value; int iValue = (int)dValue; byte[] bytes = BitConverter.GetBytes(iValue); //_am.AddTuple(iKey, P3bble.Messages.AppMessageTupleDataType.Int, bytes); _am.Content.Add((int)iKey, iValue); } } } } //byte[] package = _am.ToBuffer(); //System.Diagnostics.Debug.WriteLine("<< PAYLOAD: " + BitConverter.ToString(package).Replace("-", ":")); await _pc.Pebble._protocol.WriteMessage(_am); } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); } finally { _pc.Disconnect(newToken); } }