Commit d07f0383 by 韩飞虎

用jar 方式引入的

parent 1c3b6ca2
...@@ -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'
......
...@@ -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);
......
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
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
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
...@@ -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
...@@ -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()
......
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