Commit 24f2fafb authored by pengguangpu's avatar pengguangpu

完美解决商米称重问题

parent 98598a2e
......@@ -52,6 +52,7 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
findViewById(R.id.btnGravity).setOnClickListener(this);
findViewById(R.id.btnRelease).setOnClickListener(this);
findViewById(R.id.btnReadNow).setOnClickListener(this);
findViewById(R.id.ivBack).setOnClickListener(this);
}
@Override
......@@ -107,14 +108,19 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
break;
case R.id.btnRelease:
if (WeighingManager.getInstance().isConnected()) {
WeighingManager.getInstance().getWeighing().release();
if (WeighingManager.getInstance().getWeighing().release() == true) {
tvResultNow.setText("断开连接成功!");
}
}
break;
case R.id.btnReadNow:
if (WeighingManager.getInstance().isConnected()) {
tvResultNow.setText(WeighingManager.getInstance().getWeighing().getWeight()+"");
tvResultNow.setText(WeighingManager.getInstance().getWeighing().getWeight() + "");
}
break;
case R.id.ivBack:
finish();
break;
}
if (WeighingManager.getInstance().isConnected() == false) {
tvResult.setText("当前没有称重设备,试试初始化!");
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
......@@ -9,11 +10,18 @@
android:layout_height="88dp"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/ivBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_previous" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="称重开放平台"
android:gravity="right"
android:text="称重开放组件"
android:textColor="#ffffff"
android:textSize="36sp" />
</android.support.v7.widget.ActionMenuView>
......
......@@ -94,12 +94,16 @@ public class HisenseWeighing extends BaseWeighing {
@Override
public boolean release() {
this.context.unbindService(serviceConnection);
if (context != null && serviceConnection != null) {
this.context.unbindService(serviceConnection);
}
try {
uartScale.close();
} catch (Exception e) {
e.printStackTrace();
}
context = null;
serviceConnection = null;
curWeight = 0;
tare = 0;
weighingCallback = null;
......
......@@ -8,6 +8,7 @@ import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.miya.weighing.utils.D;
import com.sunmi.electronicscaleservice.ScaleCallback;
import com.sunmi.electronicscaleservice.ScaleService;
......@@ -30,6 +31,33 @@ public class SunmiWeighing extends BaseWeighing {
*/
ScaleService scaleService;
/**
* 称重回调监听
*/
ScaleCallback.Stub stub = new ScaleCallback.Stub() {
@Override
public void getData(int net, int tare, int status) throws RemoteException {
D.i(TAG, "商米称重硬件==>原始重量:" + net + ";皮重:" + tare + ";状态:" + status);
if (weighingCallback != null) {
String stateStr = Integer.toBinaryString(status);
boolean isStable;
if (stateStr.substring(stateStr.length() - 1).equals("1")) {
// 稳定
isStable = true;
if (net < 0) {
//程序置零一次,可能会失败
resetZero();
}
} else {
// 动态值
isStable = false;
}
curWeight = net;
weighingCallback.returnWeight(net, isStable);
}
}
};
@Override
public boolean init(Context context) {
this.context = context;
......@@ -65,41 +93,43 @@ public class SunmiWeighing extends BaseWeighing {
if (scaleService != null) {
//设置重量回调
try {
scaleService.getData(new ScaleCallback.Stub() {
@Override
public void getData(int net, int tare, int status) throws RemoteException {
Log.i(TAG, "商米称重硬件==>原始重量:" + net + ";皮重:" + tare + ";状态:" + status);
if (weighingCallback != null) {
String stateStr = Integer.toBinaryString(status);
boolean isStable;
if (stateStr.substring(stateStr.length() - 1).equals("1")) {
// 稳定
isStable = true;
if (net < 0) {
//程序置零一次,可能会失败
resetZero();
}
} else {
// 动态值
isStable = false;
}
curWeight = net;
weighingCallback.returnWeight(net, isStable);
}
}
});
scaleService.getData(stub);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "称重服务初始化失败!");
D.i(TAG, "称重服务初始化失败!");
}
} else {
Log.i(TAG, "防损称连接失败!");
D.i(TAG, "防损称连接失败!");
}
}
@Override
public void onBindingDied(ComponentName name) {
D.i(TAG, "解绑成功 ComponentName==>" + name);
}
@Override
public void onServiceDisconnected(ComponentName name) {
scaleService = null;
}
};
@Override
public boolean release() {
if (context != null && serviceConnection != null) {
this.context.unbindService(serviceConnection);
}
context = null;
serviceConnection = null;
if (scaleService != null) {
try {
scaleService.cancelGetData();
} catch (RemoteException e) {
e.printStackTrace();
}
}
scaleService = null;
this.weighingCallback = null;
return true;
}
}
......@@ -76,10 +76,6 @@ public class WeighingManager {
* @return
*/
public boolean init(@NonNull Context context) {
//如果之前初始化过,则避免重复初始化
if (weighing != null) {
return true;
}
boolean result = false;
//轮询所有的称重服务,注意是同步方法
for (Type type : Type.values()) {
......@@ -165,16 +161,4 @@ public class WeighingManager {
}
return false;
}
/**
* 断开与称重台的连接
*
* @return 断开操作的结果
*/
public boolean disConnect() {
if (isConnected()) {
return weighing.release();
}
return true;
}
}
......@@ -112,40 +112,47 @@ public class YingtaiWeighing extends BaseWeighing {
@Override
public void setWeighingCallback(WeighingCallback callback) {
if (scl == null) {
return;
}
this.weighingCallback = callback;
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//不断读取重量数据,每100ms一次
int ret = scl.read_standard(buf);
if (ret == 0) {
//读取正常
D.i(TAG, "当前重量为(kg):" + scl.ASCII2HexString(buf, 20).substring(1, 7));
curWeight = parseKg2g(scl.ASCII2HexString(buf, 20).substring(1, 7).trim());
//获取标志位
byte stateByte = buf[0];
//是否稳定标志
boolean isStable = Integer.toBinaryString((int) stateByte).substring(5, 6).equals("1");
//是否在零点
boolean isZero = Integer.toBinaryString((int) stateByte).substring(4, 5).equals("1");
if (isZero == false && curWeight < 0) {
//重置零点一次
resetZero();
}
//是否去皮
boolean isTared = Integer.toBinaryString((int) stateByte).substring(3, 4).equals("1");
D.i(TAG, "当前称状态:" + Integer.toBinaryString((int) stateByte) + "\n" + "去皮:" + isTared + ";零点:" + isZero + ";稳定:" + isStable);
if (weighingCallback != null) {
weighingCallback.returnWeight(curWeight, isStable);
//不断读取重量数据,每10ms一次
try {
int ret = scl.read_standard(buf);
if (ret == 0) {
//读取正常
D.i(TAG, "当前重量为(kg):" + scl.ASCII2HexString(buf, 20).substring(1, 7));
curWeight = parseKg2g(scl.ASCII2HexString(buf, 20).substring(1, 7).trim());
//获取标志位
byte stateByte = buf[0];
//是否稳定标志
boolean isStable = Integer.toBinaryString((int) stateByte).substring(5, 6).equals("1");
//是否在零点
boolean isZero = Integer.toBinaryString((int) stateByte).substring(4, 5).equals("1");
if (isZero == false && curWeight < 0) {
//重置零点一次
resetZero();
}
//是否去皮
boolean isTared = Integer.toBinaryString((int) stateByte).substring(3, 4).equals("1");
D.i(TAG, "当前称状态:" + Integer.toBinaryString((int) stateByte) + "\n" + "去皮:" + isTared + ";零点:" + isZero + ";稳定:" + isStable);
if (weighingCallback != null) {
weighingCallback.returnWeight(curWeight, isStable);
}
} else if (ret == 1) {
//发送指令成功,暂不关心
D.i(TAG, "指令操作结果:" + scl.ASCII2HexString(buf, 20) + ";时间:" + System.currentTimeMillis());
} else if (ret == 2) {
//读取异常
curWeight = -2;
}
} else if (ret == 1) {
//发送指令成功,暂不关心
D.i(TAG, "指令操作结果:" + scl.ASCII2HexString(buf, 20) + ";时间:" + System.currentTimeMillis());
} else if (ret == 2) {
//读取异常
curWeight = -2;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, 0, 10);
......@@ -154,9 +161,10 @@ public class YingtaiWeighing extends BaseWeighing {
@Override
public boolean release() {
if (scl != null) {
scl.SCL_close();
}
//调用该方法时会抛native异常,不建议调用
// if (scl != null) {
// scl.SCL_close();
// }
scl = null;
buf = null;
if (timer != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment