Android Studio开发安卓UDP网络读写器APP(二) |
|||
发布者:广州荣士电子有限公司 发布时间: 2022-5-31 | |||
public void SetNetworkPara(byte[] indata){ //根据设备上传的信息生成修改网络参数指令 try {byte[] SendBuf1 = new byte [42]; SendBuf1[0]=(byte)0xF9; SendBuf1[1]=indata[1]; //读卡器ip,如需修改设正确值 SendBuf1[2]=indata[2]; SendBuf1[3]=indata[3]; SendBuf1[4]=indata[4]; SendBuf1[5]=indata[5]; //读卡器子网掩码,如需修改设正确值 SendBuf1[6]=indata[6]; SendBuf1[7]=indata[7]; SendBuf1[8]=indata[8]; SendBuf1[9]=(byte)0xff; //indata[23]; //读卡器远程服务器IP,,255.255.255.255表示广播方式,如需修改设正确值 SendBuf1[10]=(byte)0xff; //indata[24]; SendBuf1[11]=(byte)0xff; //indata[25]; SendBuf1[12]=(byte)0xff; //indata[26]; SendBuf1[13]=indata[27]; //读卡器远程服务器IP MAC,取值FF-FF-FF-FF-FF-FF 表示自动搜索MAC SendBuf1[14]=indata[28]; SendBuf1[15]=indata[29]; SendBuf1[16]=indata[30]; SendBuf1[17]=indata[31]; SendBuf1[18]=indata[32]; SendBuf1[19]=indata[13]; //读卡器网关IP,如需修改设正确值 SendBuf1[20]=indata[14]; SendBuf1[21]=indata[15]; SendBuf1[22]=indata[16]; SendBuf1[23]=indata[17]; //读卡器网关MAC,取值FF-FF-FF-FF-FF-FF 表示自动搜索MAC SendBuf1[24]=indata[18]; SendBuf1[25]=indata[19]; SendBuf1[26]=indata[20]; SendBuf1[27]=indata[21]; SendBuf1[28]=indata[22]; SendBuf1[29]=indata[11]; //机号,如需修改设正确值 SendBuf1[30]=indata[12]; SendBuf1[31]=indata[9]; //通讯端口,如需修改设正确值 SendBuf1[32]=indata[10]; SendBuf1[33]=indata[35]; //机器序列号,每台机唯一,不可更改 SendBuf1[34]=indata[36]; SendBuf1[35]=indata[37]; SendBuf1[36]=indata[38]; byte Funcode; Funcode=1; //0 刷卡时不发声 1 刷卡时发声 Funcode=(byte)(Funcode+2); //+2 远程电脑MAC自动搜索 +4 网关MAC自动搜索 Funcode=(byte)(Funcode+8); //+8 DHCP自动获得IP地址 +0 指定IP Funcode=(byte)(Funcode+0); //+16 卡离开感应区发信息 +0 卡离开感应区不发信息 Funcode=(byte)(Funcode+32*0); //卡在感应区是否重发信息 *0不重发 *1隔1秒重发 *2隔2秒重发 *3隔5秒重发 *4隔10秒重发 SendBuf1[37]=Funcode; SendBuf1[38]=(byte)0x55; //4字节固定后缀 SendBuf1[39]=(byte)0xaa; SendBuf1[40]=(byte)0x66; SendBuf1[41]=(byte)0x99; String SendInfStr=""; for(int p=0;p<42;p++){ String bytestr="00"+Integer.toHexString(SendBuf1[p] & 0xff); SendInfStr=SendInfStr+ bytestr.substring(bytestr.length() -2,bytestr.length())+" "; } updatetrack("修改本设备网络参数指令:"+SendInfStr+"\n"); }catch (Exception e){ updatetrack(e.getMessage()+"\n\n"); } } public void DisableSendAge(byte[] indata,String RemIP,int RemPor){ //确定接收到信息 byte[] SendBuf1 = new byte [9]; SendBuf1[0]=(byte)0x69; String SendInfStr="69 "; for(int p=1;p<9;p++){ SendBuf1[p]=indata[p]; String bytestr="00"+Integer.toHexString(indata[p] & 0xff); SendInfStr=SendInfStr+ bytestr.substring(bytestr.length() -2,bytestr.length())+" "; } try { DatagramSocket s1 = new DatagramSocket(); DatagramPacket dp1 = new DatagramPacket(SendBuf1, SendBuf1.length, InetAddress.getByName(RemIP), RemPor); s1.send(dp1); s1.close(); updatetrack("SendTo:"+RemIP+" Data:"+SendInfStr+"\n\n"); }catch (Exception e){ updatetrack(e.getMessage()+"\n\n"); } } public void SendDispBeep(String CardNum,String RemIP,int RemPor){ //向设备发送显示文字及蜂鸣响声 try { String DispStr="卡号:"+CardNum+" "+GetSysDT().substring(2,19)+" "; //将显示的文字生成字节数组 byte[] DispBuf= DispStr.getBytes("gb2312"); byte[] SendBuf1 = new byte [39]; /** 2、将要发送的数据打包 */ SendBuf1[0]=(byte)0x5a; //指令码 SendBuf1[1]=(byte)0x00; //机号低 SendBuf1[2]=(byte)0x00; //机号高 SendBuf1[3]=(byte)0x02; //蜂鸣声代码 SendBuf1[4]=(byte)0x14; //显示时长 for(int i=0;i<34;i++){ //显示文字 SendBuf1[i+5]=DispBuf[i]; } String SendInfStr=""; for(int p=0;p<39;p++){ String bytestr="00"+Integer.toHexString(SendBuf1[p] & 0xff); SendInfStr=SendInfStr+ bytestr.substring(bytestr.length() -2,bytestr.length())+" "; } DatagramSocket s1 = new DatagramSocket(); DatagramPacket dp1 = new DatagramPacket(SendBuf1, SendBuf1.length, InetAddress.getByName(RemIP), RemPor); Recsock.send(dp1); s1.close(); updatetrack("SendTo:"+RemIP+" Data:"+SendInfStr+"\n\n"); }catch (Exception e){ updatetrack(e.getMessage()+"\n\n"); } } public void SendDispSpk(String CardNum,String RemIP,int RemPor) { //向读卡器发送显示文字及固定组合语音 try { String DispStr="卡号:"+CardNum+" "+GetSysDT().substring(2,19)+" "; //将显示的文字生成字节数组 byte[] DispBuf= DispStr.getBytes("gb2312"); byte SpkLen=3; //固定组合语音长度,一次最大取值21条 byte[] SpkBuf=new byte[SpkLen]; //固定组合语音代码缓冲, SpkBuf[0]=55; //要播报的语音代码0 SpkBuf[1]=41; //要播报的语音代码1 SpkBuf[2]=53; //要播报的语音代码2...... byte[] SendBuf1 = new byte [70]; /** 2、将要发送的数据打包 */ SendBuf1[0]=(byte)0x5B; //指令码 SendBuf1[1]=(byte)0x00; //机号低 SendBuf1[2]=(byte)0x00; //机号高,如果高低位都为0表示任意机号 SendBuf1[3]=(byte)0x02; //蜂鸣声代码,取值范围参看通讯协议 SendBuf1[4]=(byte)0xF0; //继电器代码 F0表示全部继电器 F1表示1号继电器 F2表示2号继电器 SendBuf1[5]=(byte)0x20; //继电器开启时长 低位 SendBuf1[6]=(byte)0x00; //继电器开启时长 高位 SendBuf1[7]=(byte)0x14; //显示保留时间,单位为秒,为0xFF时表示永久显示 SendBuf1[8]=(byte)0x00; //在显示屏中的哪个位置开始,一般取0表示首位 SendBuf1[9]=36; //显示字符串长度 为全屏显示为 36 个字符 SendBuf1[10]=SpkLen; //语音长度 for(int i=0;i<34;i++){ //要显示的文字 SendBuf1[11+i]=DispBuf[i]; } for(int i=0;i<SpkLen;i++){ //要播报的固定语音代码 SendBuf1[47+i]=SpkBuf[i]; } SendBuf1[47+SpkLen+0]=(byte)0x55; //固定的抗干扰后缀 SendBuf1[47+SpkLen+1]=(byte)0xaa; SendBuf1[47+SpkLen+2]=(byte)0x66; SendBuf1[47+SpkLen+3]=(byte)0x99; String SendInfStr=""; for(int p=0;p<=47+SpkLen+3;p++){ String bytestr="00"+Integer.toHexString(SendBuf1[p] & 0xff); SendInfStr=SendInfStr+ bytestr.substring(bytestr.length() -2,bytestr.length())+" "; } DatagramSocket s1 = new DatagramSocket(); /** 1、建立udp socket端点 */ DatagramPacket dp1 = new DatagramPacket(SendBuf1, SendBuf1.length, InetAddress.getByName(RemIP), RemPor); s1.send(dp1); s1.close(); updatetrack("SendTo:"+RemIP+" Data:"+SendInfStr+"\n\n"); }catch (Exception e){ updatetrack(e.getMessage()+"\n\n"); } } public void SendDispTTS(String CardNum,String RemIP,int RemPor) { //向读卡器发送显示文字及TTS中文语音 try { String DispStr="卡号:"+CardNum+" "+GetSysDT().substring(2,19)+" "; //将显示的文字生成字节数组 byte[] DispBuf= DispStr.getBytes("gb2312"); String SpeakStr=""; //将要播报的TTS语音生成字节数组,最大不能超过126个字节 SpeakStr="[v10]"; //定义语音大小,最大语语音为16,可加在任何地方 SpeakStr=SpeakStr+"欢迎您使用我们的网络读卡器"; //要播报的中文语音 byte[] SpeakBuf= SpeakStr.getBytes("gb2312"); int SpeakLen=SpeakBuf.length; //语音长度 int SendLen=11+34+SpeakLen+4; //总计发送数据长度 byte[] SendBuf1 = new byte [180]; /** 2、将要发送的数据打包 */ SendBuf1[0]=(byte)0x5C; //指令码 SendBuf1[1]=(byte)0x00; //机号低 SendBuf1[2]=(byte)0x00; //机号高 SendBuf1[3]=(byte)0x01; //蜂鸣声代码 SendBuf1[4]=(byte)0xF0; //继电器代码 F0表示全部继电器 F1表示1号继电器 F2表示2号继电器 SendBuf1[5]=(byte)0x20; //继电器开启时长 低位 SendBuf1[6]=(byte)0x00; //继电器开启时长 高位 SendBuf1[7]=(byte)0x14; //显示保留时间,单位为秒,为0xFF时表示永久显示 SendBuf1[8]=(byte)0x00; //在显示屏中的哪个位置开始 SendBuf1[9]=(byte)0x22; //显示字符串长度 为全屏显示为 34 个字符 SendBuf1[10]=(byte)SpeakLen; //语音长度 for(int i=0;i<34;i++){ //显示的文字信息 SendBuf1[i+11]=DispBuf[i]; } for(int i=0;i<SpeakLen;i++){ //TTS语音播报信息 SendBuf1[i+45]=SpeakBuf[i]; } SendBuf1[10+34+SpeakLen+1]=(byte)0x55; //固定的抗干扰后缀 SendBuf1[10+34+SpeakLen+2]=(byte)0xaa; SendBuf1[10+34+SpeakLen+3]=(byte)0x66; SendBuf1[10+34+SpeakLen+4]=(byte)0x99; String SendInfStr=""; //这段代码显示发送的16进制报文,正式项目可以删除 for(int p=0;p<SendLen;p++){ String bytestr="00"+Integer.toHexString(SendBuf1[p] & 0xff); SendInfStr=SendInfStr+ bytestr.substring(bytestr.length() -2,bytestr.length())+" "; } DatagramSocket s1 = new DatagramSocket(); /** 1、建立udp socket端点 */ DatagramPacket dp1 = new DatagramPacket(SendBuf1, SendBuf1.length, InetAddress.getByName(RemIP), RemPor); s1.send(dp1); s1.close(); updatetrack("SendTo:"+RemIP+" Data:"+SendInfStr+"\n\n"); }catch (Exception e){ updatetrack(e.getMessage()+"\n\n"); } } public void SendOrder(View view){ String RemotIpStr=RemotIpV.getText().toString() ; String SendInfStr=InputstrV.getText().toString().trim() ; if (SendInfStr.length()<1){ Recv.append("请输入发送信息!\n\n"); return; }else{ if(ipCheck(RemotIpStr)) { new SendInfThread().start(); }else{ Recv.append("IP地址错误!\n\n"); } } } private class SendInfThread extends Thread { //向指定IP发送信息线程 @Override public void run() { String RemotIpStr=RemotIpV.getText().toString().trim() ; String SendInfStr=InputstrV.getText().toString().trim() ; try{ if (!RecsockIsRuning){InitRecSock();} String[] strArr = SendInfStr.split("\\ "); /*分割接收到的数据后再分析、处理、返回指令 */ byte[] SendBuf = new byte[strArr.length]; for(int p=0;p<strArr.length;p++) { SendBuf[p]=(byte)(Integer.parseInt(strArr[p],16)); } DatagramPacket dp1=new DatagramPacket(SendBuf,SendBuf.length,InetAddress.getByName(RemotIpStr),BROADCAST_PORT); Recsock.send(dp1); updatetrack("SendTo:"+RemotIpStr+" Data:"+SendInfStr+"\n\n"); }catch (Exception e){ updatetrack(e.getMessage()+"\n\n"); //Recsock.close(); //RecsockIsRuning=false; //InitRecSock(); } } } /*----------------------------------------------------------------------------------------------------------------------------取设备IP*/ public static String getIP(){ Enumeration<NetworkInterface> netInterfaces; ArrayList<String> IpAddList = new ArrayList<String>(); try { netInterfaces = NetworkInterface.getNetworkInterfaces(); // 拿到所有网卡 InetAddress ip; while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { ip = addresses.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(':') == -1) { IpAddList.add(ip.getHostAddress()); System.out.println((IpAddList.size()-1)+""+ " " +ni.getName() + " "+ ip.getHostAddress()); } } } } catch (Exception e) { } if(IpAddList.isEmpty()){ return "127.0.0.1"; }else{ return IpAddList.get(0); //如有多张网卡,请选择与设备相连的网卡,否则无法与设备正常通讯 } } /*---------------------------------------------------------------------------------------------------------判断字符串是不是一个日期时间格式*/ public static boolean IsDateTime(String inputstr) { Date date=null; SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try{ date=formatter.parse(inputstr); return true; }catch (Exception e) { return false; } } /*--------------------------------------------------------------------------------------------------------------------取电脑系统日期时间*/ public static String GetSysDT() { Date date=new Date(); SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String TimeStr=formatter.format(date); return TimeStr; } public static boolean ipCheck(String text) { //判断是否正确的IP地址 if (text != null && !text.isEmpty()) { // 定义正则表达式 String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; // 判断ip地址是否与正则表达式匹配 if (text.matches(regex)) { // 返回判断信息 return true; } else { // 返回判断信息 return false; } } return false; } public void onBackPressed(){ //按退出返回键 RecsockIsRuning=false; Recsock.close() ; finish() ; System.exit(0); } public void onclickclea(View vies){ Recv.setText(""); InputstrV.setText(""); } } |
|||
![]() | 上一篇:OUR_IDR.dll动态库使用说明 | ![]() | 下一篇:Android Studio开发安卓UDP网络读写器APP(一) |