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
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...