Simple Android and Java Bluetooth Application
Simple Android and Java Bluetooth Application
Last week was my schools recess week. I had a lot of free time and decided
to learn Java and Android Bluetooth by reading the Bluetooth development
guide for Android. Then I had an idea to make my Android phone become a
simple remote control for my laptop, just for controlling the Power Point
slides for presentation. The volume up and volume down become buttons
for going to next and previous slide respectively. I write this post to share
with you what I have done. I have used Ecipse IDE to write the program.
REMOTE CONTROL SERVER (Java)
Firstly, we need to write the remote control server to receive the signal from
Android phone. I used a Java library for Bluetooth called Bluecove to
implement the server. You can download the bluecove-2.1.0.jar file and add
it to your external library. Note that for Linux, you need to install the bluezlibs to your system and add bluecove-gpl-2.1.0.jar to external library of the
project as well (more informationhere).
Here is my RemoteBluetoothServer class:
Java
1 package com.luugiathuy.apps.remotebluetooth;
2
3 public class RemoteBluetoothServer{
4
5 public static void main(String[] args) {
6 Thread waitThread = new Thread(new WaitThread());
7 waitThread.start();
8 }
9}
The main method creates a thread to wait for connection from client and
handle the signal.
Java
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
package com.luugiathuy.apps.remotebluetooth;
import
import
import
import
import
import
javax.bluetooth.DiscoveryAgent;
javax.bluetooth.LocalDevice;
javax.bluetooth.UUID;
javax.microedition.io.Connector;
javax.microedition.io.StreamConnection;
javax.microedition.io.StreamConnectionNotifier;
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3 }
4 }
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
package com.luugiathuy.apps.remotebluetooth;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import javax.microedition.io.StreamConnection;
public class ProcessConnectionThread implements Runnable{
private StreamConnection mConnection;
// Constant that indicate command from devices
private static final int EXIT_CMD = -1;
private static final int KEY_RIGHT = 1;
private static final int KEY_LEFT = 2;
public ProcessConnectionThread(StreamConnection connection)
{
mConnection = connection;
}
@Override
public void run() {
try {
// prepare to receive data
InputStream inputStream = mConnection.openInputStream();
System.out.println("waiting for input");
while (true) {
int command = inputStream.read();
if (command == EXIT_CMD)
{
System.out.println("finish process");
break;
}
processCommand(command);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Process the command from client
* @param command the command code
*/
private void processCommand(int command) {
try {
Robot robot = new Robot();
switch (command) {
3
case KEY_RIGHT:
2
robot.keyPress(KeyEvent.VK_RIGHT);
3
System.out.println("Right");
3
break;
3
case KEY_LEFT:
4
robot.keyPress(KeyEvent.VK_LEFT);
3
System.out.println("Left");
5
break;
3
}
6
} catch (Exception e) {
3
e.printStackTrace();
7
}
3 }
8 }
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
6
7
8
9
1
0
if (!mBluetoothAdapter.isEnabled()) {
1
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
1
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
1
}
2
// otherwise set up the command service
1
else {
3
if (mCommandService==null)
1
setupCommand();
4
}
1
}
5
1
private void setupCommand() {
6
// Initialize the BluetoothChatService to perform bluetooth connections
1
mCommandService = new BluetoothCommandService(this, mHandler);
7
}
1
8
1
9
2
0
1
2
public boolean onKeyDown(int keyCode, KeyEvent event) {
3
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
4
mCommandService.write(BluetoothCommandService.VOL_UP);
5
return true;
6
}
7
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
8
mCommandService.write(BluetoothCommandService.VOL_DOWN);
9
return true;
1
}
0
1
return super.onKeyDown(keyCode, event);
1
}
1
2
Thats it. Now we can run the server, install the application to the phone
and run it
You can go to https://github.com/luugiathuy/Remote-Bluetooth-Android to
download the project for client and server.
http://luugiathuy.com/2011/02/android-java-bluetooth/