随机数匹配逻辑调整

This commit is contained in:
Lihongda 2024-03-07 19:59:28 +08:00
parent 9ef9c1e10b
commit f62606bd96
3 changed files with 60 additions and 36 deletions

View File

@ -87,7 +87,7 @@ public class AirKissUtil {
private DatagramSocket sendSocket = null;
ProgressDialog mDialog = null;
private static final int REPLY_BYTE_CONFIRM_TIMES = 5;
private static final int REPLY_BYTE_CONFIRM_TIMES = 3;
public void init(AppCompatActivity activity) {
wifiUtil = new WifiUtil(activity);
@ -142,18 +142,16 @@ public class AirKissUtil {
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("start");
byte[] DUMMY_DATA = new byte[1500];
AirKissEncoder airKissEncoder = new AirKissEncoder(ssid, password);
DatagramSocket sendSocket = null;
try {
sendSocket = new DatagramSocket();
sendSocket.setBroadcast(true);
int encoded_data[] = airKissEncoder.getEncodedData();
for (int i = 0; i < encoded_data.length; ++i) {
int[] encoded_data = airKissEncoder.getEncodedData();
for (int encoded_datum : encoded_data) {
DatagramPacket pkg = new DatagramPacket(DUMMY_DATA,
encoded_data[i],
encoded_datum,
InetAddress.getByName("255.255.255.255"),
10000);
// LogUtils.d("LHD 发送的包encoded_datum = "+ encoded_data[i]);
sendSocket.send(pkg);
Thread.sleep(4);
}
@ -203,7 +201,6 @@ public class AirKissUtil {
mDialog.setMessage("正在接受设备信息,请耐心等待...");
mDialog.setCancelable(false);
mDialog.show();
// receiveUDPPackage(curActivity);
}
}
}, 5000);
@ -243,16 +240,23 @@ public class AirKissUtil {
@Override
public void onNext(String s) {
if ("success".equals(s)) {
Toast.makeText(curActivity, "配网成功:", Toast.LENGTH_SHORT).show();
// Toast.makeText(curActivity, "配网成功:", Toast.LENGTH_SHORT).show();
}
}
});
}
private void receivePackage(AirKissEncoder airKissEncoder, Subscriber subscriber) {
byte[] buffer = new byte[15000];
byte[] buffer = new byte[7];
DatagramSocket udpServerSocket = null;
char mRandomChar = airKissEncoder.getRandomChar();//获取UDP数据包中的随机字符
// 将char类型的变量拆分为两个字节并输出它们的值
byte highByte = (byte) (mRandomChar >> 8); // 获取高字节
byte lowByte = (byte) mRandomChar; // 获取低字节
LogUtils.d("LHD 发送的随机数 = "+mRandomChar+"高字节:" + highByte+ "低字节:" + lowByte);
try {
int replyByteCounter = 0;
udpServerSocket = new DatagramSocket(10000);
@ -261,32 +265,32 @@ public class AirKissUtil {
while (true) {
udpServerSocket.receive(packet);
if(!packet.getAddress().getHostAddress().equals(WifiUtil.intToIp(wifiUtil.getConnectIpAddress()))){
if (!packet.getAddress().getHostAddress().equals(WifiUtil.intToIp(wifiUtil.getConnectIpAddress()))) {
byte[] receivedData = packet.getData();
Log.d("LHD","收到消息:"+ Arrays.toString(receivedData));
if (receivedData!=null){
for (byte b : receivedData) {
LogUtils.d("LHD dfaf = "+(char) b); // 将低字节转换为char类型);
if (b == mRandomChar){
replyByteCounter++;
}
LogUtils.d("LHD 比较 replyByteCounter = "+replyByteCounter);
}
Log.d("LHD","收到消息:"+ Arrays.toString(receivedData));
if (replyByteCounter > REPLY_BYTE_CONFIRM_TIMES) {
subscriber.onNext("success");
String mac = ByteUtil.parseBssid(
receivedData,
1,
6);
device_mac = mac;
LogUtils.d("LHD 获取到的mac地址 = "+mac);
if (!TextUtils.isEmpty(device_mac)){
LogUtils.d("LHD 获取到的mac地址 = " + mac);
if (!TextUtils.isEmpty(device_mac)) {
stopAirKiss();
doPostBindDevice();
break;
}
}
for (byte b : receivedData) {
if (b == mRandomChar)
replyByteCounter++;
}
if (replyByteCounter > REPLY_BYTE_CONFIRM_TIMES) {
subscriber.onNext("success");
break;
}
}
}
@ -317,6 +321,7 @@ public class AirKissUtil {
@Override
public void success(BaseBean<Device> baseBean) {
result = baseBean.result;
stopAirKiss();
if (result == Commons.NetWork.Success) {
deviceObj = baseBean.data;
deviceObj.setMacAddress(device_mac);

View File

@ -1,35 +1,44 @@
package com.ifish.airkiss;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Random;
//测试时需要先启动设备端程序然后在手机上发送配网信息根据周围环境条件配网速度可能或快或慢
public class AirKissEncoder {
private int mEncodedData[] = new int[2 << 14];
private int[] mEncodedData = new int[2 << 14];
private int mLength = 0;
// Random char should be in range [0, 127).
private char mRandomChar = (char)(new Random().nextInt(0x7F));
// private char mRandomChar = 'A';//固定随机数
public AirKissEncoder(String ssid, String password) {
Log.d("LHD","随机数 = "+mRandomChar);
int times = 5;
while (times-- > 0) {
leadingPart();
magicCode(ssid, password);
for (int i = 0; i < 15; ++i) {
prefixCode(password);
String data = password + mRandomChar + ssid;
try {
data = new String(data.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int index;
byte content[] = new byte[4];
for (index = 0; index < data.length() / 4; ++index) {
for (index = 0; index < data.getBytes().length / 4; ++index) {
System.arraycopy(data.getBytes(), index * 4, content, 0, content.length);
sequence(index, content);
}
if (data.length() % 4 != 0) {
content = new byte[data.length() % 4];
if (data.getBytes().length % 4 != 0) {
content = new byte[data.getBytes().length % 4];
System.arraycopy(data.getBytes(), index * 4, content, 0, content.length);
sequence(index, content);
}
}
}
@ -50,9 +59,9 @@ public class AirKissEncoder {
private int CRC8(byte data[]) {
int len = data.length;
int i = 0;
byte crc = 0x00;
int crc = 0x00;
while (len-- > 0) {
byte extract = data[i++];
int extract = data[i++];
for (byte tempI = 8; tempI != 0; tempI--) {
byte sum = (byte) ((crc & 0xFF) ^ (extract & 0xFF));
sum = (byte) ((sum & 0xFF) & 0x01);
@ -78,13 +87,20 @@ public class AirKissEncoder {
}
private void magicCode(String ssid, String password) {
int length = ssid.length() + password.length() + 1;
byte[] bSsid = ssid.getBytes();
String u8Ssid = null;
try {
u8Ssid = new String(bSsid, "UTF-8" );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int length = u8Ssid.getBytes().length+ password.length()+1;
int magicCode[] = new int[4];
magicCode[0] = 0x00 | (length >>> 4 & 0xF);
if (magicCode[0] == 0)
magicCode[0] = 0x08;
magicCode[1] = 0x10 | (length & 0xF);
int crc8 = CRC8(ssid);
int crc8 = CRC8(u8Ssid);
magicCode[2] = 0x20 | (crc8 >>> 4 & 0xF);
magicCode[3] = 0x30 | (crc8 & 0xF);
for (int i = 0; i < 20; ++i) {
@ -112,7 +128,9 @@ public class AirKissEncoder {
int crc8 = CRC8(content);
appendEncodedData(0x80 | crc8);
appendEncodedData(0x80 | index);
for (byte aData : data)
appendEncodedData(aData | 0x100);
for (byte aData : data) {
int bData = (aData & 0xff);
appendEncodedData(bData | 0x100);
}
}
}

View File

@ -15,4 +15,5 @@
10.友盟分享初始化逻辑变更,初始化分为预先初始化和正式初始化
11.下载功能适配android12+解决高版本安卓手机无法更新APK的问题
12.增加阿里云镜像版本控制移除local.pro
12.增加阿里云镜像版本控制移除local.pro
13.增加airkiss注意在发送前AirKissEncoder只能实例化一次确保发送的随机数和接受的时候用来比较的随机数要保持一致