android蓝牙控制PC端代码分享
引言
在安卓端通过蓝牙发送指令到PC端,java程序接收指令,并执行相应的动作。其中指令格式有所规范,PC端的java程序通过robot库进行操作
代码
控制类remotePC.java
importjava.awt.AWTException; importjava.awt.Dimension; importjava.awt.Robot; importjava.awt.Toolkit; importjava.awt.event.InputEvent; importjava.awt.event.KeyEvent; importjava.io.IOException; publicclassremotePC{ //保存当前鼠标指针的坐标(px,py) privatestaticintpx; privatestaticintpy; //最大延迟时间:1秒 publicstaticfinalintMAX_DELAY=1000; //最小间隔时间:1毫秒 publicstaticfinalintSAMPLE_TIME_DIV=1; //魔法数字,用于设置默认的事件delay时间间隔 privatefinaldoublemagicX=1.0; //视觉延迟:默认100ms privatefinalintVISIBAL_MOVEMENT=100; //PC屏幕尺寸 privateintscreenWidth; privateintscreenHeight; //手机屏幕尺寸 privateintmobileWidth; privateintmobileHeight; //手机电脑尺寸转换的比例 privatedoublewidScale; privatedoubleheiScale; //用于控制的robot类 privateRobotrobot; //默认构造函数 publicremotePC()throwsAWTException{ this(1366,768); } //构造函数,指定手机屏幕尺寸 publicremotePC(intmobileWidth,intmobileHeight)throwsAWTException{ robot=newRobot(); robot.setAutoDelay((int)magicX); setScreenSize(); this.mobileHeight=mobileHeight; this.mobileWidth=mobileWidth; setScale(); } publicvoidmoveToCenter(){ this.move(screenWidth/2,screenHeight/2,1); } //[鼠标光标移动] //dt:间隔时间,时间长短对应速度 //dx,dy:手机上移动的相对横纵位移,自动转换为pc上应该移动的尺寸 publicvoidmove(intdx,intdy,intdt){ doubledeltaX=(1.0*dx/widScale); doubledeltaY=(1.0*dy/heiScale); intdxpms=(int)deltaX/dt; intdypms=(int)deltaY/dt; for(inti=0;i
PC端通过蓝牙和安卓app交互:BluetoothServer.java
/** *Createdbyluyudion2016/11/9. *ModifiedbyLannoooon2016/12/4. */ //server importjava.awt.*; importjava.io.InputStream; importjava.io.IOException; importjavax.bluetooth.UUID; importjavax.microedition.io.Connector; importjavax.microedition.io.StreamConnection; importjavax.microedition.io.StreamConnectionNotifier; publicclassBlueToothServerimplementsRunnable{ privateremotePCController; //下面的UUID必须和手机客户端的UUID相一致 privatestaticUUIDECHO_SERVER_UUID=newUUID("aeb9f938a1a34947ace29ebd0c67adf1",false); //流连接通知用于创建流连接 privateStreamConnectionNotifiermyPCConnNotifier=null; //流连接 privateStreamConnectionstreamConn=null; //接受数据字节流 //接收xy坐标 privatebyte[]acceptedByteArray=newbyte[1024]; //读取(输入)流 privateInputStreaminputStream=null; //主线程 publicstaticvoidmain(String[]args){ newBlueToothServer(); } publicBlueToothServer(){ try{ Stringurl="btspp://localhost:"+ECHO_SERVER_UUID.toString(); //得到流连接通知 myPCConnNotifier=(StreamConnectionNotifier)Connector.open(url); Controller=newremotePC(); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } //打开连接通道并读取流线程 newThread(this).start(); } publicstaticintgetInt(byte[]bytes){ return(0xff&bytes[3])| (0xff00&(bytes[2]<<8))| (0xff0000&(bytes[1]<<16))| (0xff000000&(bytes[0]<<24)); } publicstaticfloatgetFloat(byte[]b){ returnFloat.intBitsToFloat(getInt(b)); } @Override publicvoidrun(){ try{ booleanisMouseLPressed=false; booleanisWheelPressed=false; booleanend=false; while(true){ //持续保持着监听客户端的连接请求 //获取流连接 streamConn=myPCConnNotifier.acceptAndOpen(); //获取流通道 inputStream=streamConn.openInputStream(); //循环读取字节流,判断code类型和x,y坐标 while(inputStream.read(acceptedByteArray)!=-1){ StringacceptString=newString(acceptedByteArray); intindex; if((index=acceptString.indexOf("RemoteTouch"))!=-1){ byte[]codeBytes=newbyte[4]; byte[]dxBytes=newbyte[6]; byte[]dyBytes=newbyte[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); intdy=Integer.parseInt(newString(dyBytes)); intdx=Integer.parseInt(newString(dxBytes)); intcode=getInt(codeBytes); if(end){ inputStream.close(); if(streamConn!=null){ streamConn.close(); System.out.println("Disconnected..."); } break; } switch(code){ case1://按下鼠标左键 isMouseLPressed=true; Controller.pressMouseL(); System.out.println("PressingmouseL"); break; case2://释放鼠标左键 if(isMouseLPressed){ Controller.releaseMouseL(); System.out.println("ReleasedmouseL"); isMouseLPressed=false; } break; case3://点击鼠标左键 Controller.clickMouseL(); System.out.println("ClickedmouseL"); break; case4://点击鼠标右键 Controller.clickMouseR(); System.out.println("ClickedmouseR"); break; case5://按下滚轮 //isWheelPressed=true; //Controller.pressWheel(); //System.out.println("Pressingwheel"); break; case6://释放滚轮 //if(isWheelPressed){ //Controller.releaseWheel(); //System.out.println("Releasedwheel"); //} break; case7://滚轮滚动 intstep=Math.abs(dy)/40; System.out.println("wheel"); if(dy>0){ Controller.wheelDown(step); System.out.printf("WheelDown:%dsteps.dy=%d\n",step,dy); }else{ Controller.wheelUp(step); System.out.printf("WheelUp:%dsteps.dy=%d\n",step,dy); } break; case8://放大、缩小 doubles=Math.sqrt((double)(dx*dx+dy*dy)); if(dx<0){ Controller.zoomOut((int)s/20); System.out.printf("Zoomout%dsteps.dx=%d,dy=%d\n",(int)s/20,dx,dy); }else{ Controller.zoomIn((int)s/20); System.out.printf("Zoomin%dsteps.dx=%d,dy=%d\n",(int)s/20,dx,dy); } break; case9://显示可切换apps Controller.listAppsWindow(); System.out.println("showSwitchapps"); break; case10://显示桌面 Controller.showDesktop(); System.out.println("showdesktop"); break; case11://app右切 Controller.simpleRightSwitchApp(); System.out.println("switchapp:right"); break; case12://app左切 Controller.simpleLeftSwitchApp(); System.out.println("switchapp:left"); break; case13://window右切 Controller.rightSwitchWindow(); System.out.println("switchwindowright"); break; case14://window左切 Controller.leftSwitchWindow(); System.out.println("switchwindowleft"); break; case15://鼠标左键双击 Controller.clickMouseL(); Controller.delay(1); Controller.clickMouseL(); System.out.println("clickeddoublemouseL"); break; case16://鼠标移动 Controller.move(dx,dy,1); //System.out.printf("Movemouse:dx=%d,dy=%d\n",dx,dy); break; case17://左分屏 Controller.windowLeft(); System.out.println("Windowdivide:left"); break; case18://右分屏 Controller.windowRight(); System.out.println("Windowdivide:right"); break; case19://上一张ppt Controller.prevSlide(); System.out.println("previousslide"); break; case20: Controller.nextSlide(); System.out.println("NextSlide"); break; case32://PPT设置为隐藏鼠标 Controller.hideMouse(); System.out.println("Hide"); break; case33://ppt激光笔 Controller.setLaser(); System.out.println("Laser"); break; case34://ppt笔 Controller.setDraw(); System.out.println("Draw"); break; case35://ppt荧光笔 Controller.setMark(); System.out.println("Mark"); break; case100://退出 end=true; System.out.println("Quit."); break; default://未识别 System.out.println("Unknowncode"); break; } } //cleardata acceptedByteArray=newbyte[1024]; } } }catch(IOExceptione){ e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。