Commit 5c645801 authored by pengguangpu's avatar pengguangpu

完善称重sdk demo

parent 5251c10d
package com.miya.hardware;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
public class HardwareActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hardware);
findViewById(R.id.btnWeighing).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(HardwareActivity.this, WeighingActivity.class));
}
});
findViewById(R.id.btnPrint).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(HardwareActivity.this, PrintActivity.class));
}
});
}
}
......@@ -2,13 +2,114 @@ package com.miya.hardware;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class WeighingActivity extends Activity {
import com.miya.weighing.Weighing;
import com.miya.weighing.WeighingManager;
public class WeighingActivity extends Activity implements View.OnClickListener {
final static int MSG_UPDATE_RESULT = 0x01;
EditText etGravity, etTare;
TextView tvResult;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_UPDATE_RESULT:
tvResult.setText("当前称重量(g):" + msg.getData().getString("weight") + " ;是否稳定:" + msg.getData().getBoolean("isStable"));
break;
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weighing);
tvResult = findViewById(R.id.tvResult);
etGravity = findViewById(R.id.etGravity);
etTare = findViewById(R.id.etTare);
findViewById(R.id.btnInit).setOnClickListener(this);
findViewById(R.id.btnRead).setOnClickListener(this);
findViewById(R.id.btnZero).setOnClickListener(this);
findViewById(R.id.btnTare).setOnClickListener(this);
findViewById(R.id.btnGravity).setOnClickListener(this);
findViewById(R.id.btnRelease).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnInit:
boolean isSuccess = WeighingManager.getInstance().init(this.getApplicationContext());
if (isSuccess == true) {
tvResult.setText("初始化成功:硬件为:" + WeighingManager.getInstance().getWeighing().getWeighingName());
} else {
tvResult.setText("没找到称重硬件!");
}
break;
case R.id.btnRead:
if (WeighingManager.getInstance().isConnected()) {
WeighingManager.getInstance().getWeighing().setWeighingCallback(new Weighing.WeighingCallback() {
@Override
public void returnWeight(int weight, boolean isStable) {
//抛至主线程
Bundle bundle = new Bundle();
bundle.putString("weight", weight + "");
bundle.putBoolean("isStable", isStable);
Message msg = new Message();
msg.what = MSG_UPDATE_RESULT;
msg.setData(bundle);
handler.sendMessage(msg);
}
});
}
break;
case R.id.btnZero:
if (WeighingManager.getInstance().isConnected()) {
WeighingManager.getInstance().getWeighing().resetZero();
}
break;
case R.id.btnTare:
if (WeighingManager.getInstance().isConnected()) {
try {
WeighingManager.getInstance().getWeighing().setTare(Integer.parseInt(etTare.getText().toString()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
break;
case R.id.btnGravity:
if (WeighingManager.getInstance().isConnected()) {
try {
WeighingManager.getInstance().getWeighing().setGravity(Integer.parseInt(etGravity.getText().toString()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
break;
case R.id.btnRelease:
if (WeighingManager.getInstance().isConnected()) {
WeighingManager.getInstance().getWeighing().release();
}
break;
}
if (WeighingManager.getInstance().isConnected() == false) {
tvResult.setText("当前没有称重设备,试试初始化!");
return;
}
}
}
......@@ -9,6 +9,5 @@ public class HardwareApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
WeighingManager.getInstance().init(this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnWeighing"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="称重demo" />
<Button
android:id="@+id/btnPrint"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="打印demo" />
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="显示重量读取结果"
android:textSize="36sp" />
<TableLayout
android:id="@+id/layoutOp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:padding="16dp">
<TableRow>
<Button
android:id="@+id/btnInit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="初始化" />
<Button
android:id="@+id/btnRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="读取重量" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnZero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重置零点" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etTare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/btnTare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置皮重" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etGravity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/btnGravity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置重力加速度" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnRelease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="销毁称重资源" />
</TableRow>
</TableLayout>
</LinearLayout>
\ No newline at end of file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
......
......@@ -22,6 +22,12 @@ android {
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
......
......@@ -39,7 +39,7 @@ public class ChuangjieWeighing extends BaseWeighing {
public void weight(String s) {
Log.i(TAG, "当前重量为:" + s);
curWeight = parseKg2g(s.trim());
if (weighingCallback != null && curWeight > 0) {
if (weighingCallback != null && curWeight >= 0) {
weighingCallback.returnWeight(curWeight, true);
}
if (curWeight < 0) {
......
package com.miya.weighing;
import android.os.Build;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* cpu相关工具类
*
* @author pupu
* @time 2018/10/24
*/
public class CpuUtils {
private CpuUtils() {
throw new UnsupportedOperationException("不允许外部创建实例");
}
/**
* 获取cpu支持的架构类型
*
* @return 支持的架构字符串
*/
public static List<String> getCpuSupportAbis() {
String[] abis;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
abis = Build.SUPPORTED_ABIS;
} else {
abis = new String[]{Build.CPU_ABI, Build.CPU_ABI2};
}
List<String> list = new ArrayList<>();
for (String tmp : abis) {
list.add(tmp);
}
return list;
}
/**
* 是否是arm架构
*
* @return
*/
public static boolean isArm() {
boolean isArm = false;
for (String tmp : getCpuSupportAbis()) {
if (tmp.contains("armeabi")) {
//是arm架构
isArm = true;
break;
}
}
return isArm;
}
/**
* 是否是x86架构
*
* @return
*/
public static boolean isX86() {
boolean isX86 = false;
for (String tmp : getCpuSupportAbis()) {
if (tmp.contains("x86")) {
isX86 = true;
break;
}
}
return isX86;
}
/**
* [获取cpu类型和架构]
*
* @return 三个参数类型的数组,第一个参数标识是不是ARM架构,第二个参数标识是V6还是V7架构,第三个参数标识是不是neon指令集
*/
public static Object[] getCpuArchitecture() {
Object[] mArmArchitecture = new Object[3];
// if ((Integer) mArmArchitecture[1] != -1) {
// return mArmArchitecture;
// }
try {
InputStream is = new FileInputStream("/proc/cpuinfo");
InputStreamReader ir = new InputStreamReader(is);
BufferedReader br = new BufferedReader(ir);
try {
String nameProcessor = "Processor";
String nameFeatures = "Features";
String nameModel = "model name";
String nameCpuFamily = "cpu family";
while (true) {
String line = br.readLine();
String[] pair = null;
if (line == null) {
break;
}
pair = line.split(":");
if (pair.length != 2)
continue;
String key = pair[0].trim();
String val = pair[1].trim();
if (key.compareTo(nameProcessor) == 0) {
String n = "";
for (int i = val.indexOf("ARMv") + 4; i < val.length(); i++) {
String temp = val.charAt(i) + "";
if (temp.matches("\\d")) {
n += temp;
} else {
break;
}
}
mArmArchitecture[0] = "ARM";
mArmArchitecture[1] = Integer.parseInt(n);
continue;
}
if (key.compareToIgnoreCase(nameFeatures) == 0) {
if (val.contains("neon")) {
mArmArchitecture[2] = "neon";
}
continue;
}
if (key.compareToIgnoreCase(nameModel) == 0) {
if (val.contains("Intel")) {
mArmArchitecture[0] = "INTEL";
mArmArchitecture[2] = "atom";
}
continue;
}
if (key.compareToIgnoreCase(nameCpuFamily) == 0) {
mArmArchitecture[1] = Integer.parseInt(val);
continue;
}
}
} finally {
br.close();
ir.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return mArmArchitecture;
}
}
package com.miya.weighing;
public class D {
private static final String Tag = "HHVerify";
public static void log(String text) {
if (BuildConfig.DEBUG) {
android.util.Log.v(Tag, "-----:" + text);
}
}
public static void v(String tag, String msg) {
if (BuildConfig.DEBUG) {
android.util.Log.v(tag, msg);
}
}
public static void v(String tag, String msg, Throwable tr) {
if (BuildConfig.DEBUG) {
android.util.Log.v(tag, msg, tr);
}
}
public static void d(String tag, String msg) {
if (BuildConfig.DEBUG) {
android.util.Log.d(tag, msg);
}
}
public static void d(String tag, String msg, Throwable tr) {
if (BuildConfig.DEBUG) {
android.util.Log.d(tag, msg, tr);
}
}
public static void i(String tag, String msg) {
if (BuildConfig.DEBUG) {
android.util.Log.i(tag, msg);
}
}
public static void i(String tag, String msg, Throwable tr) {
if (BuildConfig.DEBUG) {
android.util.Log.i(tag, msg, tr);
}
}
public static void w(String tag, String msg) {
if (BuildConfig.DEBUG) {
android.util.Log.w(tag, msg);
}
}
public static void w(String tag, String msg, Throwable tr) {
if (BuildConfig.DEBUG) {
android.util.Log.w(tag, msg, tr);
}
}
public static void w(String tag, Throwable tr) {
if (BuildConfig.DEBUG) {
android.util.Log.w(tag, tr);
}
}
public static void e(String tag, String msg) {
if (BuildConfig.DEBUG) {
android.util.Log.e(tag, msg);
}
}
public static void e(String tag, String msg, Throwable tr) {
if (BuildConfig.DEBUG) {
android.util.Log.e(tag, msg, tr);
}
}
}
......@@ -76,6 +76,10 @@ public class WeighingManager {
* @return
*/
public boolean init(@NonNull Context context) {
//如果之前初始化过,则避免重复初始化
if (weighing != null) {
return true;
}
boolean result = false;
//轮询所有的称重服务,注意是同步方法
for (Type type : Type.values()) {
......
package com.miya.weighing;
import android.content.Context;
import com.sdy.huihuaverify.core.utils.CpuUtils;
import com.sdy.huihuaverify.core.utils.D;
import java.util.Timer;
import java.util.TimerTask;
import cn.wintec.wtandroidjar.SCL;
/**
......
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