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
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()); }*/
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) { . . . . } }