這篇文章主要為大家展示了“android如何實(shí)現(xiàn)藍(lán)牙控制PC端”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“android如何實(shí)現(xiàn)藍(lán)牙控制PC端”這篇文章吧。
成都創(chuàng)新互聯(lián)公司是一家網(wǎng)站制作、做網(wǎng)站,提供網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,專業(yè)公司,網(wǎng)站開發(fā)公司,于2013年開始是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開發(fā),后臺(tái)程序制作以及后期項(xiàng)目運(yùn)營(yíng)并提出專業(yè)建議和思路。
引言
在安卓端通過藍(lán)牙發(fā)送指令到PC端,java程序接收指令,并執(zhí)行相應(yīng)的動(dòng)作。其中指令格式有所規(guī)范,PC端的java程序通過robot庫(kù)進(jìn)行操作
代碼
控制類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 { //保存當(dāng)前鼠標(biāo)指針的坐標(biāo)(px,py) private static int px; private static int py; //最大延遲時(shí)間:1秒 public static final int MAX_DELAY = 1000; //最小間隔時(shí)間:1毫秒 public static final int SAMPLE_TIME_DIV = 1; //魔法數(shù)字,用于設(shè)置默認(rèn)的事件delay時(shí)間間隔 private final double magicX = 1.0; //視覺延遲:默認(rèn)100ms private final int VISIBAL_MOVEMENT = 100; //PC屏幕尺寸 private int screenWidth; private int screenHeight; //手機(jī)屏幕尺寸 private int mobileWidth; private int mobileHeight; //手機(jī)電腦尺寸轉(zhuǎn)換的比例 private double widScale; private double heiScale; //用于控制的robot類 private Robot robot; //默認(rèn)構(gòu)造函數(shù) public remotePC() throws AWTException{ this(1366, 768); } //構(gòu)造函數(shù),指定手機(jī)屏幕尺寸 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); } //[鼠標(biāo)光標(biāo)移動(dòng)] //dt:間隔時(shí)間,時(shí)間長(zhǎng)短對(duì)應(yīng)速度 //dx,dy:手機(jī)上移動(dòng)的相對(duì)橫縱位移,自動(dòng)轉(zhuǎn)換為pc上應(yīng)該移動(dòng)的尺寸 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端通過藍(lán)牙和安卓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必須和手機(jī)客戶端的UUID相一致 private static UUID ECHO_SERVER_UUID= new UUID("aeb9f938a1a34947ace29ebd0c67adf1", false); // 流連接通知 用于創(chuàng)建流連接 private StreamConnectionNotifier myPCConnNotifier = null; // 流連接 private StreamConnection streamConn = null; // 接受數(shù)據(jù)字節(jié)流 // 接收x y 坐標(biāo) 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) { // 持續(xù)保持著監(jiān)聽客戶端的連接請(qǐng)求 // 獲取流連接 streamConn = myPCConnNotifier.acceptAndOpen(); // 獲取流通道 inputStream = streamConn.openInputStream(); // 循環(huán)讀取字節(jié)流,判斷code類型和x,y坐標(biāo) 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://按下鼠標(biāo)左鍵 isMouseLPressed = true; Controller.pressMouseL(); System.out.println("Pressing mouse L"); break; case 2://釋放鼠標(biāo)左鍵 if (isMouseLPressed) { Controller.releaseMouseL(); System.out.println("Released mouse L"); isMouseLPressed=false; } break; case 3://點(diǎn)擊鼠標(biāo)左鍵 Controller.clickMouseL(); System.out.println("Clicked mouse L"); break; case 4://點(diǎn)擊鼠標(biāo)右鍵 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://滾輪滾動(dòng) 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://鼠標(biāo)左鍵雙擊 Controller.clickMouseL(); Controller.delay(1); Controller.clickMouseL(); System.out.println("clicked double mouse L"); break; case 16://鼠標(biāo)移動(dòng) 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設(shè)置為隱藏鼠標(biāo) 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://未識(shí)別 System.out.println("Unknown code"); break; } } // clear data acceptedByteArray = new byte[1024]; } } } catch (IOException e) { e.printStackTrace(); } } }
以上是“android如何實(shí)現(xiàn)藍(lán)牙控制PC端”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!