diff --git a/communication/src/main/java/cn/dxl/com/ComBridgeAdapter.java b/communication/src/main/java/cn/dxl/com/ComBridgeAdapter.java index eda50d5c..5948b7e5 100644 --- a/communication/src/main/java/cn/dxl/com/ComBridgeAdapter.java +++ b/communication/src/main/java/cn/dxl/com/ComBridgeAdapter.java @@ -1,159 +1,73 @@ package cn.dxl.com; +import android.net.LocalServerSocket; import android.util.Log; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.io.Serializable; -import java.nio.ByteBuffer; /** * @author DXL * Created by DXL on 2017/8/21. - * 通信控制映射类! + * 通信控制映射类,直接实现进程间通信! */ public final class ComBridgeAdapter implements Serializable { /* * Warning!!! - * For standardization and security, - * you should not change any functions and variables defined by the native keyword, - * or you may have a big problem! * If your implementation does not need to be mapped to the c/c++ (only Java exists), please do not use this tool class! * * 警告!!! - * 为了规范性和安全性,你不应当更改任何native关键字定义的函数与变量,否则可能会出现大问题! * 如果您的实现不需要映射到底层(只存在Java),请不要使用此工具类! * */ - // Buffer - private static ByteBuffer recv_buffer; - private static ByteBuffer send_buffer; - // Communication interface, implement from development. - private static Communication mCommunication = null; - //Buffer size! - private final static int BUFFER_SIZE = 1024 * 1024; + // The namespace of the LocalServerSocket + public static final String NAMESPACE = "CN.DXL.ComBridgeAdapter.2020_0413"; + // 本地套接字服务! + private static LocalServerSocket serverSocket; + // 单例! + private static ComBridgeAdapter instance; + // 输入流! + public InputStream mInputStream; + // 输出流! + public OutputStream mOutputStream; /* * 静态块初始化区域 */ static { - System.loadLibrary("commapping"); - //数据缓冲区长度,16384字节 - send_buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); - recv_buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); - //初始化缓冲区 - initBuffer(send_buffer, recv_buffer); + instance = new ComBridgeAdapter(); + try { + serverSocket = new LocalServerSocket(NAMESPACE); + } catch (IOException e) { + e.printStackTrace(); + } } // No instantiation is required private ComBridgeAdapter() { /*This constructor can't using*/ } - /** - * init the communication, use {@link DeviceChecker } - * - * @param communication base communication implement! - * @return init communication, true is successful or false is error!sss - */ - public static void initCom(Communication communication) { - if (communication == null) throw new RuntimeException("The communication impl is null."); - //初始化通信实现 - mCommunication = communication; + public static ComBridgeAdapter getInstance() { + return instance; } - /** - * write byte from {@link #send_buffer} to devices! - * - * @return write result, finish byte count. - * @throws IOException IOException in write error. - */ - private static int write() throws IOException { - logCommunicationNonnull(); - if (mCommunication == null) return -1; - //加上偏移值 - //try { - return mCommunication.write(send_buffer.array(), - send_buffer.arrayOffset(), syncLength() + - send_buffer.arrayOffset(), syncTimeout()); - //} catch (IOException e) { - //e.printStackTrace(); - //} + public InputStream getInputStream() { + return mInputStream; } - /** - * read byte from devices to{@link #recv_buffer}! - * - * @return read result, all byte count. - * @throws IOException IOException in read error. - */ - private static int read() throws IOException { - logCommunicationNonnull(); - if (mCommunication == null) return -1; - //try { - return mCommunication.read(recv_buffer.array(), - recv_buffer.arrayOffset(), syncLength() + - recv_buffer.arrayOffset(), syncTimeout()); - //} catch (IOException e) { - //e.printStackTrace(); - //} + public void setInputStream(InputStream mInputStream) { + this.mInputStream = mInputStream; } - /** - * flush data, clear buffer! - */ - private static void flush() throws IOException { - logCommunicationNonnull(); - if (mCommunication == null) return; - //try { - mCommunication.flush(); - //} catch (IOException e) { - //e.printStackTrace(); - //} + public OutputStream getOutputStream() { + return mOutputStream; } - /** - * close devices, clear buffer and break communication! - * TODO don't close, because some communication can't close() in c layer. - */ - private static void close() throws IOException { - //try { - //communication.close(); - //} catch (IOException e) { - //e.printStackTrace(); - //} + public void setOutputStream(OutputStream mOutputStream) { + this.mOutputStream = mOutputStream; } - private static void logCommunicationNonnull() { - if (mCommunication == null) { - Log.e("Com", "Error: the communication interface implement cannot null!"); - Log.e("Com", "错误: Communication接口的实现不可以为空,否则数据传输将不起作用!"); - } - } - /* - * author DXL - * some communication native function - * you can't direct invoke, because that are auto invoke in inner static layer! - */ - - /** - * inner invoking, init buffer mapping from java to c layer! - * - * @param recv_buffer read data buffer, the data is from java to native layer! - * @param send_buffer write data buffer, the data is from native layer to java! - */ - private static native int initBuffer(ByteBuffer send_buffer, ByteBuffer recv_buffer); - - /** - * get length from native layer! - * - * @return read or write length! - */ - private static native int syncLength(); - - /** - * get timeout from native layer! - * - * @return read or write timeout! - */ - private static native int syncTimeout(); } diff --git a/communication/src/main/java/cn/rrg/com/AbsBluetoothSpp.java b/communication/src/main/java/cn/rrg/com/AbsBluetoothSpp.java index fd4b3033..d9277816 100644 --- a/communication/src/main/java/cn/rrg/com/AbsBluetoothSpp.java +++ b/communication/src/main/java/cn/rrg/com/AbsBluetoothSpp.java @@ -37,11 +37,10 @@ public abstract class AbsBluetoothSpp implements DriverInterface