private RiderData ReadCharacteristicMessage(IBuffer buffer) { byte[] data; CryptographicBuffer.CopyToByteArray(buffer, out data); if (data != null) { // This is our custom calc service Result UUID. if (selectedCharacteristic.Uuid.Equals(Constants.ResultCharacteristicUuid)) { string hexString = CryptographicBuffer.EncodeToHexString(buffer); string hex = "hex" + hexString; if (hexString.Length >= 10) { int dataLength = Convert.ToInt16(hexString.Substring(2, 2), 16); String dataPageHexNumber = hexString.Substring(8, 2); if (dataPageHexNumber == "19") { int cadence = Convert.ToInt16(hexString.Substring(12, 2), 16); int instPower = Convert.ToInt16((hexString.Substring(21, 1) + hexString.Substring(18, 2)), 16); int accPower = Convert.ToInt16((hexString.Substring(16, 2) + hexString.Substring(14, 2)), 16); RiderData reading = new RiderData(); reading.Iteration = count; count++; reading.Power = instPower; return(reading); } if (dataPageHexNumber == "11") { double incline = 0.01 * Convert.ToInt16(hexString.Substring(18, 2) + hexString.Substring(16, 2), 16); double resistance = 0.01 * Convert.ToInt16(hexString.Substring(20, 2), 16); } } } // No guarantees on if a characteristic is registered for notifications. } else { return(null); } return(null); }
public static void ExportTrackData(Track track, string fn, int frames, ICamera camera) { var timeline = new linerider.Game.Timeline( track); timeline.Restart(track.GetStart(), 1); timeline.GetFrame(frames); camera.SetTimeline(timeline); List <RiderData> data = new List <RiderData>(); for (int idx = 0; idx < frames; idx++) { var frame = timeline.GetFrame(idx); var framedata = new RiderData(); framedata.Frame = idx; framedata.Points = new List <track_json.point_json>(); for (int i = 0; i < frame.Body.Length; i++) { var point = frame.Body[i]; framedata.Points.Add(new track_json.point_json() { x = point.Location.X, y = point.Location.Y }); var camframe = camera.GetFrameCamera(idx); framedata.CameraCenter = new track_json.point_json() { x = camframe.X, y = camframe.Y }; } data.Add(framedata); } using (var file = File.Create(fn)) { var bytes = JsonSerializer.Serialize <List <RiderData> >(data); file.Write(bytes, 0, bytes.Length); } }
private string FormatValueByPresentation(IBuffer buffer, GattPresentationFormat format) { // BT_Code: For the purpose of this sample, this function converts only UInt32 and // UTF-8 buffers to readable text. It can be extended to support other formats if your app needs them. byte[] data; CryptographicBuffer.CopyToByteArray(buffer, out data); if (format != null) { if (format.FormatType == GattPresentationFormatTypes.UInt32 && data.Length >= 4) { return(BitConverter.ToInt32(data, 0).ToString()); } else if (format.FormatType == GattPresentationFormatTypes.Utf8) { try { return(Encoding.UTF8.GetString(data)); } catch (ArgumentException) { return("(error: Invalid UTF-8 string)"); } } else { // Add support for other format types as needed. return("Unsupported format: " + CryptographicBuffer.EncodeToHexString(buffer)); } } else if (data != null) { // We don't know what format to use. Let's try some well-known profiles, or default back to UTF-8. if (selectedCharacteristic.Uuid.Equals(GattCharacteristicUuids.HeartRateMeasurement)) { try { return("Heart Rate: " + ParseHeartRateValue(data).ToString()); } catch (ArgumentException) { return("Heart Rate: (unable to parse)"); } } else if (selectedCharacteristic.Uuid.Equals(GattCharacteristicUuids.BatteryLevel)) { try { // battery level is encoded as a percentage value in the first byte according to // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml return("Battery Level: " + data[0].ToString() + "%"); } catch (ArgumentException) { return("Battery Level: (unable to parse)"); } } // This is our custom calc service Result UUID. Format it like an Int else if (selectedCharacteristic.Uuid.Equals(Constants.ResultCharacteristicUuid)) { string hexString = CryptographicBuffer.EncodeToHexString(buffer); if (hexString.Length >= 10) { int dataLength = Convert.ToInt16(hexString.Substring(2, 2), 16); String dataPageNumber = hexString.Substring(8, 2); if (dataPageNumber == "19") { int cadence = Convert.ToInt16(hexString.Substring(12, 2), 16); int instPower = Convert.ToInt16((hexString.Substring(21, 1) + hexString.Substring(18, 2)), 16); int accPower = Convert.ToInt16((hexString.Substring(16, 2) + hexString.Substring(14, 2)), 16); RiderData reading = new RiderData(); reading.Iteration = count; count++; reading.Power = instPower; riderData.Add(reading); return("Cadence: " + cadence + " " + "Instantaneous Power: " + instPower + " " + "Accumulated Power: " + accPower); } } } // No guarantees on if a characteristic is registered for notifications. else if (registeredCharacteristic != null) { // This is our custom calc service Result UUID. Format it like an Int if (registeredCharacteristic.Uuid.Equals(Constants.ResultCharacteristicUuid)) { return(BitConverter.ToInt32(data, 0).ToString()); } else { try { return("Hex format: " + CryptographicBuffer.EncodeToHexString(buffer)); } catch (ArgumentException) { return("Unknown format"); } } } } else { return("Empty data received"); } return("Unknown format"); }