I've been making an app, which gets data from external device (via bluetooth) and displays received data.
I'm still pretty new to programming and I'm struggling to finish this app.
I wrote some code, that works well, but I wanted to ask you if my solution can be considered as a good one.
Firstly, some background. External device sends informations in a format like this: "D1x*" - where 'x' can be '1' or '0'. Unfortunately I'm not getting the whole message at once. What I mean by that is the fact, that very often I get messages like: "D1", "1*", and so on. So, in order to interprete the data, I need to wait until the whole frame is received. That's why i created a string called appData.readBuffer. Everytime I receive a message, I add it there. I do it like this:
public void OnMessageReceived(object source, string message)
{
appData.readBuffer += message;
}
So, that's how I receive the messages. Now, the other part -the one where I interprete the data. In order to display the data almost instantly after it's sent, I created a Task, which runs my Analyze function in an infinite loop:
Task AnalyzerTask = Task.Run(() =>
{
while (true)
{
Analyze();
}
});
The above code is called at the end of my Page constructor.
Now here's the Analyze method:
public void Analyze()
{
string oneMessage = "";
if ((appData.readBuffer.Contains("A") || appData.readBuffer.Contains("D")) && appData.readBuffer.Contains("*"))
{
int indexOfStar = appData.readBuffer.IndexOf('*');
int indexOfA = appData.readBuffer.IndexOf('A');
int indexOfD = appData.readBuffer.IndexOf('D');
if (indexOfA < indexOfStar || indexOfD < indexOfStar)
{
int startIndex;
if (indexOfA < indexOfD)
startIndex = indexOfA;
else
startIndex = indexOfD;
if (startIndex < 0)
return;
oneMessage = appData.readBuffer.Substring(startIndex, indexOfStar + 1);
appData.readBuffer = appData.readBuffer.Remove(0, indexOfStar + 1);
}
}
else
return;
if (oneMessage == "")
return;
if (oneMessage[0] == 'D')
{
if (oneMessage[1] == '1')
{
if (oneMessage[2] == '1')
{
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
DI1TB.Text = "1";
});
}
else if (oneMessage[2] == '0')
{
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
DI1TB.Text = "0";
});
}
}
}
}
I think that it's not really that important to descibe how this function exactly works. It analyzes data and sets Text property of DI1TB TextBlock.
So what I'm asking for is opinions of yours (and maybe some proposals) of what could be improved, what is wrong. As I said, I'm not an experienced programmer and this is just a side project of mine, which I hope will help me to develop some skills. I also hope you could help me with that, instruct me a little.