1

I'm trying to write an app that will wake up my computer. I think I have the solution but for some reason its not working.

The script below is one class. Thats called with the mac and broadcastip from the mainactivity:

Wake.wakeup(broadcastIP, mac);


public class Wake {

BroadcastIP and mac will be strings.

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

The package should be calculated good.

        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, 9);
            DatagramSocket socket = new DatagramSocket();
            socket.send(packet);
            socket.close();

        }
        catch (Exception e) {
        }
    }

The conversion from the mac should be good to. This is called when the wakeup is intented.

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

}

I appreciate every help/hint you can give me.

acj
  • 4,821
  • 4
  • 34
  • 49
crancker
  • 11
  • 1
  • 2
    Are you seeing any errors in Logcat? Have you checked the byte array returned by 'getMacBytes()' to verify that the MAC is being parsed correctly? – acj Oct 23 '12 at 13:54
  • The mac is parsed correctly. And in Logcat there is no entry that would show an error. Also there is no trace in the logcat that shows that the package is sent or not. – crancker Oct 23 '12 at 14:21
  • Is the packet being sent over a wifi connection? – acj Oct 23 '12 at 14:29
  • yes. This should work only from local network. – crancker Oct 23 '12 at 14:35
  • Ok. The code looks reasonable to me. Have you been able to do Wake-on-LAN with this specific computer before? If not, you may want to post on ServerFault to get help with the hardware/OS configuration. – acj Oct 23 '12 at 14:44
  • Yes I can WOL may pc. At first I used ether-wake applet from my android device, but that requires busybox... and a root permisson. Thats why I try to implement WOL into my app. – crancker Oct 23 '12 at 14:46

0 Answers0