public static byte[] PrepareWorkoutStatusBytes(int timestamp, int count, int rpm, int heartrate) { byte[] timestampData = BLEProtocol.ExtractIntData(timestamp, BLEProtocol.Index.TimeStamp.Begin, BLEProtocol.Index.TimeStamp.End); byte[] rpmData = BLEProtocol.ExtractIntData(rpm, BLEProtocol.Index.RPM.Begin, BLEProtocol.Index.RPM.End); byte[] countData = BLEProtocol.ExtractIntData(count, BLEProtocol.Index.Count.Begin, BLEProtocol.Index.Count.End); byte[] preparedBytes = new byte[] { BLEProtocol.StartByte, BLEProtocol.ResponseCode.WorkoutStatusNotification, 0x09, timestampData[0], timestampData[1], countData[0], countData[1], countData[2], countData[3], rpmData[0], rpmData[1], (byte)heartrate, 0x00 }; // Calculate and set checksum preparedBytes[preparedBytes.Length - 1] = BLEProtocol.CalculateChecksum(preparedBytes); return(preparedBytes); }
private static void ReceiveWorkoutStatus(byte[] data) { BLEDebug.LogInfo("Workout Status Received"); int timeStamp = BLEProtocol.ExtractDataInt( BLEProtocol.Index.TimeStamp.Begin, BLEProtocol.Index.TimeStamp.End, data ); int count = BLEProtocol.ExtractDataInt( BLEProtocol.Index.Count.Begin, BLEProtocol.Index.Count.End, data ); int rpm = BLEProtocol.ExtractDataInt( BLEProtocol.Index.RPM.Begin, BLEProtocol.Index.RPM.End, data ); int heartRate = data[BLEProtocol.Index.HeartRate]; _bikeListener.OnReceiveWorkoutStatus(timeStamp, count, rpm, heartRate); }
public void SendWorkoutStatusNotification() { if (controlState == BLEProtocol.WorkoutControlState.Stop) { timestamp = 0; count = 0; rpm = 0; heartrate = 0; } else if (controlState == BLEProtocol.WorkoutControlState.Start) { ++timestamp; count += 2; rpm = 120; heartrate = 0; } BLEDebug.LogInfo("Sending WorkoutStatusNotification"); BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLENotify.PrepareWorkoutStatusBytes( timestamp, count, rpm, heartrate ) ) ); }
public static byte[] PrepareResistanceLevelBytes(int resistanceLevel) { byte[] preparedBytes = new byte[] { BLEProtocol.StartByte, BLEProtocol.ResponseCode.ResistanceLevelResponse, 0x01, (byte)resistanceLevel, 0x00 }; // Calculate and set checksum preparedBytes[preparedBytes.Length - 1] = BLEProtocol.CalculateChecksum(preparedBytes); return(preparedBytes); }
public static byte[] PrepareWorkoutControlStateBytes(int controlState) { byte[] preparedBytes = new byte[] { BLEProtocol.StartByte, BLEProtocol.ResponseCode.WorkoutControlStateResponse, 0x01, (byte)controlState, 0x00 }; // Calculate and set checksum preparedBytes[preparedBytes.Length - 1] = BLEProtocol.CalculateChecksum(preparedBytes); return(preparedBytes); }
public static void ProcessReceiveData(byte[] data) { BLEDebug.LogInfo("BLEAction Processing Received Data"); // Ignore and log invalid data if (!BLEProtocol.ValidateData(data)) { BLEDebug.LogWarning("Warning: Receive Data could not be validated"); return; } byte responseCode = BLEProtocol.GetResponseCode(data); BLEDebug.LogInfo($"Processing Response Code {responseCode}"); switch (responseCode) { case BLEProtocol.ResponseCode.Acknowledge: Acknowledge(); break; case BLEProtocol.ResponseCode.DeviceInformationResponse: ReceiveDeviceInformation(data); break; case BLEProtocol.ResponseCode.ErrorLogResponse: ReceiveErrorLog(data); break; case BLEProtocol.ResponseCode.ResistanceLevelRangeResponse: ReceiveResistanceLevelRange(data); break; case BLEProtocol.ResponseCode.ResistanceLevelResponse: case BLEProtocol.ResponseCode.ResistanceLevelNotification: ReceiveResistanceLevel(data); break; case BLEProtocol.ResponseCode.WorkoutControlStateResponse: case BLEProtocol.ResponseCode.WorkoutControlStateNotification: ReceiveWorkoutControlState(data); break; case BLEProtocol.ResponseCode.WorkoutStatusNotification: ReceiveWorkoutStatus(data); break; default: BLEDebug.LogError("Error: Invalid ResponseCode received from native plugin"); return; } }
public static byte[] PrepareDeviceInformationBytes (int modelID, int hardwareMajor, int hardwareMinor, int firmwareMajor, int firmwareMinor, int firmwarePatch) { byte[] preparedBytes = new byte[] { BLEProtocol.StartByte, BLEProtocol.ResponseCode.DeviceInformationResponse, 0x06, (byte)modelID, (byte)hardwareMajor, (byte)hardwareMinor, (byte)firmwareMajor, (byte)firmwareMinor, (byte)firmwarePatch, 0x00 }; // Calculate and set checksum preparedBytes[preparedBytes.Length - 1] = BLEProtocol.CalculateChecksum(preparedBytes); return(preparedBytes); }
public static void Set(byte actionCode, int value) { BLEDebug.LogInfo("BLEAction Set"); switch (actionCode) { case BLEProtocol.ActionCode.SetWorkoutControlState: case BLEProtocol.ActionCode.SetResistanceLevel: BLEPlugin.Instance.SendPluginMessage( BLEProtocol.ConvertBytesToString( BLEProtocol.PrepareSetCommandBytes(actionCode, value) ) ); break; default: BLEDebug.LogError("Error: Attempting to send invalid Set ActionCode"); break; } }
public static void Get(byte actionCode) { BLEDebug.LogInfo("BLEAction Get"); switch (actionCode) { case BLEProtocol.ActionCode.Acknowledge: case BLEProtocol.ActionCode.GetDeviceInformation: case BLEProtocol.ActionCode.GetErrorLog: case BLEProtocol.ActionCode.GetResistanceLevelRange: case BLEProtocol.ActionCode.GetWorkoutControlState: case BLEProtocol.ActionCode.GetResistanceLevel: BLEPlugin.Instance.SendPluginMessage( BLEProtocol.ConvertBytesToString( BLEProtocol.PrepareGetCommandBytes(actionCode) ) ); break; default: BLEDebug.LogError("Error: Attempting to send invalid Get ActionCode"); break; } }
public void ReceiveUnityMessage(string message) { byte[] messageData = BLEProtocol.ConvertStringToBytes(message); if (!BLEProtocol.ValidateData(messageData)) { BLEDebug.LogError("Received Message Failed Validation - Ignoring"); } byte actionCode = BLEProtocol.GetActionCode(messageData); switch (actionCode) { case BLEProtocol.ActionCode.Acknowledge: // Factory doesn't use this so we shouldn't be using it either BLEDebug.LogWarning("Acknowledge Received"); break; case BLEProtocol.ActionCode.GetDeviceInformation: BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLEResponse.PrepareDeviceInformationBytes( modelID, hardwareMajor, hardwareMinor, firmwareMajor, firmwareMinor, firmwarePatch ) ) ); break; case BLEProtocol.ActionCode.GetErrorLog: // TODO - Low Priority break; case BLEProtocol.ActionCode.GetResistanceLevelRange: BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLEResponse.PrepareResistanceLevelRangeBytes( resistanceLevelMin, resitanceLevelMax ) ) ); break; case BLEProtocol.ActionCode.GetWorkoutControlState: BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLEResponse.PrepareWorkoutControlStateBytes( controlState ) ) ); break; case BLEProtocol.ActionCode.GetResistanceLevel: BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLEResponse.PrepareResistanceLevelBytes( resistanceLevel ) ) ); break; case BLEProtocol.ActionCode.SetWorkoutControlState: controlState = messageData[BLEProtocol.Index.WorkoutControlState]; BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLENotify.PrepareWorkoutControlStateBytes( controlState ) ) ); break; case BLEProtocol.ActionCode.SetResistanceLevel: resistanceLevel = messageData[BLEProtocol.Index.ResistanceLevel]; BLEPluginInstance.SendMessage("ReceivePluginMessage", BLEProtocol.ConvertBytesToString( BLENotify.PrepareResistanceLevelBytes( resistanceLevel ) ) ); break; default: BLEDebug.LogError("Error: Received Invalid ActionCode"); break; } }
public void ReceivePluginMessage(string message) { BLEDebug.LogInfo("Plugin Received Message: " + message); BLEAction.ProcessReceiveData(BLEProtocol.ConvertStringToBytes(message)); }