Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

xtboy's Content

There have been 1 items by xtboy (Search limited from 17-April 23)


By content type

See this member's

Sort by                Order  

#57693 Netduino communicating with Android BT

Posted by xtboy on 23 April 2014 - 01:00 AM in Netduino Plus 2 (and Netduino Plus 1)

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 = "";

            }
        }    
    }
 





home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.