2016-03-21 56 views
0

我正试图让一个应用在局域网中唤醒。获取字节mac地址在局域网上唤醒

在此页面中,我找到了一个java示例: https://www.sistemasorp.es/2005/06/13/wake-on-lan-y-magic-packet/ 但我很困惑如何获取mac地址字节。

如何获取字节?

在网页:

**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(); 

(字节VS字节)

我这样做代码:

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= [[email protected] 

mac1_2= [Ljava.lang.Byte;@52e922 

mac2_1= [[email protected] 

mac3_1= [[email protected] 

mac3_2= [Ljava.lang.Byte;@647e05 

所有字节都不一样!

有人知道哪一个是正确的?

+1

由于被的toString打印出对象的散列码,而不是值。 –

+0

是的,现在我看到... –

回答

0

要从设备获得MAC使用WifiManager

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

然后

String mac = wifiManager.getConnectionInfo().getMacAddress()

现在只需从你的Mac可变

获取调用该方法getBytes(string)来自android的macanddres只适用于android api < = 21,对于版本的Android M可以看到此更改日志

http://developer.android.com/intl/es/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id

相关问题