我使用byte[512]
从Java中的套接字中恢复数据
我使用函数bytesToHexString
使其可见
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv + " ");
}
return stringBuilder.toString();
}
它看起来像那样:
50 1c 04 80 00 00 ce 01 4d 6f 64 69 6d 20 66 61 73 68 69 6f 6e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 64 33 36 30 57 69 46 69 2d 36 38 38 38 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 50 74 69 6d 65 6c 65 73 73 67 7a 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 4b 77 7a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 74 69 6d 65 6c 65 73 73 67 7a 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 39 72 69 6e 67 69 65 72 67 75 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 39 73 77 65 65 74 20 62 61 62 79 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 34 4a 49 4d 4d 59 20 33 47 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 42 65 72 74 2d 43 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 54 50 2d 4c 49 4e 4b 5f 43 42 44 30 34 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 25 43 68 69 6e 61 4e 65 74 2d 47 50 69 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 23 43 68 69 6e 61 4e 65 74 2d 55 6c 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 62 6c 75 65 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 95 bd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
我所知道的是C中的这个结构:
typedef struct _xchip_cmd_head {
u16 flag; //always 50 1c
u16 cmd; // commands, return cmd=cmd|0x8000
u16 cmd_status; //return result
u16 datalen;
u8 data[1];
}xchip_cmd_head_t;
50 1c
是开始标志
04 80
是命令
00 00
是状态
ce 01
是数据的长度。
以及ce 01
是ssid的列表:
typedef struct _ApList_str
{
char ssid[32];
char ApPower; // min:0, max:100
}ApList_str;
那么如何在Java中获取ssid列表呢?
我做了一些搜索,有没有办法使用ByteBuffer
?
答案 0 :(得分:0)
50 1c是开始标志
04 80是命令
00 00是状态
ce 01是ssid列表长度
如果上面的列表是常量,与应用程序协议缓冲区类似,那么您可以解析bytearray并打印值。 检查半径包的解析。
答案 1 :(得分:0)
你不会说ApPower
编码的位置,但是否则这个类会做你想要的。
public class ApList_str {
private final String ssid;
private int ApPower = -1; /* Unknown */
public ApList_str(String ssid) {
this.ssid = ssid;
}
public String getSsid() {
return ssid;
}
public int getApPower() {
return ApPower;
}
public void setApPower(int apPower) {
ApPower = apPower;
}
public static ApList_str fromBytes(ByteBuffer b) throws UnsupportedEncodingException {
byte[] ssid_bytes = new byte[32];
b.get(ssid_bytes);
/* You should change the encoding depending on your situation */
return new ApList_str(new String(ssid_bytes, "UTF-8"));
}
}
答案 2 :(得分:0)
最后我通过这个函数拿起了ssid列表,
看起来很愚蠢,但它确实有效,
我想找到更好的方法:
public static List<String> getSsidList(byte[] byteRecv) {
System.out.println("get from device");
List<String> ssidListOpt = new ArrayList<String>();
try {
if (byteRecv[0] != 0) {
String test = BytesUtil.bytesToHexString(byteRecv);//make it visabled
test = test.replace(" ", "");// replace the space
test = test.substring(16);//get rid of the head data
String temp = "";
for (int i = 0; i < test.length(); i++) { //split the ssid,add "," between every ssid item
if (i % 66 == 0 && i != 0) {
temp += ",";
}
temp += test.substring(i, i + 1);
}
String temps[] = temp.split(",");//put it into the string array
for (int i = 0; i < temps.length; i++) {
if (temps[i].endsWith("00")) {
continue;
}
int l = temps[i].length();
String ssidName = temps[i].substring(0, l - 2);//get the hex ssid name
ssidName = BytesUtil.deleteLastZero(ssidName);//delete all of 0x00 at the end
ssidName = BytesUtil.toStringHex(ssidName);//hex ssid name to string
if ("".equals(ssidName)) {
continue;
}
String signal = temps[i].substring(l - 2, l);//get the hex ApPower
signal = Integer.parseInt(signal, 16) + "";//hex to int
String t[] = { ssidName, signal };
System.out.println("" + t[0] + "------" + t[1]);
ssidListOpt.add(ssidName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return ssidListOpt;
}
这是我在LogCat中得到的:
02-06 15:06:40.398: I/System.out(20947): Modim fashion------100
02-06 15:06:40.398: I/System.out(20947): tessgz2------80
02-06 15:06:40.398: I/System.out(20947): sweet baby------67
02-06 15:06:40.398: I/System.out(20947): wz------62
02-06 15:06:40.408: I/System.out(20947): Bert-Co------45
02-06 15:06:40.408: I/System.out(20947): ChinaNet-GPi6------40
02-06 15:06:40.408: I/System.out(20947): ringierguest------40
02-06 15:06:40.408: I/System.out(20947): tlessgz1------32
02-06 15:06:40.408: I/System.out(20947): JIMMY 3G------32
02-06 15:06:40.408: I/System.out(20947): blue1------30
02-06 15:06:40.408: I/System.out(20947): ChinaNet-Ul6------25
02-06 15:06:40.408: I/System.out(20947): wudiuandiuan------15