Android Wake On Lan in Java
Asked Answered
K

2

1

i'm trying to make a Wake On Lan application in Java (for Android), so I searched, and found this code:

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;
}


}

I tried it on my PC, it worked, but it doesn't work for Android :( I tried to edit it, but still nothing :( Here's my code for 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) {
    }

}
}

Everytime I enter the ip and mac adress it does nothing. So, can you help me, please? :) Yeah, and sorry for my english :D

Kistler answered 24/6, 2013 at 11:5 Comment(2)
Does your android device has a real ethernet port? Or do you try to use Wake on LAN via WLAN?Sanatorium
I use my Android phone connected on wifiKistler
I
1

I'm not shure about rest of the code, but you should use String mac = mactext.getText().toString(); instead of String mac = mactext.toString(); cause without this change you converting to string entire EditText not just its value. P.S. i know its 10 months after question, still for anyone who would come here looking for WOL android code it should be usefull

Incoordinate answered 8/5, 2014 at 13:39 Comment(0)
L
0

I know it's a little bit late, but i also tried a long time to get this java code working on android. The solution is to use the broadcast ip. That's all. Cheers j

Leekgreen answered 6/5, 2015 at 16:42 Comment(2)
Could you please add more details about the solution you provide?Talky
Just use the broadcast ip of the router instead of the computers network ip. Broadcast: en.wikipedia.org/wiki/Broadcast_address Calculation tool: petenetlive.com/KB/Article/0000215.htmLeekgreen

© 2022 - 2024 — McMap. All rights reserved.