uk.co.ghosty.phonegap.plugins.WakeOnLan.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.ghosty.phonegap.plugins.WakeOnLan.java

Source

/*
 Copyright 2011 Simon Yeldon
    
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package uk.co.ghosty.phonegap.plugins;

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

import org.json.JSONArray;

import android.util.Log;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class WakeOnLan extends Plugin {

    public static final String ACTION = "wake";
    public static final int PORT = 9;

    /* (non-Javadoc)
     * @see com.phonegap.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
     */
    @Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {
        // TODO Auto-generated method stub
        Log.d("WakeOnLan", "Calling plugin execute message");
        PluginResult result = null;
        if (ACTION.equals(action)) {
            try {
                String macStr = data.getString(0);
                String ipStr = data.getString(1);

                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.");
                result = new PluginResult(Status.OK, "Packet sent");
            } catch (Exception e) {
                result = new PluginResult(Status.IO_EXCEPTION);
                Log.e("WakeOnLan", "Exception thrown", e);
            }
        } else {
            result = new PluginResult(Status.INVALID_ACTION);
            Log.d("DirectoryListPlugin", "Invalid action : " + action + " passed");
        }
        return result;
    }

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

}