Android Open Source - Remote-Bluetooth-Android-master Process Connection Thread






From Project

Back to project page Remote-Bluetooth-Android-master.

License

The source code is released under:

MIT License

If you think the Android project Remote-Bluetooth-Android-master listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.luugiathuy.apps.remotebluetooth;
//from   www  .  j  a  va2s .co  m
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) {
        case KEY_RIGHT:
          robot.keyPress(KeyEvent.VK_RIGHT);
          System.out.println("Right");
          // release the key after it is pressed. Otherwise the event just keeps getting trigged          
          robot.keyRelease(KeyEvent.VK_RIGHT);
          break;
        case KEY_LEFT:
          robot.keyPress(KeyEvent.VK_LEFT);
          System.out.println("Left");
          // release the key after it is pressed. Otherwise the event just keeps getting trigged          
          robot.keyRelease(KeyEvent.VK_LEFT);
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

com.luugiathuy.apps.remotebluetooth.BluetoothCommandService.java
com.luugiathuy.apps.remotebluetooth.DeviceListActivity.java
com.luugiathuy.apps.remotebluetooth.ProcessConnectionThread.java
com.luugiathuy.apps.remotebluetooth.RemoteBluetoothServer.java
com.luugiathuy.apps.remotebluetooth.RemoteBluetooth.java
com.luugiathuy.apps.remotebluetooth.WaitThread.java