In this blog post, I will show you a simple way of turning your Android phone into a wireless touchpad. For this to work all you need is your Android phone, an Arduino board and a HC-06 (or HC-05) bluetooth module based on BC417. A short demonstration video is included towards the end of the post.

One of the easiest ways to drive a mouse or keyboard is via the USB HID interface. Many of the Arduino boards (such as Arduino Leonardo, Arduino Micro or Arduino Due) can be conveniently turned into an HID device. For my implementation, I used an Arduino Due, and Arduino mouse and keyboard library is used to drive the mouse.

bluetoothtrackpad.

On the Android side, I wrote a simple app (largely based on the examples discussed here) which sends the current touch locations and button click events via Bluetooth. And on the Arduino side, the Bluetooth to serial adapter sends the received commands to Arduino’s serial port. The event commands are then parsed and used to drive the mouse. To simplify the serial communications, I used a simple serial protocol I created earlier. You can take a look at that post for more detailed explanation of the protocol.

The serial commands sent from the Android to the Arduino take the following format:

x pos, y pos, action

The action field indicate whether it is a mouse move event (0), a left click (1) or a right click (2).

Here is the Arduino source code in its entirety:

#include <SimpleSerialProtocol.h>

SimpleSerialProtocol p;

int xOrg = 0, yOrg = 0, xVal = 0, yVal = 0, xDelta = 0, yDelta = 0;

void setup() {
  Serial1.begin(9600);
  p.CmdReceivedPtr = CmdReceived;
  Mouse.begin();
}

void loop() {
  p.receive();
}

void CmdReceived(byte* cmd, byte cmdLength)
{
  String s = String((char *)cmd);
  String x = parseCSV(s, 0);
  String y = parseCSV(s, 1);

  xVal = x.toInt();
  yVal = y.toInt();

  if (xVal == 0 && yVal == 0) { //buton clicked
    String c = parseCSV(s, 2);
    int btnVal = c.toInt();
    
    if (btnVal == 1) Mouse.click(MOUSE_LEFT);      
    else if (btnVal == 2) Mouse.click(MOUSE_RIGHT);    
  } else {  // moving
    if (abs(xVal - xOrg) > 1) xDelta = (xVal - xOrg);
    else xDelta = 0;

    if (abs(yVal - yOrg) > 1) yDelta = (yVal - yOrg);
    else yDelta = 0;

    if (xDelta != 0 || yDelta != 0) {
      Mouse.move(xDelta, yDelta, 0);
      xOrg = xVal;
      yOrg = yVal;
    }
  }
}

String parseCSV(String data, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  char separator = ',';
  
  int maxIndex = data.length() - 1;
  for (int i = 0; i <= maxIndex && found <= index; i++) {
    if (data.charAt(i) == separator || i == maxIndex) {
      found++;
      strIndex[0] = strIndex[1] + 1;
      strIndex[1] = (i == maxIndex) ? i + 1 : i;
    }
  }

  return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

The function used to parse the CSV command is modified from the code in this thread on stackexchange. The Java source code on the Android side is included towards the end. For this demonstration, I only implemented mouse move and mouse click events but you can easily implement other functionalities such as scrolling and gestures as well if you want to.

Here is a short video demonstration:


View on YouTube in a new window

Example source code (created using Android Studio)

BluetoothTouchpad.tar.gz

Be Sociable, Share!