purplecabbage / phonegap-plugins

Plugins for use with PhoneGap.
972 stars 3.82k forks source link

MacAddress plugin incompatible with Cordova 2.7.0 #120

Open obgrseneca opened 11 years ago

obgrseneca commented 11 years ago

The title says it, the MacAddress plugin seems to be incompatible with newer cordova release. The first error I am getting is, that "org.apache.cordova.api.Plugin" does not exist. The rest are errors dependant on this one.

themightychris commented 11 years ago

@obgrseneca, here's a fixed version of the Java plugin for cordova 2.7, I'm not sure what the JS plugin should look like I ended up just calling cordova.exec directly. The new plugin API doesn't seem to support synchronous calls anymore so it's async now:

package com.phonegap.plugin.macaddress;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.wifi.WifiManager;

/**
 * The Class MacAddressPlugin.
 */
public class MacAddressPlugin extends CordovaPlugin {

    /* (non-Javadoc)
     * @see org.apache.cordova.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
     */
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

        if (action.equals("getMacAddress")) {
            String macAddress = this.getMacAddress();
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, macAddress);
            callbackContext.sendPluginResult(pluginResult);
            return true;
        }

        return false;
    }

    /**
     * Gets the mac address.
     * 
     * @return the mac address
     */
    private String getMacAddress() {
        String macAddress = null;
        WifiManager wm = (WifiManager) cordova.getActivity().getSystemService(Context.WIFI_SERVICE);
        macAddress = wm.getConnectionInfo().getMacAddress();

        if (macAddress == null || macAddress.length() == 0) {
            macAddress = "00:00:00:00:00:00";
        }

        return macAddress;
    }
}