同时打开两个串口设备
package com.cnd.gesturetest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import com.aill.androidserialport.SerialPort;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;import java.util.Timer;import java.util.TimerTask;import android.content.IntentFilter;public class MainActivity extends Activity { private static String TAG = "MainActivity"; private static final char[] HEXES = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private SerialPort serialPort,serialPort1 = null; private InputStream inputStream,inputStream1; private OutputStream outputStream,outputStream1; private byte[] buffer; private byte[] buffer1; private IntentFilter mIntentFilter_code = null; private DiyBroadcastReceiver diyBroadcastReceiver; private String result1; private final Timer timer = new Timer(); private TimerTask task; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what){ case 1: Log.i(TAG, "十秒一次"); sendGasBroadcast(result1); break; default: break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { setView(); } catch (IOException e) { e.printStackTrace(); } if (task != null){ task.cancel(); //将原任务从队列中移除 } task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; timer.schedule(task, 2000, 10000); diyBroadcastReceiver = new DiyBroadcastReceiver(); mIntentFilter_code = new IntentFilter("com.cnd.usbprovider.gesture.result"); registerReceiver(diyBroadcastReceiver, mIntentFilter_code); test(); } private void test() { Log.i(TAG, "just test:"); } @Override protected void onPause() { super.onPause(); task.cancel(); unregisterReceiver(diyBroadcastReceiver); } private void sendGasBroadcast(String code){ Intent intentGas = new Intent("com.cnd.usbprovider.gas.result"); intentGas.putExtra("gas",code); sendBroadcast(intentGas); Log.i(TAG, "发送气体广播"); } private void setView() throws IOException { Log.i(TAG, "setView: "); serialPort = new SerialPort(new File("/dev/ttyUSB0"), 9600, 0); serialPort1 = new SerialPort(new File("/dev/ttyUSB1"), 9600, 0); new ReadThread("read").start(); new ReadThread1("read1").start(); } class ReadThread extends Thread { public ReadThread(String name) { super(name); } @Override public void run() { super.run(); int ret = 0; do{ //Log.i(TAG, "ReadThread: 开始读"); try { Thread.sleep(500); inputStream = serialPort.getInputStream(); ret = inputStream.available(); if (ret > 0) { buffer = new byte[ret]; inputStream.read(buffer); String result = bytes2Hex(buffer); Log.i(TAG, "手势:" result); Intent intentScan = new Intent("com.cnd.usbprovider.gesture.result"); switch (result.substring(2, 4)) { case "08": Log.i(TAG, "悬停"); intentScan.putExtra("gesture", "08"); sendBroadcast(intentScan); break; case "01": Log.i(TAG, "从左至右"); intentScan.putExtra("gesture", "01"); sendBroadcast(intentScan); break; case "02": Log.i(TAG, "从右至左"); intentScan.putExtra("gesture", "02"); sendBroadcast(intentScan); break; } } } catch (Exception e) { e.printStackTrace(); } }while (true); } } class ReadThread1 extends Thread { public ReadThread1(String name) { super(name); } @Override public void run() { super.run(); int ret = 0; do{ //Log.i(TAG, "ReadThread1: 开始读"); try { Thread.sleep(400); inputStream1 = serialPort1.getInputStream(); ret = inputStream1.available(); if (ret > 0) { buffer1 = new byte[ret]; inputStream1.read(buffer1); result1 = bytes2Hex(buffer1); Log.i(TAG, "烟雾:" result1); } } catch (Exception e) { e.printStackTrace(); } }while (true); } } /** * byte数组 转换成 16进制小写字符串 */ public static String bytes2Hex(byte[] bytes) { if (bytes == null || bytes.length == 0) { return null; } StringBuilder hex = new StringBuilder(); for (byte b : bytes) { hex.append(HEXES[(b >> 4) & 0x0F]); hex.append(HEXES[b & 0x0F]); } return hex.toString(); } public static int covert(String content){ int number=0; String [] HighLetter = {"A","B","C","D","E","F"}; Map<String,Integer> map = new HashMap<>(); for(int i = 0;i <= 9;i ){ map.put(i "",i); } for(int j= 10;j<HighLetter.length 10;j ){ map.put(HighLetter[j-10],j); } String[]str = new String[content.length()]; for(int i = 0; i < str.length; i ){ str[i] = content.substring(i,i 1); } for(int i = 0; i < str.length; i ){ number = map.get(str[i])*Math.pow(16,str.length-1-i); } return number; }}
评论