|
@@ -49,7 +49,16 @@ import java.util.Locale;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
-import static android.os.Build.VERSION_CODES.M;
|
|
|
+
|
|
|
+import android.util.Log;
|
|
|
+import android.widget.EditText;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.ServerSocket;
|
|
|
+import java.net.Socket;
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
|
|
//===============================================================//
|
|
|
//=================================================================//
|
|
@@ -71,7 +80,7 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
private UsbEndpoint mUsbEndpointIn;
|
|
|
private boolean mToggle = true;
|
|
|
private boolean isDetached = false;
|
|
|
- private byte[] mBytes = new byte[64];
|
|
|
+ private byte[] mBytes = new byte[1024];
|
|
|
private boolean isReceiverMessage = true;
|
|
|
private boolean isNeedFindDevice = true;
|
|
|
|
|
@@ -88,6 +97,19 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
|
|
|
public static List<Activity> activityList = new LinkedList();
|
|
|
|
|
|
+ private Button bt_tcpstart, bt_tcpsend1;
|
|
|
+ private EditText et_text, et_text1;
|
|
|
+ private final static String TAG = "Main2Activity-------->";
|
|
|
+ private final static String addreeip = "192.168.3.80";
|
|
|
+ private final static int port = 5555;
|
|
|
+ private String app_text, receData;
|
|
|
+ private Socket socket = null;
|
|
|
+ private ServerSocket server = new ServerSocket(5168);
|
|
|
+
|
|
|
+ // 获取输出流与输入流
|
|
|
+ private OutputStream outputStream = null;
|
|
|
+ private InputStream inputStream = null;
|
|
|
+
|
|
|
Handler mHandler = new Handler() {
|
|
|
@Override
|
|
|
public void handleMessage(Message msg) {
|
|
@@ -118,6 +140,9 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ public GT9MainActivity() throws IOException {
|
|
|
+ }
|
|
|
+
|
|
|
//==========================================================================================//
|
|
|
|
|
|
@Override
|
|
@@ -199,6 +224,18 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+ bt_tcpstart = findViewById(R.id.bt_tcpstart);
|
|
|
+
|
|
|
+ bt_tcpsend1 = findViewById(R.id.bt_tcpsend1);
|
|
|
+
|
|
|
+ et_text = findViewById(R.id.et_text);
|
|
|
+
|
|
|
+ et_text1 = findViewById(R.id.et_text1);
|
|
|
+
|
|
|
+ bt_tcpstart.setOnClickListener(this);
|
|
|
+
|
|
|
+ bt_tcpsend1.setOnClickListener(this);
|
|
|
+
|
|
|
init();//
|
|
|
}
|
|
|
|
|
@@ -353,15 +390,179 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
btnDataAnalysis = (Button) findViewById(R.id.DataAnalysis);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public void onClick(final View v) {
|
|
|
switch (v.getId()) {
|
|
|
case R.id.Exit: //Exit
|
|
|
exit();
|
|
|
break;
|
|
|
+
|
|
|
+ case R.id.bt_tcpstart:
|
|
|
+
|
|
|
+ String btContent = bt_tcpstart.getText().toString();
|
|
|
+
|
|
|
+ if (btContent.equals("TCP启动")) {
|
|
|
+
|
|
|
+ bt_tcpstart.setText("TCP停止");
|
|
|
+
|
|
|
+ //bt_tcpstart.setBackgroundColor(getResources().getColor(R.color.colorAccent1));
|
|
|
+
|
|
|
+ new Thread(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+
|
|
|
+ public void run() {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+
|
|
|
+ // server将一直等待连接的到来
|
|
|
+ System.out.println("server将一直等待连接的到来");
|
|
|
+
|
|
|
+ socket = server.accept();
|
|
|
+ // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
|
|
|
+ inputStream = socket.getInputStream();
|
|
|
+ byte[] headbytes = new byte[8];
|
|
|
+ byte[] databytes = new byte[512];
|
|
|
+ int len;
|
|
|
+ int datalen = 0;
|
|
|
+ int framelen = 0;
|
|
|
+ int frameid = 0;
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+
|
|
|
+ //for(int i = 0; i < 128; i++)
|
|
|
+ //{
|
|
|
+ // mBytes[i] = 0 ;
|
|
|
+ //}
|
|
|
+
|
|
|
+ //只有当客户端关闭它的输出流的时候,服务端才能取得结尾的-1
|
|
|
+ while ((len = inputStream.read(headbytes)) != -1) {
|
|
|
+
|
|
|
+ //先判断头部字节是否正确
|
|
|
+ if ( headbytes[0] == (byte)0x8d
|
|
|
+ && headbytes[1] == 0x7c
|
|
|
+ && headbytes[2] == 0x6b
|
|
|
+ && headbytes[3] == 0x5a )
|
|
|
+ {
|
|
|
+ framelen = headbytes[4] + (int)(headbytes[5] * 256); //低位在前
|
|
|
+ frameid = headbytes[6] + (int)(headbytes[7] * 256);
|
|
|
+
|
|
|
+ byte[] bytes = new byte[framelen];
|
|
|
+
|
|
|
+ len = inputStream.read(bytes); //根据头部数据读取对应长度的数据
|
|
|
+ datalen = bytes[5]+ (int)(bytes[4] * 256); //高位在前
|
|
|
+
|
|
|
+ GetUSBData(bytes[1],bytes[2],bytes[3],databytes,datalen); //根据命令去获取USB数据
|
|
|
+
|
|
|
+ //bytes[4] = 24;
|
|
|
+ //bytes[5] = 0;
|
|
|
+
|
|
|
+
|
|
|
+ for(int i = 0; i < 8 + datalen; i++)
|
|
|
+ {
|
|
|
+ if (i < 8) //把前面8个字节的头部补上
|
|
|
+ {
|
|
|
+ mBytes[i] = headbytes[i];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+
|
|
|
+ mBytes[i] = databytes[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ outputStream = socket.getOutputStream();
|
|
|
+ //outputStream.write("Hello Client,I get the message.".getBytes("UTF-8"));
|
|
|
+ outputStream.write(mBytes);
|
|
|
+ }
|
|
|
+ // 注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-
|
|
|
+ /*
|
|
|
+ for(int i=0; i<len; i++)
|
|
|
+ {
|
|
|
+ sb.append(Integer.toHexString(bytes[i]));
|
|
|
+ sb.append(',');
|
|
|
+ }
|
|
|
+ sb.append('\n');
|
|
|
+ System.out.println(sb);
|
|
|
+ */
|
|
|
+ }
|
|
|
+ // System.out.println("get message from client: " + sb);
|
|
|
+
|
|
|
+ //outputStream = socket.getOutputStream();
|
|
|
+ //outputStream.write("Hello Client,I get the message.".getBytes("UTF-8"));
|
|
|
+
|
|
|
+ inputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ socket.close();
|
|
|
+ server.close();
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ Log.i(TAG, "连接失败!!!" + e.toString());
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }).start();
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ bt_tcpstart.setText("TCP启动");
|
|
|
+ //bt_tcpstart.setBackgroundColor(getResources().getColor(R.color.colorAccent2));
|
|
|
+
|
|
|
+ try {
|
|
|
+ //socket.close();
|
|
|
+ //inputStream.close();
|
|
|
+ //outputStream.close();
|
|
|
+ //socket.close();
|
|
|
+ server.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case R.id.bt_tcpsend1:
|
|
|
+ tcp_start();
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void tcp_start() {
|
|
|
+
|
|
|
+ new Thread(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+
|
|
|
+ public void run() {
|
|
|
+ app_text = et_text.getText().toString().trim();
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (app_text.equals("")) {
|
|
|
+ Log.i(TAG, "输入不能为空");
|
|
|
+ } else {
|
|
|
+ //注意charset.forName 字符编码,utf-8中文。。。。。
|
|
|
+ if(outputStream !=null){
|
|
|
+ byte[] sendData = app_text.getBytes(Charset.forName("ASCII"));
|
|
|
+ outputStream.write(sendData, 0, sendData.length);
|
|
|
+ outputStream.flush();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* ???? , ??????????
|
|
|
*/
|
|
@@ -514,7 +715,17 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
@Override
|
|
|
protected void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
+ /*
|
|
|
+ try {
|
|
|
+
|
|
|
+ socket.close();
|
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ }
|
|
|
+ */
|
|
|
if (mUsbDeviceConnection != null) {
|
|
|
mUsbDeviceConnection.releaseInterface(mUsbInterface);
|
|
|
mUsbDeviceConnection.close();
|
|
@@ -569,6 +780,105 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
|
|
|
return "0.0.0.0";
|
|
|
}
|
|
|
+
|
|
|
+ private byte iicByteData[]={
|
|
|
+ //0 1 2 3 4 5 6
|
|
|
+ 0x03,0x00,0x0f,0x00,0x00,0x00,0x01,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+
|
|
|
+ };
|
|
|
+ private byte iicWriteData[]={
|
|
|
+ //0 1 2 3 4 5 6
|
|
|
+ 0x03,0x00,0x0f,0x00,0x00,0x00,0x39,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
+
|
|
|
+ };
|
|
|
+ //发送命令,使得S818可以通过IIC向GT9的固定地址读取任意bytes的数据
|
|
|
+ private boolean GetUSBData(byte cmd,byte addressHigh,byte addressLow,byte[] wBuffer,int length) {
|
|
|
+
|
|
|
+ int i;
|
|
|
+
|
|
|
+ iicByteData[1] = 0x00; //
|
|
|
+ iicByteData[2] = cmd; //get report id
|
|
|
+ iicByteData[3] = addressHigh;
|
|
|
+ iicByteData[4] = addressLow;
|
|
|
+ iicByteData[5] = (byte)(length >> 8); //lengthHigh
|
|
|
+ iicByteData[6] = (byte)(length & 0x00FF); //lengthLow
|
|
|
+
|
|
|
+ for (int j = 7; j < 64; j++) {
|
|
|
+ iicByteData[j] = 0x00;
|
|
|
+ }
|
|
|
+
|
|
|
+ //int address1 = ((addressHigh&0xff)<<8);
|
|
|
+ //int address2 = addressLow&0xff;
|
|
|
+ //int address = address1|address2;
|
|
|
+
|
|
|
+ int sendTimes = length/63 + 1; //预计是向下取整,所以+1
|
|
|
+
|
|
|
+ for (int k = 0; k < sendTimes; k++) {
|
|
|
+ int finalOffset = k*63; //每次只能发57bytes数据
|
|
|
+ int offset = k*64;
|
|
|
+
|
|
|
+ if(length - (finalOffset ) < 63) {
|
|
|
+
|
|
|
+ iicByteData[6] = (byte) (length - (finalOffset)); //length
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ iicByteData[6] = 63;
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送读取命令
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
|
|
|
+ if (i != 0x40)
|
|
|
+ return false; //传输失败
|
|
|
+ // SystemClock.sleep(1);
|
|
|
+
|
|
|
+ isReceiverMessage = false; //先关闭循环接收信息
|
|
|
+
|
|
|
+ //接收数据
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, offset, 0x40, 3000);
|
|
|
+
|
|
|
+ if (i != 0x40) {
|
|
|
+ // mError.setText("iicWrite Failed");
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //address = address + 63;
|
|
|
+ //iicByteData[3] = (byte)(address>>8);
|
|
|
+ //iicByteData[4] = (byte)address;
|
|
|
+ }
|
|
|
+
|
|
|
+ //将report id从数组中去除
|
|
|
+ //byte[] temperBuffer =new byte[length];
|
|
|
+ /*
|
|
|
+ if(true) {
|
|
|
+ int count1 = 0;
|
|
|
+ for (int k = 0; k < length; k++) {
|
|
|
+ if (k % 63 == 0) {
|
|
|
+ count1++;
|
|
|
+ }
|
|
|
+
|
|
|
+ wBuffer[k] = wBuffer[k + count1];
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ */
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
class OpenDevicesReceiver extends BroadcastReceiver {
|