/** * Handle data received from the Realtime server */ private void OnDataReceived(object sender, DataReceivedEventArgs e) { FlatJSON fJSON = new FlatJSON(); BLEDebug.LogInfo("OPCODE " + e.OpCode + " Received, Data:" + BytesToString(e.Data)); switch (e.OpCode) { // handle message based on OpCode case OP_CODE_PLAYER_ACCEPTED: _peerId = e.Sender; OnPlayerAccepted(peerId); break; case OP_CODE_PLAYER_DISCONNECTED: Int32.TryParse(BytesToString(e.Data), out _peerId); OnPlayerDisconnected(peerId); break; case OP_CODE_RACE_START: OnRaceStart(); break; case OP_CODE_RACE_END: OnRaceEnd(); break; case OP_CODE_TIME_TILL_TERMINATE: int time; Int32.TryParse(BytesToString(e.Data), out time); OnNotifyTimeTillTerminate(time); break; case OP_CODE_STATS_UPDATE: fJSON.Deserialize(BytesToString(e.Data)); fJSON.TryGetIntValue("rotations", out int rotations); fJSON.TryGetIntValue("rpm", out int rpm); fJSON.TryGetFloatArray("playerPosition", out float[] playerPosition); fJSON.TryGetFloatValue("progressDistance", out float progressDistance); OnStatsUpdate(e.Sender, rotations, rpm, playerPosition, progressDistance); break; case OP_CODE_CUSTOMIZATION_UPDATE: fJSON.Deserialize(BytesToString(e.Data)); fJSON.TryGetStringValue("PlayerId", out string customplayer); fJSON.TryGetStringValue("characterModelId", out string characterModel); OnCustomizationUpdate(e.Sender, customplayer, characterModel); break; default: BLEDebug.LogWarning("Unknown OPCODE Received"); break; } }
public void RequestEnableBLE() { if (!_nativePluginInstance.EnabledBLE) { _nativePluginInstance.RequestEnableBLE(); } else { BLEDebug.LogWarning("BLE Already Enabled, Ignoring Request"); } RequestEnableLocation(); }
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 void OnReceiveWorkoutControlState(int controlState) { BLEDebug.LogWarning("Warning - Bike Listener Not Registered"); }
public void OnReceiveResistanceLevel(int resistanceLevel) { BLEDebug.LogWarning("Warning - Bike Listener Not Registered"); }
public void OnReceiveResistanceLevelRange(int min, int max) { BLEDebug.LogWarning("Warning - Bike Listener Not Registered"); }
public void OnReceiveErrorLog(byte[] log) { BLEDebug.LogWarning("Warning - Bike Listener Not Registered"); }
public void OnReceiveDeviceInformation(int modelID, string hardwareVersion, string firmwareVersion) { BLEDebug.LogWarning("Warning - Bike Listener Not Registered"); }
public void OnAcknowledge() { BLEDebug.LogWarning("Warning - Bike Listener Not Registered"); }
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; } }