Commit 24f2fafb authored by pengguangpu's avatar pengguangpu

完美解决商米称重问题

parent 98598a2e
...@@ -52,6 +52,7 @@ public class WeighingActivity extends Activity implements View.OnClickListener { ...@@ -52,6 +52,7 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
findViewById(R.id.btnGravity).setOnClickListener(this); findViewById(R.id.btnGravity).setOnClickListener(this);
findViewById(R.id.btnRelease).setOnClickListener(this); findViewById(R.id.btnRelease).setOnClickListener(this);
findViewById(R.id.btnReadNow).setOnClickListener(this); findViewById(R.id.btnReadNow).setOnClickListener(this);
findViewById(R.id.ivBack).setOnClickListener(this);
} }
@Override @Override
...@@ -107,14 +108,19 @@ public class WeighingActivity extends Activity implements View.OnClickListener { ...@@ -107,14 +108,19 @@ public class WeighingActivity extends Activity implements View.OnClickListener {
break; break;
case R.id.btnRelease: case R.id.btnRelease:
if (WeighingManager.getInstance().isConnected()) { if (WeighingManager.getInstance().isConnected()) {
WeighingManager.getInstance().getWeighing().release(); if (WeighingManager.getInstance().getWeighing().release() == true) {
tvResultNow.setText("断开连接成功!");
}
} }
break; break;
case R.id.btnReadNow: case R.id.btnReadNow:
if (WeighingManager.getInstance().isConnected()) { if (WeighingManager.getInstance().isConnected()) {
tvResultNow.setText(WeighingManager.getInstance().getWeighing().getWeight()+""); tvResultNow.setText(WeighingManager.getInstance().getWeighing().getWeight() + "");
} }
break; break;
case R.id.ivBack:
finish();
break;
} }
if (WeighingManager.getInstance().isConnected() == false) { if (WeighingManager.getInstance().isConnected() == false) {
tvResult.setText("当前没有称重设备,试试初始化!"); tvResult.setText("当前没有称重设备,试试初始化!");
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
...@@ -9,11 +10,18 @@ ...@@ -9,11 +10,18 @@
android:layout_height="88dp" android:layout_height="88dp"
android:background="@color/colorPrimary"> 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 <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="16dp" android:layout_margin="16dp"
android:text="称重开放平台" android:gravity="right"
android:text="称重开放组件"
android:textColor="#ffffff" android:textColor="#ffffff"
android:textSize="36sp" /> android:textSize="36sp" />
</android.support.v7.widget.ActionMenuView> </android.support.v7.widget.ActionMenuView>
......
...@@ -94,12 +94,16 @@ public class HisenseWeighing extends BaseWeighing { ...@@ -94,12 +94,16 @@ public class HisenseWeighing extends BaseWeighing {
@Override @Override
public boolean release() { public boolean release() {
if (context != null && serviceConnection != null) {
this.context.unbindService(serviceConnection); this.context.unbindService(serviceConnection);
}
try { try {
uartScale.close(); uartScale.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
context = null;
serviceConnection = null;
curWeight = 0; curWeight = 0;
tare = 0; tare = 0;
weighingCallback = null; weighingCallback = null;
......
...@@ -8,6 +8,7 @@ import android.os.IBinder; ...@@ -8,6 +8,7 @@ import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import com.miya.weighing.utils.D;
import com.sunmi.electronicscaleservice.ScaleCallback; import com.sunmi.electronicscaleservice.ScaleCallback;
import com.sunmi.electronicscaleservice.ScaleService; import com.sunmi.electronicscaleservice.ScaleService;
...@@ -30,6 +31,33 @@ public class SunmiWeighing extends BaseWeighing { ...@@ -30,6 +31,33 @@ public class SunmiWeighing extends BaseWeighing {
*/ */
ScaleService scaleService; 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 @Override
public boolean init(Context context) { public boolean init(Context context) {
this.context = context; this.context = context;
...@@ -65,41 +93,43 @@ public class SunmiWeighing extends BaseWeighing { ...@@ -65,41 +93,43 @@ public class SunmiWeighing extends BaseWeighing {
if (scaleService != null) { if (scaleService != null) {
//设置重量回调 //设置重量回调
try { try {
scaleService.getData(new ScaleCallback.Stub() { scaleService.getData(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);
}
}
});
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.i(TAG, "称重服务初始化失败!"); D.i(TAG, "称重服务初始化失败!");
} }
} else { } else {
Log.i(TAG, "防损称连接失败!"); D.i(TAG, "防损称连接失败!");
} }
} }
@Override
public void onBindingDied(ComponentName name) {
D.i(TAG, "解绑成功 ComponentName==>" + name);
}
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
scaleService = null; 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 { ...@@ -76,10 +76,6 @@ public class WeighingManager {
* @return * @return
*/ */
public boolean init(@NonNull Context context) { public boolean init(@NonNull Context context) {
//如果之前初始化过,则避免重复初始化
if (weighing != null) {
return true;
}
boolean result = false; boolean result = false;
//轮询所有的称重服务,注意是同步方法 //轮询所有的称重服务,注意是同步方法
for (Type type : Type.values()) { for (Type type : Type.values()) {
...@@ -165,16 +161,4 @@ public class WeighingManager { ...@@ -165,16 +161,4 @@ public class WeighingManager {
} }
return false; return false;
} }
/**
* 断开与称重台的连接
*
* @return 断开操作的结果
*/
public boolean disConnect() {
if (isConnected()) {
return weighing.release();
}
return true;
}
} }
...@@ -112,13 +112,17 @@ public class YingtaiWeighing extends BaseWeighing { ...@@ -112,13 +112,17 @@ public class YingtaiWeighing extends BaseWeighing {
@Override @Override
public void setWeighingCallback(WeighingCallback callback) { public void setWeighingCallback(WeighingCallback callback) {
if (scl == null) {
return;
}
this.weighingCallback = callback; this.weighingCallback = callback;
if (timer == null) { if (timer == null) {
timer = new Timer(); timer = new Timer();
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
//不断读取重量数据,每100ms一次 //不断读取重量数据,每10ms一次
try {
int ret = scl.read_standard(buf); int ret = scl.read_standard(buf);
if (ret == 0) { if (ret == 0) {
//读取正常 //读取正常
...@@ -147,6 +151,9 @@ public class YingtaiWeighing extends BaseWeighing { ...@@ -147,6 +151,9 @@ public class YingtaiWeighing extends BaseWeighing {
//读取异常 //读取异常
curWeight = -2; curWeight = -2;
} }
} catch (Exception ex) {
ex.printStackTrace();
}
} }
}, 0, 10); }, 0, 10);
} }
...@@ -154,9 +161,10 @@ public class YingtaiWeighing extends BaseWeighing { ...@@ -154,9 +161,10 @@ public class YingtaiWeighing extends BaseWeighing {
@Override @Override
public boolean release() { public boolean release() {
if (scl != null) { //调用该方法时会抛native异常,不建议调用
scl.SCL_close(); // if (scl != null) {
} // scl.SCL_close();
// }
scl = null; scl = null;
buf = null; buf = null;
if (timer != 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