Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
BluToysApplication
Overview
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chengchong
BluToysApplication
Commits
d07f0383
Commit
d07f0383
authored
Mar 18, 2021
by
韩飞虎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用jar 方式引入的
parent
1c3b6ca2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
9 additions
and
359 deletions
+9
-359
build.gradle
app/build.gradle
+1
-0
ble-agreement.jar
app/libs/ble-agreement.jar
+0
-0
SelectTimerActivity.java
...va/com/example/blu/toys/activity/SelectTimerActivity.java
+1
-1
BCD8421Operater.java
...a/com/example/blu/toys/ble/agreement/BCD8421Operater.java
+0
-65
BitOperator.java
.../java/com/example/blu/toys/ble/agreement/BitOperator.java
+0
-0
HexStringUtils.java
...va/com/example/blu/toys/ble/agreement/HexStringUtils.java
+0
-57
TrafficLightBean.java
.../com/example/blu/toys/ble/agreement/TrafficLightBean.java
+0
-214
activity_select_timer.xml
app/src/main/res/layout/activity_select_timer.xml
+2
-22
build.gradle
build.gradle
+5
-0
ble-agreement.jar
libs/ble-agreement.jar
+0
-0
No files found.
app/build.gradle
View file @
d07f0383
...
...
@@ -65,6 +65,7 @@ dependencies {
implementation
project
(
path:
':library'
)
testImplementation
'junit:junit:4.12'
androidTestImplementation
'androidx.test.ext:junit:1.1.2'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.3.0'
...
...
app/libs/ble-agreement.jar
0 → 100644
View file @
d07f0383
File added
app/src/main/java/com/example/blu/toys/activity/SelectTimerActivity.java
View file @
d07f0383
...
...
@@ -41,7 +41,7 @@ public class SelectTimerActivity extends BaseActivity {
SpUtils
spUtils
=
SpUtils
.
getSpUtils
(
this
);
spUtils
.
getSPValue
(
"close"
,
1
);
spUtils
.
getSPValue
(
"open"
,
10
);
int
open
=
spUtils
.
getSPValue
(
"open"
,
1
);
int
alarm
=
spUtils
.
getSPValue
(
"alarm"
,
1
);
int
green
=
spUtils
.
getSPValue
(
"green"
,
1
);
int
brightness
=
spUtils
.
getSPValue
(
"brightness"
,
50
);
...
...
app/src/main/java/com/example/blu/toys/ble/agreement/BCD8421Operater.java
deleted
100644 → 0
View file @
1c3b6ca2
package
com
.
example
.
blu
.
toys
.
ble
.
agreement
;
public
class
BCD8421Operater
{
/**
* BCD字节数组===>String
* 中间
* @param bytes
* @return 十进制字符串
*/
public
static
String
bcd2String
(
byte
[]
bytes
)
{
StringBuilder
temp
=
new
StringBuilder
(
bytes
.
length
*
2
);
for
(
int
i
=
0
;
i
<
bytes
.
length
;
i
++)
{
// 高四位
temp
.
append
((
bytes
[
i
]
&
0xf0
)
>>>
4
);
// 低四位
temp
.
append
(
bytes
[
i
]
&
0x0f
);
}
return
temp
.
toString
().
substring
(
0
,
1
).
equalsIgnoreCase
(
"0"
)
?
temp
.
toString
().
substring
(
1
)
:
temp
.
toString
();
}
/**
* 字符串==>BCD字节数组
*
* @param str
* @return BCD字节数组
*/
public
static
byte
[]
string2Bcd
(
String
str
)
{
// 奇数,前补零
if
((
str
.
length
()
&
0x1
)
==
1
)
{
str
=
"0"
+
str
;
}
byte
ret
[]
=
new
byte
[
str
.
length
()
/
2
];
byte
bs
[]
=
str
.
getBytes
();
for
(
int
i
=
0
;
i
<
ret
.
length
;
i
++)
{
byte
high
=
ascII2Bcd
(
bs
[
2
*
i
]);
byte
low
=
ascII2Bcd
(
bs
[
2
*
i
+
1
]);
// TODO 只遮罩BCD低四位?
ret
[
i
]
=
(
byte
)
((
high
<<
4
)
|
low
);
}
return
ret
;
}
private
static
byte
ascII2Bcd
(
byte
asc
)
{
if
((
asc
>=
'0'
)
&&
(
asc
<=
'9'
))
return
(
byte
)
(
asc
-
'0'
);
else
if
((
asc
>=
'A'
)
&&
(
asc
<=
'F'
))
return
(
byte
)
(
asc
-
'A'
+
10
);
else
if
((
asc
>=
'a'
)
&&
(
asc
<=
'f'
))
return
(
byte
)
(
asc
-
'a'
+
10
);
else
return
(
byte
)
(
asc
-
48
);
}
// public static void main(String[] sr){
// String aa=ByteUtil.bytesToHexString(BCD8421Operater.string2Bcd("211212121213"));
// System.out.println(aa);
//
// }
}
\ No newline at end of file
app/src/main/java/com/example/blu/toys/ble/agreement/BitOperator.java
deleted
100644 → 0
View file @
1c3b6ca2
This diff is collapsed.
Click to expand it.
app/src/main/java/com/example/blu/toys/ble/agreement/HexStringUtils.java
deleted
100644 → 0
View file @
1c3b6ca2
package
com
.
example
.
blu
.
toys
.
ble
.
agreement
;
import
java.io.UnsupportedEncodingException
;
import
java.nio.charset.Charset
;
public
class
HexStringUtils
{
private
static
final
char
[]
DIGITS_HEX
=
{
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
};
protected
static
char
[]
encodeHex
(
byte
[]
data
)
{
int
l
=
data
.
length
;
char
[]
out
=
new
char
[
l
<<
1
];
for
(
int
i
=
0
,
j
=
0
;
i
<
l
;
i
++)
{
out
[
j
++]
=
DIGITS_HEX
[(
0xF0
&
data
[
i
])
>>>
4
];
out
[
j
++]
=
DIGITS_HEX
[
0x0F
&
data
[
i
]];
}
return
out
;
}
protected
static
byte
[]
decodeHex
(
char
[]
data
)
{
int
len
=
data
.
length
;
if
((
len
&
0x01
)
!=
0
)
{
throw
new
RuntimeException
(
"字符个数应该为偶数"
);
}
byte
[]
out
=
new
byte
[
len
>>
1
];
for
(
int
i
=
0
,
j
=
0
;
j
<
len
;
i
++)
{
int
f
=
toDigit
(
data
[
j
],
j
)
<<
4
;
j
++;
f
|=
toDigit
(
data
[
j
],
j
);
j
++;
out
[
i
]
=
(
byte
)
(
f
&
0xFF
);
}
return
out
;
}
protected
static
int
toDigit
(
char
ch
,
int
index
)
{
int
digit
=
Character
.
digit
(
ch
,
16
);
if
(
digit
==
-
1
)
{
throw
new
RuntimeException
(
"Illegal hexadecimal character "
+
ch
+
" at index "
+
index
);
}
return
digit
;
}
public
static
String
toHexString
(
byte
[]
bs
)
{
return
new
String
(
encodeHex
(
bs
));
}
public
static
byte
[]
hexString2Bytes
(
String
hex
)
{
return
decodeHex
(
hex
.
toCharArray
());
}
public
static
byte
[]
chars2Bytes
(
char
[]
bs
)
{
return
decodeHex
(
bs
);
}
}
\ No newline at end of file
app/src/main/java/com/example/blu/toys/ble/agreement/TrafficLightBean.java
deleted
100644 → 0
View file @
1c3b6ca2
package
com
.
example
.
blu
.
toys
.
ble
.
agreement
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
TrafficLightBean
{
/**
* 固定头 1字节
*/
private
byte
begin
=
0x24
;
/**
* 长度 1字节
*/
private
byte
length
=
0x11
;
/**
* 协议编号 01:关机协议 02:暂停于继续协议 1字节
*/
private
byte
cmd
;
/**
* 手机时间 yyMMddHHssmm 6个字节
*/
private
byte
[]
phoneTime
;
/**
* 暂停继续 0:暂停 1:继续 1字节
*/
private
byte
suspendAndContinue
;
/**
* 剩余时间(小端) 秒为单位 2字节
*/
private
byte
[]
timeRemaining
;
/**
* 亮度调节 0-100 1字节
*/
private
byte
brightness
;
/**
* 开机声音编号 1-20 1字节
*/
private
byte
openSoundNo
;
/**
* 关机声音编号 1-20 1字节
*/
private
byte
closeSoundNo
;
/**
* 红绿灯变更方式 0: 由绿变红 1:由红变黄 1字节
*/
private
byte
trafficLightUpdateSet
;
/**
* 警报开关 0:关 1:开 1字节
*/
private
byte
alarm
;
private
byte
key
;
/**
*
* @param protocolNo 协议编号
* @param suspendAndContinue 暂停或者继续
* @param timeRemaining 剩余时间 毫秒
* @param brightness 亮度值 0-100 默认 50
* @param openSoundNo 开机声音编号 默认 1
* @param closeSoundNo 关机声音编号 默认 10
* @param trafficLightUpdateSet 红绿灯变更方式 默认 1
* @param alarm 报警状态 默认 0
*/
public
TrafficLightBean
(
Integer
protocolNo
,
Integer
suspendAndContinue
,
Integer
timeRemaining
,
Integer
brightness
,
Integer
openSoundNo
,
Integer
closeSoundNo
,
Integer
trafficLightUpdateSet
,
Integer
alarm
){
//这个方法是 给该对象属性赋值的 ,就是 在new TrafficLightBean(xx,xxx) 的时候调用
// 传入的协议编号 01 和 02 关机 是 01 其他操作 都是02
this
.
cmd
=
BitOperator
.
integerTo1Byte
(
protocolNo
);
//暂停与继续 ,如果是关机协议传入0 即可
this
.
suspendAndContinue
=
BitOperator
.
integerTo1Byte
(
suspendAndContinue
);
System
.
out
.
println
(
"传入的剩余时间:"
+
timeRemaining
);
//剩余时间 小端 2字节 toLH 方法是转为小端的 传入的是秒数
this
.
timeRemaining
=
toLH
(
timeRemaining
);
//BitOperator.integerTo2Bytes();
//亮度 0-100 将 10进制的 BitOperator.integerTo1Byte是 数字 转为 1字节的byte
this
.
brightness
=
BitOperator
.
integerTo1Byte
(
brightness
);
//开机声音的编号 1-20 BitOperator.integerTo1Byte是 将 10进制的 数字 转为 1字节的byte
this
.
openSoundNo
=
BitOperator
.
integerTo1Byte
(
openSoundNo
);
//关机声音的编号 1-20 BitOperator.integerTo1Byte是 将 10进制的 数字 转为 1字节的byte
this
.
closeSoundNo
=
BitOperator
.
integerTo1Byte
(
closeSoundNo
);
//红绿灯变更方式 BitOperator.integerTo1Byte是 将 10进制的 数字 转为 1字节的byte
this
.
trafficLightUpdateSet
=
BitOperator
.
integerTo1Byte
(
trafficLightUpdateSet
);
//警报开关 BitOperator.integerTo1Byte是 将 10进制的 数字 转为 1字节的byte
this
.
alarm
=
BitOperator
.
integerTo1Byte
(
alarm
);
//格式化时间的对象
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyMMddHHmmss"
);
//将当前时间格式化
String
phoneTime
=
format
.
format
(
new
Date
());
//将字符串的日期转换为 6个字节的byte 数组
this
.
phoneTime
=
BCD8421Operater
.
string2Bcd
(
phoneTime
);
//分别获取时间的时分秒
String
yy
=
phoneTime
.
substring
(
0
,
2
);
String
MM
=
phoneTime
.
substring
(
2
,
4
);
String
dd
=
phoneTime
.
substring
(
4
,
6
);
String
HH
=
phoneTime
.
substring
(
6
,
8
);
String
mm
=
phoneTime
.
substring
(
8
,
10
);
String
ss
=
phoneTime
.
substring
(
10
,
12
);
//开始加密协议
// yy + HH 也就是 年 +时 = 第一个数
// 比如 20 + 12=0x32 10 //20年 + 12点 =0x32
Integer
bo1
=
BCD8421Operater
.
string2Bcd
(
yy
)[
0
]+
BCD8421Operater
.
string2Bcd
(
HH
)[
0
];
// MM + mm 也就是 月 异或 分钟 = 第二个数
Integer
bo2
=
BCD8421Operater
.
string2Bcd
(
MM
)[
0
]^
BCD8421Operater
.
string2Bcd
(
mm
)[
0
];
// dd + ss 也就是 天 + 秒 = 第三个数
Integer
bo3
=
BCD8421Operater
.
string2Bcd
(
dd
)[
0
]+
BCD8421Operater
.
string2Bcd
(
ss
)[
0
];
//最后 经过下面公式 得到KEY
Integer
key
=(
bo1
^
bo2
)+
bo3
;
// 将KEY 换算为byte
this
.
key
=
BitOperator
.
integerTo1Byte
(
key
);
System
.
out
.
println
(
"时间:"
+
phoneTime
);
System
.
out
.
println
(
"key 10进制:"
+
key
);
System
.
out
.
println
(
"key java byte:"
+
this
.
key
);
System
.
out
.
println
(
"key HEX:"
+
HexStringUtils
.
toHexString
(
new
byte
[]{
this
.
key
}));
//到此协议组装 完毕, 值也都付完了, 因为java 框架,发送给蓝牙设备的的是byte 有一个toByte方法 在下面 调用即可得到 这个协议的byte[] 数组 发送给设备即可
}
/**
* int 转换小端
* @param n
* @return
*/
public
static
byte
[]
toLH
(
int
n
)
{
byte
[]
b
=
new
byte
[
2
];
b
[
0
]
=
(
byte
)
(
n
&
0xff
);
b
[
1
]
=
(
byte
)
(
n
>>
8
&
0xff
);
return
b
;
}
public
byte
[]
toByte
(){
//数据要加密的
//协议总长度 是17个自己 因此定义 byte 17
byte
[]
resData
=
new
byte
[
17
];
//第一个字节 固定头
resData
[
0
]=
this
.
begin
;
//第二个节=协议长度
resData
[
1
]=
this
.
length
;
//第三个节=协议编号 从这里开始 往下每个字节都需要加密 手机时间字节不需要 加密方式就是 将数据加上刚计算出来的KEY 所以是 this.cmd+this.key
resData
[
2
]=
BitOperator
.
integerTo1Byte
(
this
.
cmd
+
this
.
key
);
//拼装 手机时间
resData
[
3
]=
this
.
phoneTime
[
0
];
//1 不需要加密
resData
[
4
]=
this
.
phoneTime
[
1
];
//2 不需要加密
resData
[
5
]=
this
.
phoneTime
[
2
];
//3 不需要加密
resData
[
6
]=
this
.
phoneTime
[
3
];
//4 不需要加密
resData
[
7
]=
this
.
phoneTime
[
4
];
//5 不需要急吗
resData
[
8
]=
this
.
phoneTime
[
5
];
//6 不需要加密
//暂停与继续 +this.key
resData
[
9
]=
BitOperator
.
integerTo1Byte
(
this
.
suspendAndContinue
+
this
.
key
);
//剩余时间 第一个字节 KEY
resData
[
10
]=
BitOperator
.
integerTo1Byte
(
this
.
timeRemaining
[
0
]+
this
.
key
);
//剩余时间 第二个字节 KEY
resData
[
11
]=
BitOperator
.
integerTo1Byte
(
this
.
timeRemaining
[
1
]+
this
.
key
);
resData
[
12
]=
BitOperator
.
integerTo1Byte
(
this
.
brightness
+
this
.
key
);
resData
[
13
]=
BitOperator
.
integerTo1Byte
(
this
.
openSoundNo
+
this
.
key
);
resData
[
14
]=
BitOperator
.
integerTo1Byte
(
this
.
closeSoundNo
+
this
.
key
);
resData
[
15
]=
BitOperator
.
integerTo1Byte
(
this
.
trafficLightUpdateSet
+
this
.
key
);
//报警开关
resData
[
16
]=
BitOperator
.
integerTo1Byte
(
this
.
alarm
+
this
.
key
);
System
.
out
.
println
(
"发送数据为:"
+
HexStringUtils
.
toHexString
(
resData
));
return
resData
;
}
//这里是调用的示例 main 函数
public
static
void
main
(
String
[]
args
)
{
//创建了一个这样的对象 ,将参数都传入
TrafficLightBean
trafficLightBean
=
new
TrafficLightBean
(
2
,
0
,
255
,
100
,
10
,
20
,
01
,
01
);
//然后调用这个对象里面的 toByte 方法 生成给设备发送的数据
System
.
out
.
println
(
HexStringUtils
.
toHexString
(
trafficLightBean
.
toByte
()));
System
.
out
.
println
(
HexStringUtils
.
toHexString
(
new
byte
[]{-
1
}));
System
.
out
.
println
(
HexStringUtils
.
toHexString
(
new
byte
[]{
117
}));
System
.
out
.
println
(
HexStringUtils
.
toHexString
(
new
byte
[]{
116
}));
System
.
out
.
println
(
HexStringUtils
.
toHexString
(
new
byte
[]{
116
}));
int
a
=
0xff
+
0x75
;
System
.
out
.
println
(
a
);
}
}
\ No newline at end of file
app/src/main/res/layout/activity_select_timer.xml
View file @
d07f0383
...
...
@@ -234,28 +234,7 @@
android:layout_alignParentBottom=
"true"
android:layout_marginBottom=
"@dimen/dp_20"
android:gravity=
"center_horizontal"
android:orientation=
"horizontal"
>
<ImageView
android:layout_width=
"wrap_content"
android:layout_height=
"@dimen/dp_80"
android:src=
"@mipmap/shopbutton"
/>
<ImageView
android:layout_width=
"wrap_content"
android:layout_height=
"@dimen/dp_80"
android:layout_marginLeft=
"@dimen/dp_8"
android:layout_marginRight=
"@dimen/dp_8"
android:src=
"@mipmap/settingsbutton"
/>
<ImageView
android:layout_width=
"wrap_content"
android:layout_height=
"@dimen/dp_80"
android:src=
"@mipmap/ratebutton"
/>
</LinearLayout>
android:orientation=
"horizontal"
/>
</RelativeLayout>
\ No newline at end of file
build.gradle
View file @
d07f0383
...
...
@@ -8,11 +8,16 @@ buildscript {
}
dependencies
{
classpath
'com.android.tools.build:gradle:4.0.1'
classpath
'com.jakewharton:butterknife-gradle-plugin:9.0.0'
}
}
allprojects
{
repositories
{
google
()
...
...
libs/ble-agreement.jar
0 → 100644
View file @
d07f0383
File added
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