Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
miya-hardware
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengguangpu
miya-hardware
Commits
24f2fafb
Commit
24f2fafb
authored
Dec 24, 2018
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完美解决商米称重问题
parent
98598a2e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
115 additions
and
75 deletions
+115
-75
WeighingActivity.java
app/src/main/java/com/miya/hardware/WeighingActivity.java
+8
-2
activity_weighing.xml
app/src/main/res/layout/activity_weighing.xml
+9
-1
HisenseWeighing.java
...hing/src/main/java/com/miya/weighing/HisenseWeighing.java
+5
-1
SunmiWeighing.java
weighing/src/main/java/com/miya/weighing/SunmiWeighing.java
+55
-25
WeighingManager.java
...hing/src/main/java/com/miya/weighing/WeighingManager.java
+0
-16
YingtaiWeighing.java
...hing/src/main/java/com/miya/weighing/YingtaiWeighing.java
+38
-30
No files found.
app/src/main/java/com/miya/hardware/WeighingActivity.java
View file @
24f2fafb
...
@@ -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
(
"当前没有称重设备,试试初始化!"
);
...
...
app/src/main/res/layout/activity_weighing.xml
View file @
24f2fafb
<?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>
...
...
weighing/src/main/java/com/miya/weighing/HisenseWeighing.java
View file @
24f2fafb
...
@@ -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
()
{
this
.
context
.
unbindService
(
serviceConnection
);
if
(
context
!=
null
&&
serviceConnection
!=
null
)
{
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
;
...
...
weighing/src/main/java/com/miya/weighing/SunmiWeighing.java
View file @
24f2fafb
...
@@ -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
;
}
}
}
weighing/src/main/java/com/miya/weighing/WeighingManager.java
View file @
24f2fafb
...
@@ -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
;
}
}
}
weighing/src/main/java/com/miya/weighing/YingtaiWeighing.java
View file @
24f2fafb
...
@@ -112,40 +112,47 @@ public class YingtaiWeighing extends BaseWeighing {
...
@@ -112,40 +112,47 @@ 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一次
int
ret
=
scl
.
read_standard
(
buf
);
try
{
if
(
ret
==
0
)
{
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
());
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
];
//获取标志位
//是否稳定标志
byte
stateByte
=
buf
[
0
];
boolean
isStable
=
Integer
.
toBinaryString
((
int
)
stateByte
).
substring
(
5
,
6
).
equals
(
"1"
);
//是否稳定标志
//是否在零点
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
)
{
boolean
isZero
=
Integer
.
toBinaryString
((
int
)
stateByte
).
substring
(
4
,
5
).
equals
(
"1"
);
//重置零点一次
if
(
isZero
==
false
&&
curWeight
<
0
)
{
resetZero
();
//重置零点一次
}
resetZero
();
//是否去皮
}
boolean
isTared
=
Integer
.
toBinaryString
((
int
)
stateByte
).
substring
(
3
,
4
).
equals
(
"1"
);
//是否去皮
D
.
i
(
TAG
,
"当前称状态:"
+
Integer
.
toBinaryString
((
int
)
stateByte
)
+
"\n"
+
"去皮:"
+
isTared
+
";零点:"
+
isZero
+
";稳定:"
+
isStable
);
boolean
isTared
=
Integer
.
toBinaryString
((
int
)
stateByte
).
substring
(
3
,
4
).
equals
(
"1"
);
if
(
weighingCallback
!=
null
)
{
D
.
i
(
TAG
,
"当前称状态:"
+
Integer
.
toBinaryString
((
int
)
stateByte
)
+
"\n"
+
"去皮:"
+
isTared
+
";零点:"
+
isZero
+
";稳定:"
+
isStable
);
weighingCallback
.
returnWeight
(
curWeight
,
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
)
{
}
catch
(
Exception
ex
)
{
//发送指令成功,暂不关心
ex
.
printStackTrace
();
D
.
i
(
TAG
,
"指令操作结果:"
+
scl
.
ASCII2HexString
(
buf
,
20
)
+
";时间:"
+
System
.
currentTimeMillis
());
}
else
if
(
ret
==
2
)
{
//读取异常
curWeight
=
-
2
;
}
}
}
}
},
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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment