Android实现的简单蓝牙程序示例

作者:pku_android 时间:2021-06-27 01:23:44 

本文实例讲述了Android实现的简单蓝牙程序。分享给大家供大家参考,具体如下:

我将在这篇文章中介绍了的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遥控器了!

PS:关于AndroidManifest.xml详细内容可参考本站在线工具:

Android Manifest功能与权限描述大全:

http://tools.jb51.net/table/AndroidManifest

希望本文所述对大家Android程序设计有所帮助。

标签:Android,蓝牙
0
投稿

猜你喜欢

  • Java毕业设计实战之宠物医院与商城一体的系统的实现

    2023-06-02 13:28:44
  • Android 中Activity 之间传递参数

    2022-09-06 04:29:20
  • Java中lambda表达式实现aop切面功能

    2022-12-02 09:44:59
  • MyBatis找不到mapper文件的实现

    2023-12-15 09:22:11
  • VS Code里使用Debugger for Unity插件调试的方法(2023最新版)

    2023-09-22 02:55:38
  • Android开发双向滑动选择器范围SeekBar实现

    2023-04-07 14:46:59
  • JavaMail入门教程之发送邮件(3)

    2023-02-01 20:01:16
  • word ppt excel文档转换成pdf的C#实现代码

    2022-10-10 16:51:54
  • Spring security登录过程逻辑详解

    2022-09-04 19:58:20
  • Android实现购物车功能

    2022-08-14 19:06:55
  • springboot接收别人上传的本地视频实例代码

    2023-03-22 01:55:39
  • 浅谈@FeignClient中name和value属性的区别

    2023-11-06 13:04:14
  • Java读取Properties文件的七种方法的总结

    2022-08-12 13:27:18
  • Android自定义View实现BMI指数条

    2021-08-28 18:12:12
  • 在Spring Boot2中使用CompletableFuture的方法教程

    2022-09-27 14:18:51
  • Java之Jackson的基本使用案例讲解

    2022-05-27 22:08:49
  • 细品Java8中hashCode方法的使用

    2023-10-04 14:01:19
  • C#使用Socket发送和接收TCP数据实例

    2022-12-13 13:33:46
  • C#字符串和Acsii码相互转换

    2022-09-24 00:12:07
  • Java常用JVM参数实战

    2023-03-15 10:33:39
  • asp之家 软件编程 m.aspxhome.com