2013-06-24 103 views
1

我試圖做一個喚醒在Java中局域網中的應用(Android版),所以我搜索,發現這個代碼:的Android局域網喚醒在Java中

import java.io.*; 
import java.net.*; 

public class WakeOnLan { 

public static final int PORT = 9;  

public static void main(String[] args) { 

    if (args.length != 2) { 
     System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>"); 
     System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A"); 
     System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A"); 
     System.exit(1); 
    } 

    String ipStr = args[0]; 
    String macStr = args[1]; 

    try { 
     byte[] macBytes = getMacBytes(macStr); 
     byte[] bytes = new byte[6 + 16 * macBytes.length]; 
     for (int i = 0; i < 6; i++) { 
      bytes[i] = (byte) 0xff; 
     } 
     for (int i = 6; i < bytes.length; i += macBytes.length) { 
      System.arraycopy(macBytes, 0, bytes, i, macBytes.length); 
     } 

     InetAddress address = InetAddress.getByName(ipStr); 
     DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT); 
     DatagramSocket socket = new DatagramSocket(); 
     socket.send(packet); 
     socket.close(); 

     System.out.println("Wake-on-LAN packet sent."); 
    } 
    catch (Exception e) { 
     System.out.println("Failed to send Wake-on-LAN packet: + e"); 
     System.exit(1); 
    } 

} 

private static byte[] getMacBytes(String macStr) throws IllegalArgumentException { 
    byte[] bytes = new byte[6]; 
    String[] hex = macStr.split("(\\:|\\-)"); 
    if (hex.length != 6) { 
     throw new IllegalArgumentException("Invalid MAC address."); 
    } 
    try { 
     for (int i = 0; i < 6; i++) { 
      bytes[i] = (byte) Integer.parseInt(hex[i], 16); 
     } 
    } 
     catch (NumberFormatException e) { 
     throw new IllegalArgumentException("Invalid hex digit in MAC address."); 
    } 
    return bytes; 
} 


} 

我嘗試了我的電腦,它的工作,但它並不適用於Android的工作:(我想編輯它,但還是一無所獲:(這是我的Android代碼:

import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 

import com.macura.wakemypc.MainActivity; 

import com.macura.wakemypc.R; 
import com.macura.wakemypc.MainActivity; 

import com.macura.wakemypc.MainActivity; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends Activity { 
public static final int PORT = 9;  





@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
public void buttonClick(View view) { 
    EditText iptext = (EditText)findViewById(R.id.editText1); 
    EditText mactext = (EditText)findViewById(R.id.editText2); 
    String mac = mactext.toString(); 
    String broadcastIP = iptext.toString(); 
    MainActivity.wakeup(broadcastIP, mac); 
} 
private static byte[] getMacBytes(String mac) throws IllegalArgumentException { 
    // TODO Auto-generated method stub 
    byte[] bytes = new byte[6]; 
    if (mac.length() != 12) 
    { 
     throw new IllegalArgumentException("Invalid MAC address..."); 
    } 
    try { 
     String hex; 
     for (int i = 0; i < 6; i++) { 
      hex = mac.substring(i*2, i*2+2); 
      bytes[i] = (byte) Integer.parseInt(hex, 16); 
     } 
    } 
    catch (NumberFormatException e) { 
     throw new IllegalArgumentException("Invalid hex digit..."); 
    } 
    return bytes; 
} 

public static void wakeup(String broadcastIP, String mac) { 
    if (mac == null) { 
     return; 
    } 

     try { 
      byte[] macBytes = getMacBytes(mac); 
      byte[] bytes = new byte[6 + 16 * macBytes.length]; 
      for (int i = 0; i < 6; i++) { 
       bytes[i] = (byte) 0xff; 
      } 
      for (int i = 6; i < bytes.length; i += macBytes.length) { 
       System.arraycopy(macBytes, 0, bytes, i, macBytes.length); 
      } 

      InetAddress address = InetAddress.getByName(broadcastIP); 
      DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 5555); 
      DatagramSocket socket = new DatagramSocket(); 
      socket.send(packet); 
      socket.close(); 



     } 
     catch (Exception e) { 
    } 

} 
} 

每次我進入了IP和MAC地址是什麼都不做 所以,你能幫我嗎?:) 是啊,對不起我的英語:D

+0

你的android設備是否有真正的以太網端口?或者您是否嘗試通過WLAN使用Wake on LAN? –

+0

我使用我的Android手機連接上WiFi – StfgMccx

回答

1

我不是舒爾有關代碼的休息,但你應該使用的String mac = mactext.getText().toString();代替String mac = mactext.toString();原因如果沒有這種改變,你轉換爲字符串整個的EditText不只是它的價值。 P.S.我知道它的10個月後,仍然任何人會來這裏尋找WOL android代碼它應該是有用的

0

我知道這是有點晚,但我也試了很長時間才能得到這個java代碼工作機器人。解決方案是使用廣播ip。就這樣。 乾杯j

+0

請問您可以添加有關您提供的解決方案的更多詳細信息? – abarisone

+0

只需使用路由器的廣播IP而不是計算機網絡IP。 廣播:http://en.wikipedia.org/wiki/Broadcast_address 計算工具:http://www.petenetlive.com/KB/Article/0000215.htm – JuloOr