我正在尝试让应用程序在lan pc上唤醒。
在这个页面中我找到了一个java示例: https://www.sistemasorp.es/2005/06/13/wake-on-lan-y-magic-packet/ 但我对如何获取mac-address字节感到困惑。
如何获取字节?
在网页中:
**byte** mac[]={0x01,0x02,0x03,0x04,0x05,0x06};
在这个问题中: (How to convert a Mac Address to a Hex and pass it to a bytearray in java)
**Byte**[] macAddressBytes = new Byte[6];
...
Integer hex = Integer.parseInt(macAddressParts[i], 16);
macAddressBytes[i] = hex.byteValue();
(字节与字节)
我执行此代码:
String output="";
byte mac1_1[]={0x01,0x02,0x03,0x04,0x05,0x06};
output+="mac1_1= "+mac1_1.toString()+"\n";
Byte mac1_2[]={0x01,0x02,0x03,0x04,0x05,0x06};
output+="mac1_2= "+mac1_2.toString()+"\n";
String mac2String="010203040506";
byte mac2_1[]=mac2String.getBytes();
output+="mac2_1= "+mac2_1.toString()+"\n";
String mac3 = "01:02:03:04:05:06";
String[] macAddressParts = mac3.split(":");
byte[] mac3_1 = new byte[6];
Byte[] mac3_2 = new Byte[6];
for(int i=0; i<6; i++){
Integer hex = Integer.parseInt(macAddressParts[i], 16);
mac3_1[i] = hex.byteValue();
mac3_2[i] = hex.byteValue();
}
output+="mac3_1= "+mac3_1.toString()+"\n";
output+="mac3_2= "+mac3_2.toString()+"\n";
System.out.println(output);
我得到了这个结果:
mac1_1= [B@106d69c
mac1_2= [Ljava.lang.Byte;@52e922
mac2_1= [B@25154f
mac3_1= [B@10dea4e
mac3_2= [Ljava.lang.Byte;@647e05
所有字节都不同!
有人知道哪个是正确的?
答案 0 :(得分:0)
要从您的设备获取MAC,请使用WifiManager
类
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
然后
String mac = wifiManager.getConnectionInfo().getMacAddress()
现在只需从mac变量
中调用方法getBytes(string)
即可
从android获取mac anddres仅适用于android api&lt; = 21,对于Android M你可以看到这个更改日志