现在的位置: 首页 > 综合 > 正文

一个简单实用的Android蓝牙程序实例

2013年10月12日 ⁄ 综合 ⁄ 共 4221字 ⁄ 字号 评论关闭

作者:   张奇

       我将在这篇文章中介绍了的Android蓝牙程序。这个程序就是将实现把手机变做电脑PPT播放的遥控器:用音量加和音量减键来控制PPT页面的切换。

遥控器服务器端

首先,我们需要编写一个遥控器的服务器端(支持蓝牙的电脑)来接收手机端发出的信号。为了实现这个服务器端,我用到了一个叫做Bluecove(专门用来为蓝牙服务的!)的Java库。

以下是我的RemoteBluetoothServer类:

public class RemoteBluetoothServer{

	public static void main(String[] args) {
		Thread waitThread = new Thread(new WaitThread());
		waitThread.start();
	}
}


在主方法中创建了一个线程,用于连接客户端,并处理信号。


public class WaitThread implements Runnable{

	/** Constructor */
	public WaitThread() {
	}

	@Override
	public void run() {
		waitForConnection();
	}

	/** Waiting for connection from devices */
	private void waitForConnection() {
		// retrieve the local Bluetooth device object
		LocalDevice local = null;

		StreamConnectionNotifier notifier;
		StreamConnection connection = null;

		// setup the server to listen for connection
		try {
			local = LocalDevice.getLocalDevice();
			local.setDiscoverable(DiscoveryAgent.GIAC);

			UUID uuid = new UUID(80087355); // "04c6093b-0000-1000-8000-00805f9b34fb"
			String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
			notifier = (StreamConnectionNotifier)Connector.open(url);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
       	       	// waiting for connection
		while(true) {
			try {
				System.out.println("waiting for connection...");
	                  	connection = notifier.acceptAndOpen();

				Thread processThread = new Thread(new ProcessConnectionThread(connection));
				processThread.start();
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
		}
	}
}


在waitForConnection()中,首先将服务器设为可发现的,并为这个程序创建了UUID(用于同客户端通信);然后就等待来自客户端的连接请求。当它收到一个初始的连接请求时,将创建一个ProcessConnectionThread来处理来自客户端的命令。以下是ProcessConnectionThread的代码:

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) {
	    		case KEY_RIGHT:
	    			robot.keyPress(KeyEvent.VK_RIGHT);
	    			System.out.println("Right");
	    			break;
	    		case KEY_LEFT:
	    			robot.keyPress(KeyEvent.VK_LEFT);
	    			System.out.println("Left");
	    			break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


ProcessConnectionThread类主要用于接收并处理客户端发送的命令。需要处理的命令只有两个:KEY_RIGHT和KEY_LEFT。我用java.awt.Robot来生成电脑端的键盘事件。

 

以上就是服务器端所需要做的工作。

 

遥控器客户端

这里的客户端指的其实就是Android手机。在开发手机端代码的过程中,我参考了 Android Dev Guide中Bluetooth Chat这个程序的代码,这个程序在SDK的示例代码中可以找到。

 

要将客户端连接服务器端,那么必须让手机可以扫描到电脑,DeviceListActivity 类的工作就是扫描并连接服务器。BluetoothCommandService类负责将命令传至服务器端。这两个类与Bluetooth Chat中的内容相似,只是删除了Bluetooth Chat中的BluetoothCommandService中的AcceptThread ,因为客户端不需要接受连接请求。ConnectThread用于初始化与服务器的连接,ConnectedThread 用于发送命令。

RemoteBluetooth 是客户端的主activity,其中主要代码如下:


protected void onStart() {
	super.onStart();

	// If BT is not on, request that it be enabled.
        // setupCommand() will then be called during onActivityResult
	if (!mBluetoothAdapter.isEnabled()) {
		Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
	}
	// otherwise set up the command service
	else {
		if (mCommandService==null)
			setupCommand();
	}
}

private void setupCommand() {
	// Initialize the BluetoothChatService to perform bluetooth connections
        mCommandService = new BluetoothCommandService(this, mHandler);
}

onStart()用于检查手机上的蓝牙是否已经打开,如果没有打开则创建一个Intent来打开蓝牙。setupCommand()用于在按下音量加或音量减键时向服务器发送命令。其中用到了onKeyDown事件:


public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
		mCommandService.write(BluetoothCommandService.VOL_UP);
		return true;
	}
	else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
		mCommandService.write(BluetoothCommandService.VOL_DOWN);
		return true;
	}

	return super.onKeyDown(keyCode, event);
}

此外,还需要在AndroidManifest.xml加入打开蓝牙的权限的代码。


 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />


以上就是客户端的代码。

 

将两个程序分别在电脑和手机上安装后,即可实现用手机当作一个PPT遥控器了!

 

参考文献:

http://developer.android.com/guide/topics/wireless/bluetooth.html

http://developer.android.com/resources/samples/BluetoothChat/index.html

抱歉!评论已关闭.