Need to modify the code so that when the button on the netduino is pressed, it sends a message to the bluetooth terminal on my android phone.
public class Program
{
public static string outBuff = "";
static SerialPort serialPort;
static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
public static void Main()
{
string portName = SerialPorts.COM1;
int baudRate = 9600;
Parity parity = Parity.Odd;
int dataBits = 8;
StopBits stopBits = StopBits.One;
serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
serialPort.Open();
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
byte[] bytesToSend = encoder.GetBytes("HHHHHHHHH\r");
serialPort.Write(bytesToSend, 0, bytesToSend.Length);
Debug.Print("This is debug print Did you see string sent thru serial port?");
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
while (true)
{
serialPort.Write(bytesToSend, 0, bytesToSend.Length);// keep on sending the string out forever
Thread.Sleep(1000); // wait a bit to give a chance for receiving a few bytes ...
}
}
static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{ // This is woken up upon reception of data from the com1 port, to handle the received data.
byte[] bufferData = new byte[20];
if ((serialPort.BytesToRead > 4) && (serialPort.BytesToRead < 20))
{
serialPort.Read(bufferData, 0, 20);
///convert received bytes to char array
char[] charArray = System.Text.UTF8Encoding.UTF8.GetChars(bufferData);
//convert char array to string
for (int i = 0; i < charArray.Length - 1; i++)
{
outBuff += charArray[i]; //build the string char by char.
}
Debug.Print(outBuff + "\n");
if (outBuff.IndexOf("off") >= 0)
{
led.Write(false);
}
else if (outBuff.IndexOf("on") >= 0)
{
led.Write(true);
}
outBuff = "";
}
}
}