美文网首页
2018-03-30

2018-03-30

作者: ProZoom | 来源:发表于2018-03-30 13:21 被阅读13次
    package com.jokey.wisdomaldetection.service;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    import com.jokey.wisdomaldetection.utils.IConstant;
    import com.jokey.wisdomaldetection.utils.LogUtil;
    import com.jokey.wisdomaldetection.utils.TimeUtil;
    import com.jokey.wisdomaldetection.utils.ToastUtil;
    
    import android_serialport_api.SerialPortManager;
    
    
    /**
     * Created by Administrator on 2018/1/16.
     */
    
    public class MyService extends Service {
    
        private MyTimeThread myTimeThread;
        private ReceiveDataThread receiveDataThread;
        private byte[] buffer;
        private static final String open = "D&C0004010D";
        private static final String close = "D&C0004010E";
    
    
        @Override
        public void onCreate() {
            showTime();
            if (!SerialPortManager.getInstance().openSerialPort()) {
                ToastUtil.showToast(this, "OpenSerialPort--Failed!");
            } else {
                ToastUtil.showToast(this, "OpenSerialPort--Success!");
                startReceive();
            }
            super.onCreate();
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
    
        @Override
        public void onDestroy() {
            stopTime();
            stopReceive();
            SerialPortManager.getInstance().write(close.getBytes());
            SerialPortManager.getInstance().closeSerialPort();
            super.onDestroy();
        }
    
    
        private void showTime() {
            if (myTimeThread == null) {
                myTimeThread = new MyTimeThread();
                myTimeThread.setContinue(true);
                myTimeThread.start();
            }
        }
    
        private void stopTime() {
            if (myTimeThread != null) {
                myTimeThread.setContinue(false);
                myTimeThread = null;
            }
        }
    
        private class MyTimeThread extends Thread {
            private boolean isContinue;
    
            public void setContinue(boolean aContinue) {
                isContinue = aContinue;
            }
    
            @Override
            public void run() {
                while (isContinue) {
                    try {
                        Thread.sleep(999);
                        Intent intent = new Intent();
                        intent.setAction(IConstant.ACTION_TIME);
                        String time = TimeUtil.getFormatted(IConstant.TIME_FORMAT, System.currentTimeMillis());
                        intent.putExtra(IConstant.TIME_NOW, time);
                        sendBroadcast(intent);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
        }
    
    
        private void startReceive() {
            if (receiveDataThread == null) {
                receiveDataThread = new ReceiveDataThread();
                receiveDataThread.setContinue(true);
                receiveDataThread.start();
            }
        }
    
        private void stopReceive() {
            if (receiveDataThread != null) {
                receiveDataThread.setContinue(false);
                receiveDataThread = null;
            }
        }
    
        private class ReceiveDataThread extends Thread {
            private boolean Continue;
    
            public void setContinue(boolean aContinue) {
                Continue = aContinue;
            }
    
            @Override
            public void run() {
    
                SerialPortManager.getInstance().write(open.getBytes());
    
                while (Continue) {
                    //Log.i("jokeyservices","读取数据!!!!!!!!!");
    
                    buffer = new byte[256];
    
                    int length = SerialPortManager.getInstance().read(buffer, 100, 50);
    
    
                    Log.i("jokeyservices", "length=" + length + "----buffer[1]=" + buffer[1]);
    
                    if (length == 3 && buffer[1] != 0x4f) {
    
                        /*synchronized (this){
                            isReceiveData=true;
                        }*/
    
                        Log.i("jokeyservices", "串口数据读取成功,处理中....");
    
                        float vol = ((buffer[1] & 0xff) * 256 + (buffer[2] & 0xff)) / 10f;
    
                        float voltage = (Math.round((vol) * 10f)) / 10f;
    
                        LogUtil.d("jokeyservices", "vol: " + vol + "----voltage: " + voltage);
    
    
                        if (vol == 0) {
                            Intent intent = new Intent();
                            intent.setAction(IConstant.ACTION_VOLTAGE_DATA_ISZERO);
                            intent.putExtra(IConstant.VOLTAGE_VALUE_ISZERO, voltage);
                            sendBroadcast(intent);
                        }
    
                        Intent intent = new Intent();
                        intent.setAction(IConstant.ACTION_VOLTAGE_DATA);
                        intent.putExtra(IConstant.VOLTAGE_VALUE, voltage);
                        sendBroadcast(intent);
    
    
                    } else {
    
                    /*    synchronized (this){
                            isReceiveData=false;
    
                        }*/
                    }
                    SerialPortManager.getInstance().clearBuffer();
                }
    
            }
        }
    
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:2018-03-30

          本文链接:https://www.haomeiwen.com/subject/ytqicftx.html