/// <summary> /// Called when a characteristic changes /// </summary> /// <param name="gatt">Gatt</param> /// <param name="characteristic">Characteristic.</param> public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { base.OnCharacteristicChanged(gatt, characteristic); // Setting the last update to be now lastChange = DateTime.Now; // If this is one of chars we are tracking if (notifyChars.Contains(characteristic.Uuid.ToString())) { Log.Debug(TAG, "The Characteristic Changed"); // Getting and setting the data byte[] data = characteristic.GetValue(); handle.updateData(data); // Notifying Intent message = new Intent(); message.SetAction(AppUtil.SENSOR_READING_UPDATE_ACTION); Bundle intentBundle = new Bundle(); intentBundle.PutString(AppUtil.ADDRESS_KEY, gatt.Device.Address); intentBundle.PutString(AppUtil.DETAIL_KEY, handle.xmlDetail); message.PutExtras(intentBundle); SendBroadcast(message); } }
/// <summary> /// Updates the data associated with the sensor /// </summary> /// <param name="hr">Hr.</param> void UpdateData(NSData hr) { var now = DateTime.Now; // A byte array to store data from sensor byte[] data = new byte[hr.Length]; // Copy data from sensor data to byte array System.Runtime.InteropServices.Marshal.Copy(hr.Bytes, data, 0, Convert.ToInt32(data.Length)); // Then send to sensor handler for parsing and processing SensorHandler.updateData(data); }
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; }