dimanche 22 février 2015

How to send integer via bluetooth to Arduino in WP8?

I am making an application where I need to send my phone's accelerometer readings to my Arduino via bluetooth. I am able to send strings easily but, I am getting errors when I am altering the function for integers. Here is the code I used:



private async void BT2Arduino_Sendint(int value)
{



if (BTSock == null) // If we don't have a connection, Send Error Control
{
// MessageBox.Show("Please connect to a device first."); // Alert the user with a Notification (Optional)
txtBTStatus.Text = "No connection found. Try again!"; // Alert the UI
return; // Stop
}
else
if (BTSock != null) // Since we have a Connection
{
byte[] buffer = new byte[] { Convert.ToByte(value) };
var datab = GetBufferFromByteArray(UTF8Encoding.UTF8.GetBytes(buffer, 0, 4)); // Create Buffer/Packet for Sending
await BTSock.OutputStream.WriteAsync(datab); // Send our Message to Connected Arduino

txtBTStatus.Text = "Connected to the Device";
}
}


private IBuffer GetBufferFromByteArray(byte[] package)
{
using (DataWriter dw = new DataWriter())
{
dw.WriteBytes(package);
return dw.DetachBuffer();
}
}


The error is basically in the line where I am using UTF8 encoding. It says "The best overloaded method match for System.Text.Encoding.GetBytes(char[], int, int) has some invalid arguments" Please help me resolve this issue as soon as you can. I know I am doing a mistake with the basics, but I don't have much knowledge with Encoding. Thanks for any help you can provide. :)


Aucun commentaire:

Enregistrer un commentaire