diff --git a/app/build.gradle b/app/build.gradle index f8dc4c9..22aca66 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,12 +22,12 @@ android { signingConfigs { config { -// keyAlias 'key0' -// keyPassword '123456' -// storeFile file('test.jks') -// storePassword '123456' -// v1SigningEnabled true -// v2SigningEnabled true + keyAlias 'key0' + keyPassword '123456' + storeFile file('test.jks') + storePassword '123456' + v1SigningEnabled true + v2SigningEnabled true keyAlias 'test.keystore' keyPassword '666666' @@ -47,9 +47,9 @@ android { } debug { signingConfig signingConfigs.config - zipAlignEnabled true - minifyEnabled true - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' +// zipAlignEnabled true +// minifyEnabled true +// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } //使用uniapp时,需复制下面代码 diff --git a/zmt_module/build.gradle b/zmt_module/build.gradle index 991f69c..82cbb64 100644 --- a/zmt_module/build.gradle +++ b/zmt_module/build.gradle @@ -56,4 +56,6 @@ dependencies { compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar']) compileOnly 'com.alibaba:fastjson:1.1.46.android' + + } \ No newline at end of file diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java b/zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java new file mode 100644 index 0000000..884b780 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java @@ -0,0 +1,43 @@ +package io.dcloud.zmt_module; + +public class MyThread extends Thread { + String TAG = "zmt---"; + public void run(){ + System.out.println("MyThread thread running..."); + + TcpServer server = new TcpServer(); + server.initSocket((Integer.parseInt("16666"))); + + while (true){ + String message = server.receiveMessage(SocketConfig.UTF_8); + System.out.println(TAG+"接收消息:"+message); + + String send_msg = "收到消息了,你说的对!"; + + send_msg = SocketHelper.getMessageByReadLine(send_msg); + server.sendMessage(send_msg,SocketConfig.UTF_8); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + +// while (true){ +// String message = server.receiveMessage(SocketConfig.UTF_8); +// System.out.println(TAG+"接收消息:"+message); +// +// String send_msg = "101010"; +// +// send_msg = SocketHelper.getMessageByReadLine(send_msg); +// server.sendMessage(send_msg,SocketConfig.UTF_8); +// try { +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// +// } + } +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java b/zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java new file mode 100644 index 0000000..d22f614 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java @@ -0,0 +1,8 @@ +package io.dcloud.zmt_module; + +public class SocketConfig { + +// 设置编码 + public static final String UTF_8 = "UTF-8"; + public static final String GBK = "GBK"; +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java b/zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java new file mode 100644 index 0000000..e9545aa --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java @@ -0,0 +1,53 @@ +package io.dcloud.zmt_module; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +public class SocketHelper { + public static String getMessageByReadLine(String message) { + message = message + "\n"; + return message; + } + + public static byte[] toByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024 * 4]; + int n = 0; + while ((n = in.read(buffer)) != -1) { + out.write(buffer, 0, n); + } + return out.toByteArray(); + } + + public static byte[] toIntByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[100]; + + System.out.println("zmt-toIntByteArray--" + buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4] + buffer[5] + " " + Arrays.toString(buffer)); + int n = 0; + while ((n = in.read(buffer)) != -1) { + out.write(buffer, 0, n); + } + + + return out.toByteArray(); + } + + public static String byte2hex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + String tmp = null; + for (byte b : bytes) { + //将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制 + tmp = Integer.toHexString(0xFF & b); + if (tmp.length() == 1) { + tmp = "0" + tmp; + } + sb.append(tmp); + + } + return sb.toString(); + } + +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java b/zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java new file mode 100644 index 0000000..c5aff5d --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java @@ -0,0 +1,164 @@ +package io.dcloud.zmt_module; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.Charset; +import java.util.Arrays; + +public class TcpServer { + // 私有变量 + private String TAG = "zmt---"; + private ServerSocket mServerSocket; + // private DatagramSocket mServerSocket; + private Socket mServer; + + private OutputStream mOutStream; + private PrintWriter mPrinter; + + private InputStream mInputStream; + private BufferedInputStream mBufferedInputStream; + private InputStreamReader mInputStreamReader; + private BufferedReader mBufferedReader; + + /** + * 建立socket server + * @param port 端口号:0-65535 + */ + public void initSocket(int port){ + System.out.println(TAG+"initSocket"); + try{ + if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; } + + if (mServerSocket==null){ + mServerSocket = new ServerSocket(port); + mServerSocket.setReuseAddress(true); + +// mServerSocket.bind(new InetSocketAddress(port)); + InetAddress inetAddress = InetAddress.getLocalHost(); //本机地址 + System.out.println(TAG+"tcp server 建立完成:host name"+inetAddress.getHostName()+"\n ---host address:" + + inetAddress.getHostAddress()+" , address:"+inetAddress.getAddress()+" 端口:"+port); + } + + +// if(mServerSocket==null){ +// //自己IP收信 +//// IPAddress ip = IPAddress.Parse("192.168.1.96"); +// +//// String ip = "192.168.1.96"; +// InetAddress ip = InetAddress.getByName("192.168.1.96"); +// +// mServerSocket = new ServerSocket(port); +// mServerSocket.bind(new IPEndPoint(ip,port)); +// +// Socket mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); +// m_serverListenSocket.Bind(new IPEndPoint(ip, 10035)); +// m_serverListenSocket.Listen(100); +// } + }catch (IOException e){ + e.printStackTrace(); + } + } + + // 接收消息 + public String receiveMessage(String charsetName){ + String receiveData=null; + String readLine=null; + +// if(mServerSocket==null){ +// System.out.println(TAG+"Socket 未启动"); +// return null; +// } + System.out.println(TAG+"tcp server receiveMessage ing..."); + try{ +// 获取输入信息 + mServer = mServerSocket.accept(); + mInputStream = mServer.getInputStream(); + + byte[] data = SocketHelper.toByteArray(mInputStream); + +// String _rs = new String(data, "gbk"); + +// System.out.println(TAG+"二进制读取结果:"+data+" - "+_rs +" - "+SocketHelper.toIntByteArray(mInputStream) + Arrays.toString(data)) ; + + System.out.println(TAG+SocketHelper.byte2hex(data)); + receiveData = SocketHelper.byte2hex(data); +// 读取输入流 +// charsetName = null; +// if(charsetName!=null&&charsetName!=""){ +// mInputStreamReader = new InputStreamReader(mInputStream, Charset.forName(charsetName)); +// }else{ +// mInputStreamReader = new InputStreamReader(mInputStream); +// } + +// mBufferedReader = new BufferedReader(mInputStreamReader); +// readLine = mBufferedReader.readLine(); +// while (readLine!=null&&readLine!=""){ +// System.out.println(TAG+"读取结果:"+readLine); +// break; +// } + System.out.println(TAG+"接收结束,数据:"+receiveData); + + } catch (IOException e) { + e.printStackTrace(); + } + return receiveData; + + } + + // 发送消息 + public void sendMessage(String message,String charsetName){ + if(mServer != null){ + System.out.println(TAG+"sendMessage ing..."); + + try { + mOutStream = mServer.getOutputStream(); + mPrinter = new PrintWriter(new OutputStreamWriter(mOutStream,Charset.forName(charsetName))); + mPrinter.println(message); + mPrinter.flush(); + + System.out.println(TAG+"发送成功 :"+message); + } catch (IOException e) { + System.out.println(TAG+"tcp server sendMessage err :"+message); + e.printStackTrace(); + } + + } + } + + // 关闭连接 + public void close(){ + try { + if (mPrinter!=null){ + mPrinter.close(); + } + if (mOutStream!=null){ + mOutStream.close(); + } + if (mInputStream!=null){ + mInputStream.close(); + } + if (mInputStreamReader!=null){ + mInputStreamReader.close(); + } + if (mBufferedReader!=null){ + mBufferedReader.close(); + } + if (mServer!=null){ + mServer.close(); + } + System.out.println(TAG+"关闭tcp server <------"); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java b/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java index 218ffb8..96ba4d1 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java @@ -14,6 +14,10 @@ public class Zmt_AppProxy implements UniAppHookProxy { Log.e(TAG, "testAsyncFunc--zmt:onCreate"); //当前uni应用进程回调 仅触发一次 多进程不会触发 //可通过UniSDKEngine注册UniModule或者UniComponent + + + MyThread mt = new MyThread(); + mt.run(); } @Override diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java index aab5cfa..f39162e 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java @@ -10,19 +10,69 @@ import android.util.Log; public class zmtClass extends UniModule { - String TAG = "zmt"; + String TAG = "zmt---"; //run ui thread @UniJSMethod(uiThread = true) - public void testAsyncFunc(JSONObject options, UniJSCallback callback) { + public void testAsyncFunc(JSONObject options, final UniJSCallback callback) { Log.e(TAG, "testAsyncFunc--zmt:"+options); + + //启动线程 +// MyThread mt = new MyThread(); +// mt.run(); + if(callback != null) { JSONObject data = new JSONObject(); - data.put("code", "success-zmt async"); + data.put("code", "已开启服务,等待接收数据"); callback.invoke(data); } + + + // Android 4.0 之后不能在主线程中请求HTTP请求 + new Thread(new Runnable(){ + @Override + public void run() { + System.out.println("MyThread thread running..."); + + TcpServer server = new TcpServer(); + server.initSocket((Integer.parseInt("16666"))); + + while (true){ + String message = server.receiveMessage(SocketConfig.UTF_8); + System.out.println(TAG+"接收消息:"+message); + + JSONObject data = new JSONObject(); + data.put("code", message); + callback.invoke(data); + + String send_msg = "101010"; + + send_msg = SocketHelper.getMessageByReadLine(send_msg); + server.sendMessage(send_msg,SocketConfig.UTF_8); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + } + }).start(); + } +// @UniJSMethod(uiThread = true) +// public void testAsyncFunc(JSONObject options, UniJSCallback callback) { +// Log.e(TAG, "testAsyncFunc--zmt:"+options); +// +// if(callback != null) { +// JSONObject data = new JSONObject(); +// data.put("code", "success-zmt async"); +// callback.invoke(data); +// } +// } + //run JS thread @UniJSMethod(uiThread = false) public JSONObject testSyncFunc(){