diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..2e37988 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +fengshui_compass \ No newline at end of file diff --git a/app/src/main/java/com/motse/fengshui_compass/common/SerialPortManager.java b/app/src/main/java/com/motse/fengshui_compass/common/SerialPortManager.java index 8c2f6a6..7db2e8f 100644 --- a/app/src/main/java/com/motse/fengshui_compass/common/SerialPortManager.java +++ b/app/src/main/java/com/motse/fengshui_compass/common/SerialPortManager.java @@ -2,7 +2,6 @@ package com.motse.fengshui_compass.common; import android.os.Handler; import android.os.HandlerThread; -import android.os.Looper; import android.serialport.SerialPort; @@ -19,7 +18,7 @@ public class SerialPortManager { private OutputStream mOutputStream; private HandlerThread mWriteThread; private Scheduler mSendScheduler; - private Handler handler; +// private Handler handler; private static class InstanceHolder { public static SerialPortManager sManager = new SerialPortManager(); @@ -31,12 +30,9 @@ public class SerialPortManager { private SerialPort mSerialPort; - private SerialPortManager() { - this.handler = null; - } - private SerialPortManager(Handler dataReceiveHandle) { - this.handler = dataReceiveHandle; + private SerialPortManager() { + } /** @@ -45,8 +41,9 @@ public class SerialPortManager { * @param device * @return */ - public SerialPort open(Device device) { - return open(device.getPath(), device.getBaudrate()); + public SerialPort open(Device device, Handler dataHandle) { +// this.handler = dataHandle; + return open(device.getPath(), device.getBaudrate(), dataHandle); } /** @@ -56,20 +53,19 @@ public class SerialPortManager { * @param baudrateString * @return */ - public SerialPort open(String devicePath, String baudrateString) { + public SerialPort open(String devicePath, String baudrateString,Handler handler) { if (mSerialPort != null) { close(); } - try { File device = new File(devicePath); int baurate = Integer.parseInt(baudrateString); mSerialPort = new SerialPort(device, baurate); - if (handler != null) { - mReadThread = new SerialReadThread(mSerialPort.getInputStream(), handler); - } else { - mReadThread = new SerialReadThread(mSerialPort.getInputStream()); - } +// if (handler != null) { + mReadThread = new SerialReadThread(mSerialPort.getInputStream(), handler); +// } else { +// mReadThread = new SerialReadThread(mSerialPort.getInputStream()); +// } mReadThread.start(); mOutputStream = mSerialPort.getOutputStream(); @@ -119,7 +115,7 @@ public class SerialPortManager { * @param datas * @return */ - private void sendData(byte[] datas) throws Exception { + public void sendData(byte[] datas) throws Exception { mOutputStream.write(datas); } } diff --git a/app/src/main/java/com/motse/fengshui_compass/common/SerialReadThread.java b/app/src/main/java/com/motse/fengshui_compass/common/SerialReadThread.java index c71199d..8506c0d 100644 --- a/app/src/main/java/com/motse/fengshui_compass/common/SerialReadThread.java +++ b/app/src/main/java/com/motse/fengshui_compass/common/SerialReadThread.java @@ -1,6 +1,9 @@ package com.motse.fengshui_compass.common; +import android.icu.util.Measure; +import android.os.Bundle; import android.os.Handler; +import android.os.Message; import android.os.SystemClock; import com.licheedev.hwutils.ByteUtil; @@ -40,7 +43,7 @@ public class SerialReadThread extends Thread { if (available > 0) { size = mInputStream.read(received); if (size > 0) { - // TODO: onDataReceive(received, size); + onDataReceive(received, size); } } else { // 暂停一点时间,免得一直循环造成CPU占用率过高 @@ -68,6 +71,12 @@ public class SerialReadThread extends Thread { private void onDataReceive(byte[] received, int size) { // TODO: 2018/3/22 解决粘包、分包等 String hexStr = ByteUtil.bytes2HexStr(received, 0, size); + Bundle bundle = new Bundle(); + bundle.putString("hex_str", hexStr); + Message message = new Message(); + message.setData(bundle); + message.what = 1; + dataHandler.sendMessage(message); // LogManager.instance().post(new RecvMessage(hexStr)); } diff --git a/app/src/main/java/com/motse/fengshui_compass/ui/home/HomeViewModel.java b/app/src/main/java/com/motse/fengshui_compass/ui/home/HomeViewModel.java index 82a7839..2a8124e 100644 --- a/app/src/main/java/com/motse/fengshui_compass/ui/home/HomeViewModel.java +++ b/app/src/main/java/com/motse/fengshui_compass/ui/home/HomeViewModel.java @@ -9,16 +9,24 @@ import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import com.licheedev.hwutils.ByteUtil; +import com.motse.fengshui_compass.common.Device; import com.motse.fengshui_compass.common.RotateThread; +import com.motse.fengshui_compass.common.SerialPortManager; public class HomeViewModel extends ViewModel { private final MutableLiveData rotation; + private MutableLiveData isSerialOpen; RotateThread rotateThread; - private Handler handler; - private Handler serialThreadHandle; + private final Handler handler; + private final Handler serialThreadHandle; + private final SerialPortManager serialPortManager; public HomeViewModel() { + isSerialOpen = new MutableLiveData<>(); + isSerialOpen.setValue(false); + serialPortManager = SerialPortManager.instance(); handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(@NonNull Message msg) { @@ -27,6 +35,14 @@ public class HomeViewModel extends ViewModel { rotation.setValue(_rotation); } }; + serialThreadHandle = new Handler(Looper.getMainLooper()) { + @Override + public void handleMessage(@NonNull Message msg) { + super.handleMessage(msg); + String data = msg.getData().getString("hex_str"); + System.out.println(data); + } + }; rotation = new MutableLiveData<>(); rotation.setValue((float) 0); } @@ -36,6 +52,31 @@ public class HomeViewModel extends ViewModel { rotateThread.start(); } + public void initSerialPort() { + Device device = new Device(); + device.setBaudrate("115200"); + device.setPath("/dev/ttyS2"); + serialPortManager.open(device, serialThreadHandle); + } + + public void destroySerialPort() { + serialPortManager.close(); + } + + public void openUpLaser() { + sendCommand("7E01010100000000000000010D0A"); + } + + public void sendCommand(String command) { + byte[] bytes = ByteUtil.hexStr2bytes(command); + try { + serialPortManager.sendData(bytes); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + public void destroyThread() { rotateThread.interrupt(); rotateThread = null; diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 377da88..6cf8bfe 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -9,13 +9,28 @@ + + + \ No newline at end of file