我使用了一个 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
?
50 1c是起始标志
04 80是命令
00 00是一个状态
ce 01 是 ssid 列表的长度
如果上面的列表是常数,就像应用协议缓冲区一样,那么你可以解析字节数组,并打印数值.检查解析半径包。
你没有说在哪里 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"));
}
}
最后,我拿起了 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) {
// make it visabled
String test = BytesUtil.bytesToHexString(byteRecv);
// replace the space
test = test.replace(" ", "");
// get rid of the head data
test = test.substring(16);
String temp = "";
// split the ssid,add "," between every ssid item
for (int i = 0; i < test.length(); i++) {
if (i % 66 == 0 && i != 0) {
temp += ",";
}
temp += test.substring(i, i + 1);
}
// put it into the string array
String temps[] = temp.split(",");
for (int i = 0; i < temps.length; i++) {
if (temps[i].endsWith("00")) {
continue;
}
int l = temps[i].length();
// get the hex ssid name
String ssidName = temps[i].substring(0, l - 2);
// delete all of 0x00 at the end
ssidName = BytesUtil.deleteLastZero(ssidName);
// hex ssid name to string
ssidName = BytesUtil.toStringHex(ssidName);
if ("".equals(ssidName)) {
continue;
}
// get the hex ApPower
String signal = temps[i].substring(l - 2, l);
// hex to int
signal = Integer.parseInt(signal, 16) + "";
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