android如何实现蓝牙控制PC端
这篇文章主要为大家展示了“android如何实现蓝牙控制PC端”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“android如何实现蓝牙控制PC端”这篇文章吧。
成都创新互联公司是一家网站制作、做网站,提供网页设计,网站设计,网站制作,建网站,专业公司,网站开发公司,于2013年开始是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。
引言
在安卓端通过蓝牙发送指令到PC端,java程序接收指令,并执行相应的动作。其中指令格式有所规范,PC端的java程序通过robot库进行操作
代码
控制类remotePC.java
import java.awt.AWTException; import java.awt.Dimension; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.io.IOException; public class remotePC { //保存当前鼠标指针的坐标(px,py) private static int px; private static int py; //最大延迟时间:1秒 public static final int MAX_DELAY = 1000; //最小间隔时间:1毫秒 public static final int SAMPLE_TIME_DIV = 1; //魔法数字,用于设置默认的事件delay时间间隔 private final double magicX = 1.0; //视觉延迟:默认100ms private final int VISIBAL_MOVEMENT = 100; //PC屏幕尺寸 private int screenWidth; private int screenHeight; //手机屏幕尺寸 private int mobileWidth; private int mobileHeight; //手机电脑尺寸转换的比例 private double widScale; private double heiScale; //用于控制的robot类 private Robot robot; //默认构造函数 public remotePC() throws AWTException{ this(1366, 768); } //构造函数,指定手机屏幕尺寸 public remotePC(int mobileWidth, int mobileHeight) throws AWTException{ robot = new Robot(); robot.setAutoDelay((int)magicX); setScreenSize(); this.mobileHeight = mobileHeight; this.mobileWidth = mobileWidth; setScale(); } public void moveToCenter(){ this.move(screenWidth/2, screenHeight/2, 1); } //[鼠标光标移动] //dt:间隔时间,时间长短对应速度 //dx,dy:手机上移动的相对横纵位移,自动转换为pc上应该移动的尺寸 public void move(int dx, int dy, int dt){ double deltaX = (1.0*dx/widScale); double deltaY = (1.0*dy/heiScale); int dxpms = (int)deltaX/dt; int dypms = (int)deltaY/dt; for(int i=0; i
PC端通过蓝牙和安卓app交互:BluetoothServer.java
/** * Created by luyudi on 2016/11/9. * Modified by Lannooo on 2016/12/4. */ // server import java.awt.*; import java.io.InputStream; import java.io.IOException; import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnectionNotifier; public class BlueToothServer implements Runnable { private remotePC Controller; // 下面的UUID必须和手机客户端的UUID相一致 private static UUID ECHO_SERVER_UUID= new UUID("aeb9f938a1a34947ace29ebd0c67adf1", false); // 流连接通知 用于创建流连接 private StreamConnectionNotifier myPCConnNotifier = null; // 流连接 private StreamConnection streamConn = null; // 接受数据字节流 // 接收x y 坐标 private byte[] acceptedByteArray = new byte[1024]; // 读取(输入)流 private InputStream inputStream = null; // 主线程 public static void main(String[] args) { new BlueToothServer(); } public BlueToothServer() { try { String url = "btspp://localhost:" + ECHO_SERVER_UUID.toString(); // 得到流连接通知 myPCConnNotifier = (StreamConnectionNotifier) Connector.open(url); Controller = new remotePC(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // 打开连接通道并读取流线程 new Thread(this).start(); } public static int getInt(byte[] bytes){ return (0xff & bytes[3]) | (0xff00 & (bytes[2] << 8)) | (0xff0000 & (bytes[1] << 16)) | (0xff000000 & (bytes[0] << 24)); } public static float getFloat(byte[] b){ return Float.intBitsToFloat(getInt(b)); } @Override public void run() { try { boolean isMouseLPressed = false; boolean isWheelPressed = false; boolean end = false; while (true) { // 持续保持着监听客户端的连接请求 // 获取流连接 streamConn = myPCConnNotifier.acceptAndOpen(); // 获取流通道 inputStream = streamConn.openInputStream(); // 循环读取字节流,判断code类型和x,y坐标 while (inputStream.read(acceptedByteArray) != -1) { String acceptString = new String(acceptedByteArray); int index; if((index = acceptString.indexOf("RemoteTouch")) != -1) { byte[] codeBytes = new byte[4]; byte[] dxBytes = new byte[6]; byte[] dyBytes = new byte[6]; System.arraycopy(acceptedByteArray, index + 11, codeBytes, 0, 4); System.arraycopy(acceptedByteArray, index + 15, dyBytes, 0, 6); System.arraycopy(acceptedByteArray, index + 21, dxBytes, 0, 6); int dy = Integer.parseInt(new String(dyBytes)); int dx = Integer.parseInt(new String(dxBytes)); int code = getInt(codeBytes); if (end) { inputStream.close(); if (streamConn != null) { streamConn.close(); System.out.println("Disconnected..."); } break; } switch (code) { case 1://按下鼠标左键 isMouseLPressed = true; Controller.pressMouseL(); System.out.println("Pressing mouse L"); break; case 2://释放鼠标左键 if (isMouseLPressed) { Controller.releaseMouseL(); System.out.println("Released mouse L"); isMouseLPressed=false; } break; case 3://点击鼠标左键 Controller.clickMouseL(); System.out.println("Clicked mouse L"); break; case 4://点击鼠标右键 Controller.clickMouseR(); System.out.println("Clicked mouse R"); break; case 5://按下滚轮 // isWheelPressed = true; // Controller.pressWheel(); // System.out.println("Pressing wheel"); break; case 6://释放滚轮 // if(isWheelPressed){ // Controller.releaseWheel(); // System.out.println("Released wheel"); // } break; case 7://滚轮滚动 int step = Math.abs(dy) / 40; System.out.println("wheel"); if (dy > 0) { Controller.wheelDown(step); System.out.printf("Wheel Down:%d steps. dy=%d\n", step, dy); } else { Controller.wheelUp(step); System.out.printf("Wheel Up:%d steps. dy=%d\n", step, dy); } break; case 8://放大、缩小 double s = Math.sqrt((double) (dx * dx + dy * dy)); if (dx < 0) { Controller.zoomOut((int) s / 20); System.out.printf("Zoom out %d steps. dx=%d,dy=%d\n", (int) s / 20, dx, dy); } else { Controller.zoomIn((int) s / 20); System.out.printf("Zoom in %d steps. dx=%d,dy=%d\n", (int) s / 20, dx, dy); } break; case 9://显示可切换apps Controller.listAppsWindow(); System.out.println("show Switch apps"); break; case 10://显示桌面 Controller.showDesktop(); System.out.println("show desktop"); break; case 11://app 右切 Controller.simpleRightSwitchApp(); System.out.println("switch app: right"); break; case 12://app 左切 Controller.simpleLeftSwitchApp(); System.out.println("switch app: left"); break; case 13://window 右切 Controller.rightSwitchWindow(); System.out.println("switch window right"); break; case 14://window 左切 Controller.leftSwitchWindow(); System.out.println("switch window left"); break; case 15://鼠标左键双击 Controller.clickMouseL(); Controller.delay(1); Controller.clickMouseL(); System.out.println("clicked double mouse L"); break; case 16://鼠标移动 Controller.move(dx, dy, 1); //System.out.printf("Move mouse:dx=%d,dy=%d\n", dx, dy); break; case 17://左分屏 Controller.windowLeft(); System.out.println("Window divide: left"); break; case 18://右分屏 Controller.windowRight(); System.out.println("Window divide: right"); break; case 19: //上一张ppt Controller.prevSlide(); System.out.println("previous slide"); break; case 20: Controller.nextSlide(); System.out.println("Next Slide"); break; case 32: // PPT设置为隐藏鼠标 Controller.hideMouse(); System.out.println("Hide"); break; case 33: // ppt激光笔 Controller.setLaser(); System.out.println("Laser"); break; case 34: // ppt笔 Controller.setDraw(); System.out.println("Draw"); break; case 35: // ppt 荧光笔 Controller.setMark(); System.out.println("Mark"); break; case 100://退出 end = true; System.out.println("Quit."); break; default://未识别 System.out.println("Unknown code"); break; } } // clear data acceptedByteArray = new byte[1024]; } } } catch (IOException e) { e.printStackTrace(); } } }
以上是“android如何实现蓝牙控制PC端”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
网页标题:android如何实现蓝牙控制PC端
网页URL:http://myzitong.com/article/jjpojd.html