2016-03-21 84 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

相關問題