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
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
9 additions
and
808 deletions
+9
-808
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
-449
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 {
...
@@ -65,6 +65,7 @@ dependencies {
implementation
project
(
path:
':library'
)
implementation
project
(
path:
':library'
)
testImplementation
'junit:junit:4.12'
testImplementation
'junit:junit:4.12'
androidTestImplementation
'androidx.test.ext:junit:1.1.2'
androidTestImplementation
'androidx.test.ext:junit:1.1.2'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.3.0'
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 {
...
@@ -41,7 +41,7 @@ public class SelectTimerActivity extends BaseActivity {
SpUtils
spUtils
=
SpUtils
.
getSpUtils
(
this
);
SpUtils
spUtils
=
SpUtils
.
getSpUtils
(
this
);
spUtils
.
getSPValue
(
"close"
,
1
);
spUtils
.
getSPValue
(
"close"
,
1
);
spUtils
.
getSPValue
(
"open"
,
10
);
int
open
=
spUtils
.
getSPValue
(
"open"
,
1
);
int
alarm
=
spUtils
.
getSPValue
(
"alarm"
,
1
);
int
alarm
=
spUtils
.
getSPValue
(
"alarm"
,
1
);
int
green
=
spUtils
.
getSPValue
(
"green"
,
1
);
int
green
=
spUtils
.
getSPValue
(
"green"
,
1
);
int
brightness
=
spUtils
.
getSPValue
(
"brightness"
,
50
);
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
package
com
.
example
.
blu
.
toys
.
ble
.
agreement
;
import
java.nio.ByteBuffer
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
BitOperator
{
/**
* 把一个整形该为byte
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
integerTo1Byte
(
int
value
)
{
return
(
byte
)
(
value
&
0xFF
);
}
/**
* 把一个整形该为1位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
[]
integerTo1Bytes
(
int
value
)
{
byte
[]
result
=
new
byte
[
1
];
result
[
0
]
=
(
byte
)
(
value
&
0xFF
);
return
result
;
}
/**
* 把一个整形改为2位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
[]
integerTo2Bytes
(
int
value
)
{
byte
[]
result
=
new
byte
[
2
];
result
[
0
]
=
(
byte
)
((
value
>>>
8
)
&
0xFF
);
result
[
1
]
=
(
byte
)
(
value
&
0xFF
);
return
result
;
}
/**
* 把一个整形改为3位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
[]
integerTo3Bytes
(
int
value
)
{
byte
[]
result
=
new
byte
[
3
];
result
[
0
]
=
(
byte
)
((
value
>>>
16
)
&
0xFF
);
result
[
1
]
=
(
byte
)
((
value
>>>
8
)
&
0xFF
);
result
[
2
]
=
(
byte
)
(
value
&
0xFF
);
return
result
;
}
/**
* 把一个整形改为4位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
[]
integerTo4Bytes
(
int
value
)
{
byte
[]
result
=
new
byte
[
4
];
result
[
0
]
=
(
byte
)
((
value
>>>
24
)
&
0xFF
);
result
[
1
]
=
(
byte
)
((
value
>>>
16
)
&
0xFF
);
result
[
2
]
=
(
byte
)
((
value
>>>
8
)
&
0xFF
);
result
[
3
]
=
(
byte
)
(
value
&
0xFF
);
return
result
;
}
/**
* 把byte[]转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public
static
int
byteToInteger
(
byte
[]
value
)
{
int
result
;
if
(
value
.
length
==
1
)
{
result
=
oneByteToInteger
(
value
[
0
]);
}
else
if
(
value
.
length
==
2
)
{
result
=
twoBytesToInteger
(
value
);
}
else
if
(
value
.
length
==
3
)
{
result
=
threeBytesToInteger
(
value
);
}
else
if
(
value
.
length
==
4
)
{
result
=
fourBytesToInteger
(
value
);
}
else
{
result
=
fourBytesToInteger
(
value
);
}
return
result
;
}
/**
* 把一个byte转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public
static
int
oneByteToInteger
(
byte
value
)
{
return
(
int
)
value
&
0xFF
;
}
/**
* 把一个2位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public
static
int
twoBytesToInteger
(
byte
[]
value
)
{
// if (value.length < 2) {
// throw new Exception("Byte array too short!");
// }
int
temp0
=
value
[
0
]
&
0xFF
;
int
temp1
=
value
[
1
]
&
0xFF
;
return
((
temp0
<<
8
)
+
temp1
);
}
/**
* 把一个3位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public
static
int
threeBytesToInteger
(
byte
[]
value
)
{
int
temp0
=
value
[
0
]
&
0xFF
;
int
temp1
=
value
[
1
]
&
0xFF
;
int
temp2
=
value
[
2
]
&
0xFF
;
return
((
temp0
<<
16
)
+
(
temp1
<<
8
)
+
temp2
);
}
/**
* 把一个4位的数组转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public
static
int
fourBytesToInteger
(
byte
[]
value
)
{
// if (value.length < 4) {
// throw new Exception("Byte array too short!");
// }
int
temp0
=
value
[
0
]
&
0xFF
;
int
temp1
=
value
[
1
]
&
0xFF
;
int
temp2
=
value
[
2
]
&
0xFF
;
int
temp3
=
value
[
3
]
&
0xFF
;
return
((
temp0
<<
24
)
+
(
temp1
<<
16
)
+
(
temp2
<<
8
)
+
temp3
);
}
/**
* 把一个4位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public
static
long
fourBytesToLong
(
byte
[]
value
)
throws
Exception
{
// if (value.length < 4) {
// throw new Exception("Byte array too short!");
// }
int
temp0
=
value
[
0
]
&
0xFF
;
int
temp1
=
value
[
1
]
&
0xFF
;
int
temp2
=
value
[
2
]
&
0xFF
;
int
temp3
=
value
[
3
]
&
0xFF
;
return
(((
long
)
temp0
<<
24
)
+
(
temp1
<<
16
)
+
(
temp2
<<
8
)
+
temp3
);
}
/**
* 把一个数组转化长整形
*
* @param value
* @return
* @throws Exception
*/
public
static
long
bytes2Long
(
byte
[]
value
)
{
long
result
=
0
;
int
len
=
value
.
length
;
int
temp
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
temp
=
(
len
-
1
-
i
)
*
8
;
if
(
temp
==
0
)
{
result
+=
(
value
[
i
]
&
0x0ff
);
}
else
{
result
+=
(
value
[
i
]
&
0x0ff
)
<<
temp
;
}
}
return
result
;
}
/**
* 把一个长整形改为byte数组
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
[]
longToBytes
(
long
value
)
{
return
longToBytes
(
value
,
8
);
}
/**
* 把一个长整形改为byte数组
*
* @param value
* @return
* @throws Exception
*/
public
static
byte
[]
longToBytes
(
long
value
,
int
len
)
{
byte
[]
result
=
new
byte
[
len
];
int
temp
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
temp
=
(
len
-
1
-
i
)
*
8
;
if
(
temp
==
0
)
{
result
[
i
]
+=
(
value
&
0x0ff
);
}
else
{
result
[
i
]
+=
(
value
>>>
temp
)
&
0x0ff
;
}
}
return
result
;
}
/**
* 把IP拆分位int数组
*
* @param ip
* @return
* @throws Exception
*/
public
static
int
[]
getIntIPValue
(
String
ip
)
throws
Exception
{
String
[]
sip
=
ip
.
split
(
"[.]"
);
// if (sip.length != 4) {
// throw new Exception("error IPAddress");
// }
int
[]
intIP
=
{
Integer
.
parseInt
(
sip
[
0
]),
Integer
.
parseInt
(
sip
[
1
]),
Integer
.
parseInt
(
sip
[
2
]),
Integer
.
parseInt
(
sip
[
3
])};
return
intIP
;
}
/**
* 把byte类型IP地址转化位字符串
*
* @param address
* @return
* @throws Exception
*/
public
static
String
getStringIPValue
(
byte
[]
address
)
throws
Exception
{
int
first
=
oneByteToInteger
(
address
[
0
]);
int
second
=
oneByteToInteger
(
address
[
1
]);
int
third
=
oneByteToInteger
(
address
[
2
]);
int
fourth
=
oneByteToInteger
(
address
[
3
]);
return
first
+
"."
+
second
+
"."
+
third
+
"."
+
fourth
;
}
/**
* 合并字节数组
*
* @param first
* @param rest
* @return
*/
public
static
byte
[]
concatAll
(
byte
[]
first
,
byte
[]...
rest
)
{
int
totalLength
=
first
.
length
;
for
(
byte
[]
array
:
rest
)
{
if
(
array
!=
null
)
{
totalLength
+=
array
.
length
;
}
}
byte
[]
result
=
Arrays
.
copyOf
(
first
,
totalLength
);
int
offset
=
first
.
length
;
for
(
byte
[]
array
:
rest
)
{
if
(
array
!=
null
)
{
System
.
arraycopy
(
array
,
0
,
result
,
offset
,
array
.
length
);
offset
+=
array
.
length
;
}
}
return
result
;
}
/**
* 合并字节数组
*
* @param rest
* @return
*/
public
static
byte
[]
concatAll
(
List
<
byte
[]>
rest
)
{
int
totalLength
=
0
;
for
(
byte
[]
array
:
rest
)
{
if
(
array
!=
null
)
{
totalLength
+=
array
.
length
;
}
}
byte
[]
result
=
new
byte
[
totalLength
];
int
offset
=
0
;
for
(
byte
[]
array
:
rest
)
{
if
(
array
!=
null
)
{
System
.
arraycopy
(
array
,
0
,
result
,
offset
,
array
.
length
);
offset
+=
array
.
length
;
}
}
return
result
;
}
public
static
float
byte2Float
(
byte
[]
bs
)
{
return
Float
.
intBitsToFloat
(
(((
bs
[
3
]
&
0xFF
)
<<
24
)
+
((
bs
[
2
]
&
0xFF
)
<<
16
)
+
((
bs
[
1
]
&
0xFF
)
<<
8
)
+
(
bs
[
0
]
&
0xFF
)));
}
public
float
byteBE2Float
(
byte
[]
bytes
)
{
int
l
;
l
=
bytes
[
0
];
l
&=
0xff
;
l
|=
((
long
)
bytes
[
1
]
<<
8
);
l
&=
0xffff
;
l
|=
((
long
)
bytes
[
2
]
<<
16
);
l
&=
0xffffff
;
l
|=
((
long
)
bytes
[
3
]
<<
24
);
return
Float
.
intBitsToFloat
(
l
);
}
public
static
int
getCheckSum4JT808
(
byte
[]
bs
,
int
start
,
int
end
)
{
if
(
start
<
0
||
end
>
bs
.
length
)
throw
new
ArrayIndexOutOfBoundsException
(
"getCheckSum4JT808 error : index out of bounds(start="
+
start
+
",end="
+
end
+
",bytes length="
+
bs
.
length
+
")"
);
int
cs
=
0
;
for
(
int
i
=
start
;
i
<
end
;
i
++)
{
cs
^=
bs
[
i
];
}
return
cs
;
}
public
static
int
getBitRange
(
int
number
,
int
start
,
int
end
)
{
if
(
start
<
0
)
throw
new
IndexOutOfBoundsException
(
"min index is 0,but start = "
+
start
);
if
(
end
>=
Integer
.
SIZE
)
throw
new
IndexOutOfBoundsException
(
"max index is "
+
(
Integer
.
SIZE
-
1
)
+
",but end = "
+
end
);
return
(
number
<<
Integer
.
SIZE
-
(
end
+
1
))
>>>
Integer
.
SIZE
-
(
end
-
start
+
1
);
}
public
static
int
getBitAt
(
int
number
,
int
index
)
{
if
(
index
<
0
)
throw
new
IndexOutOfBoundsException
(
"min index is 0,but "
+
index
);
if
(
index
>=
Integer
.
SIZE
)
throw
new
IndexOutOfBoundsException
(
"max index is "
+
(
Integer
.
SIZE
-
1
)
+
",but "
+
index
);
return
((
1
<<
index
)
&
number
)
>>
index
;
}
public
static
int
getBitAtS
(
int
number
,
int
index
)
{
String
s
=
Integer
.
toBinaryString
(
number
);
return
Integer
.
parseInt
(
s
.
charAt
(
index
)
+
""
);
}
@Deprecated
public
static
int
getBitRangeS
(
int
number
,
int
start
,
int
end
)
{
String
s
=
Integer
.
toBinaryString
(
number
);
StringBuilder
sb
=
new
StringBuilder
(
s
);
while
(
sb
.
length
()
<
Integer
.
SIZE
)
{
sb
.
insert
(
0
,
"0"
);
}
String
tmp
=
sb
.
reverse
().
substring
(
start
,
end
+
1
);
sb
=
new
StringBuilder
(
tmp
);
return
Integer
.
parseInt
(
sb
.
reverse
().
toString
(),
2
);
}
/**
* 计算crc值,注意这里只计算到header 到 length-2的位置
*
* @param
* @return
*/
// public static byte[] CRC(ByteBuf data) {
//// int len = data.readableBytes();
//// if (len < 2) return null;
////
//// int crc = 0xFFFF;//16位
////
////
//// //crc前置部分
//// ByteBuf crcPrefix = Unpooled.wrappedBuffer(new byte[]{0x5c, (byte) 0xad, (byte) 0xf3, 0x4e});
//// //全部
//// ByteBuf crcAll = Unpooled.wrappedBuffer(crcPrefix, data);
//// for (int pos = 0; pos < len + 4 - 2; pos++) {
//// byte b = crcAll.readByte();
//// if (b < 0) {
//// crc ^= (int) b + 256; // XOR byte into least sig. byte of
//// } else {
//// crc ^= (int) b; // XOR byte into least sig. byte of crc
//// }
////
//// for (int i = 8; i != 0; i--) { // Loop over each bit
//// if ((crc & 0x0001) != 0) { // If the LSB is set
//// crc >>= 1; // Shift right and XOR 0xA001
//// crc ^= 0xA001;
//// } else
//// // Else LSB is not set
//// crc >>= 1; // Just shift right
//// }
//// }
////
//// //移动data的readerIndex
//// data.readerIndex(data.readerIndex() + data.readableBytes() - 2);
//// return integerTo2Bytes(crc);
//// }
private
static
int
crcSingle
(
int
crc
,
byte
b
)
{
return
crc
;
}
// /**
// * 校验
// *
// * @param data
// * @return
// */
// public static boolean checkCRC(ByteBuf data) {
// byte[] crc = CRC(data);
//// Log.info("crc:" + crc[0] + "," + crc[1]);
// return crc != null && crc[0] == data.readByte() && crc[1] == data.readByte();
// }
//
// public static byte[] getBytes(String str, int length) {
// ByteBuffer byteBuffer = ByteBuffer.allocate(length);
// byte[] src = str.getBytes();
// int dataLength = src.length < length ? src.length : length;
// byteBuffer.put(src, 0, dataLength);
// while (dataLength < length) {
// byteBuffer.put((byte) 0x00);
// dataLength++;
// }
// return byteBuffer.array();
// }
}
\ No newline at end of file
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 @@
...
@@ -234,28 +234,7 @@
android:layout_alignParentBottom=
"true"
android:layout_alignParentBottom=
"true"
android:layout_marginBottom=
"@dimen/dp_20"
android:layout_marginBottom=
"@dimen/dp_20"
android:gravity=
"center_horizontal"
android:gravity=
"center_horizontal"
android:orientation=
"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>
</RelativeLayout>
</RelativeLayout>
\ No newline at end of file
build.gradle
View file @
d07f0383
...
@@ -8,11 +8,16 @@ buildscript {
...
@@ -8,11 +8,16 @@ buildscript {
}
}
dependencies
{
dependencies
{
classpath
'com.android.tools.build:gradle:4.0.1'
classpath
'com.android.tools.build:gradle:4.0.1'
classpath
'com.jakewharton:butterknife-gradle-plugin:9.0.0'
classpath
'com.jakewharton:butterknife-gradle-plugin:9.0.0'
}
}
}
}
allprojects
{
allprojects
{
repositories
{
repositories
{
google
()
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