private void resetListViews() { ((MainActivity)Activity).RunOnUiThread(delegate { clearConnectionList(); List <string> activeAddress = _connectedDeviceMap.Where(kvp => kvp.Value == true).Select(kvp => kvp.Key).ToList(); List <string> inactiveAddress = _connectedDeviceMap.Where(kvp => kvp.Value == false).Select(kvp => kvp.Key).ToList(); if (_connectedDeviceMap.Count == 0) { Log.Warn(TAG, "There are no connected devices"); _connectedDevicesArrayAdapter.Add(LE_SENSOR_WARNING_NO_CONNECT); radioGroupList.Visibility = ViewStates.Gone; // Hiding the sensor feedback hideLiveView(); // Turn off sensor feedback if it was on if (_scanSwitch.Checked) { _scanSwitch.Checked = false; } } else if (activeAddress.Count == 0) { Log.Warn(TAG, "There are no active devices"); _connectedDevicesArrayAdapter.Add("There are no active devices"); // Hiding the sensor feedback hideLiveView(); // Turn off sensor feedback if it was on if (_scanSwitch.Checked) { _scanSwitch.Checked = false; } } if (activeAddress.Count > 0) { radioGroupList.Visibility = ViewStates.Visible; if (AppConfig.PostSensorUpdates) { showLiveView(); } Log.Debug(TAG, "Active devices found, adding to list"); // Enabling click enableConnectedListViewClick(); // Add the items foreach (string a in activeAddress) { BluetoothDevice device = AppUtil.getDevice(a, _adpt); if (device != null && (!string.IsNullOrEmpty(device.Name))) { _connectedDevicesArrayAdapter.Add(device.Name + "\n" + device.Address); _spinnerDevicesArrayAdapter.Add(device.Name + "\n" + device.Address); } else { Log.Warn(TAG, System.String.Format("Sensor with address {0} was not available. Getting name from cache", a)); _connectedDevicesArrayAdapter.Add(AppConfig.getDeviceName(a) + "\n" + a); _spinnerDevicesArrayAdapter.Add(AppConfig.getDeviceName(a) + "\n" + a); } } sensorChoice.SetSelection(0); sensorChoice.Enabled = true; } if (inactiveAddress.Count > 0) { Log.Debug(TAG, "Inactive devices found, adding to list"); radioGroupList.Visibility = ViewStates.Visible; foreach (string a in inactiveAddress) { _disconnectedDevicesArrayAdapter.Add(AppConfig.getDeviceName(a) + "\n" + a); } } else { _disconnectedDevicesArrayAdapter.Add("There are no inactive devices"); } _connectedDevicesArrayAdapter.NotifyDataSetChanged(); _spinnerDevicesArrayAdapter.NotifyDataSetChanged(); _disconnectedDevicesArrayAdapter.NotifyDataSetChanged(); }); }
public void hexoSkinConnect(BluetoothDevice dev) { DateTime lastChange = DateTime.MinValue; System.Timers.Timer t = new System.Timers.Timer(AppUtil.HEXOSKIN_INTERVAL); System.Timers.Timer timeOut = new System.Timers.Timer(AppUtil.HEXOSKIN_CONNECT_TIMEOUT); timeOut.AutoReset = true; timeOut.Elapsed += delegate { // Checking if HexoSkin Timed out TimeSpan duration = DateTime.Now - lastChange; if (duration.TotalMilliseconds > AppUtil.HEXOSKIN_CONNECT_TIMEOUT) { timeOut.Enabled = false; t.Enabled = false; // Send disconnect notice sendStateUpdate(dev.Address, false, "Hexoskin stopped reporting data"); } timeOut.Interval = AppUtil.SENSOR_CONNECT_TIMEOUT; }; t.AutoReset = true; t.Elapsed += async delegate { timeOut.Enabled = true; Log.Debug(LOG_TAG, "Getting hexoskin reading"); try { string data = await HTTPSender.getMessage("https://s3.amazonaws.com/pscloud-watchtower/", dev.Name); // If we got valid data if (!string.IsNullOrWhiteSpace(data)) { if (lastChange == DateTime.MinValue) { // First time we read data. sendStateUpdate(dev.Address, true, "Hexoskin started reporting data"); } lastChange = DateTime.Now; // Sending data to SensorHandler Dictionary <string, string> deviceDetails = new Dictionary <string, string>(); deviceDetails.Add("device_name", dev.Name); // Get the sensor handler SensorHandler sh = new SensorHandler(deviceDetails, AppConfig.UserID); byte[] dataArr = Encoding.UTF8.GetBytes(data); sh.updateData(dataArr); string xmlDetail = sh.xmlDetail; Log.Debug(LOG_TAG, xmlDetail); // Notifying Intent message = new Intent(AppUtil.SENSOR_READING_UPDATE_ACTION); Bundle intentBundle = new Bundle(); intentBundle.PutString(AppUtil.ADDRESS_KEY, dev.Address); intentBundle.PutString(AppUtil.DETAIL_KEY, xmlDetail); // Get Time last reading was taken Dictionary <string, string> dataJson = JsonConvert.DeserializeObject <Dictionary <string, string> >(data); double epochValue; if (double.TryParse(dataJson["timestamp"], out epochValue)) { epochValue = epochValue / 256; DateTime hexoSkinRead = AppUtil.FromUnixTime((long)epochValue); hexoSkinRead = DateTime.SpecifyKind(hexoSkinRead, DateTimeKind.Utc); hexoSkinRead = hexoSkinRead.ToLocalTime(); intentBundle.PutString(AppUtil.TIME_KEY, hexoSkinRead.ToString() ); } message.PutExtras(intentBundle); SendBroadcast(message); } } catch (Exception ex) { Log.Debug(LOG_TAG, "Getting Hexoskin data failed"); } }; t.Enabled = true; }