A modularisation of the Generic Bluetooth Serial App extracting out all of the UI functionality. Received serial messages are interpreted as Phone Keypad events. Depends upon KeypadUWPLib as it fires the keypad events based upon received messages.

 

The following blog covers the modified Generic Bluetooth Serial App, the BTKeypad App

 

Changes wrt Generic Bluetooth Serial App

  • Code was a copy of GenericBluetothSeiall app MainPage class
  • Was established as a new class, BluetoothSerial in the BluetoothSerialLib namespace. 
  • All UI code was removed
  • The InitializeRfcommDeviceService() method was removed as this list oriented code was to remain in the Main app.
    Double click in Main app on an item in list places a call to this class, using the selected item, to ConnectDevice( ).
    async public Task<bool> ConnectDevice(PairedDeviceInfo pairedDevice)
    It returns the status (true or false) of the connection
  • PairedDeviceInfo class  remains in this class (Main app refers to it.)
  • Main app UI  button_Click( ) method calls button_Click( ) method in this class to action its calls.
  • But Main App UI modifications remain in Main App (such as Command Buttons enabled/disabled).
  • The Keypad class is referenced and made a property. The Main app instantiates it as sets its property in this class.
    The Main app need access to the Keypad instance so that it can implement and set the Keypad event handlers.
  • The ReadAsync method was modified to fire keypad events in the Keypad class as events (as two character strings) are received over the Bluetooth serial stream.

 

 

The BluetoothSerial Class Diagram

image

 

The Keypad Class Reference

The Keypad class is referenced and instantiated as a property.

The Main app need access to the Keypad instance (property) so that it can set the Keypad event handlers.
Keypad event Handler code is shown here as comments.

        public KeypadUWPLib.Keypad Keypad { get; internal set; }
        public BluetoothSerial()  //Class constructor
        {
            Keypad = new KeypadUWPLib.Keypad();
            ////Specific handlers
            //Keypad.KeyDown += Keypad_KeyDown;
            //Keypad.KeyUp += Keypad_KeyUp;
            //Keypad.KeyHolding += Keypad_KeyHolding;
            ////Common handlers
            //Keypad.KeyDown += Keypad_Keys;
            //Keypad.KeyUp += Keypad_Keys;
            //Keypad.KeyHolding += Keypad_Keys;
        }

        /*
        private void Keypad_Keys(object sender, KeypadUWPLib.KeypadEventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine("Keys " + e.Key + " " + e.Action.ToString());
        }*/

 

 

ReadAsync( )

The string reception in ReadAsync( ) is modified to raise keypress events as a complete keypress event string is received from the Bluetooth serial stream from the Arduino device.

Related declarations:

        int recvbytesCntr = 0;
        byte recvbytes;

Modified method code:

            if (bytesRead > 0)
            {
                try
                {
                    byte temp = new byte[bytesRead];
                    dataReaderObject.ReadBytes(temp);  
                    for (int i=0; i< bytesRead; i++)
                    {
                        recvbytes[recvbytesCntr++] = temp[i];
                        if (recvbytesCntr == 3)
                        {
                            if (recvbytes[2] == 0)
                            {
                                String MyString = Encoding.ASCII.GetString(recvbytes).TrimEnd((Char)0);
                                Keypad.RaiseTestEvent(this, MyString);
                                recvbytesCntr = 0;
                                break;
                            }
                            else
                            {
                                //Errant condition
                                recvbytesCntr = 0;
                                break;
                            }
                        }
                    }
                }
catch (Exception ex)
{
. . . .
}
}