public async void resetConnection() { await DisplayAlert("Notice", "Disconnected from Ticwatch", "OK"); try { if (SmartWatchPage.ClearGraphs != null) { SmartWatchPage.ClearGraphs(); } } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); } try { if (inputStream != null) { inputStream.Close(); inputStream = null; } } catch (IOException ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); } try { if (serverClientSocket != null) { serverClientSocket.Close(); serverClientSocket = null; } } catch (IOException ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); } }
private async Task StartListening(BluetoothSocket socket) { smartWatchSensors = new SmartWatchSensors(); // Get the BluetoothSocket input and output streams try { inputStream = socket.InputStream; //ConnectionStates.watchConnected = true; } catch (IOException ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); resetConnection(); } const int BUFFER_SIZE = 1024; byte[] buffer = new byte[BUFFER_SIZE]; int bytes = 0; int b = BUFFER_SIZE; // Keep listening to the InputStream while connected while (true) { try { // Read from the input stream bool available = inputStream.IsDataAvailable(); if (available) { if (serverClientSocket == null || !serverClientSocket.IsConnected) { resetConnection(); break; } int i = 0; while (serverClientSocket != null) { var size = await inputStream.ReadAsync(buffer, 0, buffer.Length); i = 0; if (size > 0) { while (size > i) { if (buffer[i] == '$') { if (buffer[i + 1] == '~' && buffer[i + 2] == '!') { resetConnection(); return; } int length = buffer[i + 1] << 24 | (buffer[i + 2] & 0xFF) << 16 | (buffer[i + 3] & 0xFF) << 8 | (buffer[i + 4] & 0xFF); var stringRead = Encoding.UTF8.GetString(buffer, i + 5, i + length + 5); var startIndex = stringRead.IndexOf("{"); var endIndex = stringRead.IndexOf("}") + 1; try { var response = stringRead.Remove(endIndex, stringRead.Length - endIndex).Remove(0, startIndex); var jsonObject = JsonConvert.DeserializeObject <SensorDataJson>(response); var watchData = SensorActivityWatch.GetSensorActivityWatchFromJson(jsonObject); if (SmartWatchPage.SetWatchData != null) { SmartWatchPage.SetWatchData(SensorActivityWatch.GetDoubleArray(jsonObject)); } if (MainPage.StepAndHeartRate != null) { MainPage.StepAndHeartRate(SensorActivityWatch.GetDoubleArray(jsonObject)); } smartWatchSensors = ConvertJsonToSmartWatchSensor(jsonObject, smartWatchSensors); await FIUAssist.DatabaseManager.SensorDataService.Instance.CurrentClient.GetSyncTable("SmartWatchSensors").InsertAsync(JObject.FromObject(smartWatchSensors)); //var dataWatch = string.Format(Constants.smartwatchdata, DateTime.Now.ToString(Constants.DATETIMEFORMAT), watchData.XAccelerationValue.ToString(), watchData.YAccelerationValue.ToString(), watchData.ZAccelerationValue.ToString(), watchData.HeartRateValue.ToString(), watchData.StepCountValue.ToString()); //if (PowerBILive != null) //{ // PowerBILive(dataWatch, NetworkCommunicationManager.DATATYPE.WATCH); //} } catch (Exception ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); resetConnection(); } i += length + 4; } i += size; } } } } } catch (IOException ex) { ExceptionErrorLogger.writeFileOnInternalStorage(ex.ToString()); resetConnection(); return; } } }