发布于 2017-07-09 08:58:41 | 143 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android实现的简单蓝牙程序,结合实例形式分析了Android蓝牙程序的原理与客户端、服务器端具体实现步骤,需要的朋友可以参考下

本文实例讲述了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功能与权限描述大全:

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务