Skip to main content
Inspiring
July 21, 2011
Answered

Getting iPad or Android device MAC address doesn't work (works in AIR Desktop)

  • July 21, 2011
  • 9 replies
  • 10614 views

I'm trying to get the MAC address of the user's device in Adobe AIR. This is the code I'm using.

public function getDeviceMac():String {

     return NetworkInfo.networkInfo.findInterfaces()[0].hardwareAddress.toString();

}

That function works ok in the PC (or when testing in the PC with ADL Mobile),

but in the iPad 2 or in an Android device (Acer Iconia A500) I get an error

about the property not existing. I suppose it's not implemented in the mobile

versions of AIR. It would be nice if it were implemented in future versions.

Is there any workaround for this?

I'm just seeking a way to uniquely identify a device, so is there

an alternative unique identifier I can get from AIR programatically

(maybe the UDID?), if it's not possible to get the MAC?

Thanks for your help!

This topic has been closed for replies.
Correct answer Pahup

Hi,

You'll have to iterate throught all the interfaces available and then find the hardwareAddress.

NetworkInfo.networkInfo.findInterfaces()[1].hardwareAddress.toString( );

Also, for Android, you'll have to specify ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE permissions also in your application descriptor as following.

<android>

        <manifestAdditions><![CDATA[

     <manifest android:installLocation="auto">

   <uses-permission android:name="android.permission.INTERNET"/>

   <!--The ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE permissions should be toggled

together in order to use AIR's NetworkInfo APIs-->

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

</manifest>

]]></manifestAdditions>

    </android>

-Pahup

9 replies

Participant
December 2, 2011

Hello,

i am also facing such type of problem in detecting the netowrk state in ipad.

In my application there are two modes available

1. Online

2. Offline

I was using the NetworkInfo.networkInfo.findInterfaces() API to find weather my user is in Online mode or in Offline mode. This API is working very well in my Android build however when i install app in ipad, my app was not able to find any network state.

Can you please suggest me a way by which i can find this in ipad or in iphone?

You can find the code here :

var interfaces:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();

                for(var i:uint = 0; i < interfaces.length; i++)

                {

                    if(interfaces.name.toLowerCase() == "wifi" && interfaces.active) {

                        GlobalVaribales.IS_OFFLINE = false;

                        break;

                    } else if(interfaces.name.toLowerCase() == "mobile" && interfaces.active) {

                        GlobalVaribales.IS_OFFLINE = false;

                        break;

                    }

                    else if(interfaces.displayName == 'Local Area Connection' && interfaces.active)

                    {

                        GlobalVaribales.IS_OFFLINE = false;

                        break;

                    }

                   

                }

OMA2kAuthor
Inspiring
January 2, 2012

Hi neel_229. With the new Native Extensions feature of AIR 3.0+ you can use the NetworkInterface native extension for iOS to get the same functionality:

http://www.adobe.com/es/devnet/air/native-extensions-for-air/extensions/networkinfo.html

The process to get it working is a little convoluted but it works.

Take a look at this page to learn how to use Native Extensions:

http://help.adobe.com/en_US/air/build/WS597e5dadb9cc1e0253f7d2fc1311b491071-8000.html

Since this extension creates a class with the same name as the existing flash.net.NetworkInfo

it will collide if you want to use both (for cross-platform compatibility in iOS and Android

with the same code). I've managed to use both by not importing their classes

but calling them by their fully qualified path and using generic Objects for the vectors.

See below:

var vNetworkInterfaces:Object;

if (flash.net.NetworkInfo.isSupported)

{

      trace('Getting MAC from NetworkInfo (AIR)');

      vNetworkInterfaces = getDefinitionByName('flash.net.NetworkInfo')['networkInfo']['findInterfaces']();

}

else

{

      trace('Getting MAC from NetworkInfo (ANE)');

      vNetworkInterfaces = getDefinitionByName('com.adobe.nativeExtensions.Networkinfo.NetworkInfo')['networkInfo']['findInterfaces']();

}

for each (var networkInterface:Object in vNetworkInterfaces)

{

      // Do your networkInterface stuff here. Use the notation networkInterface['property'] to avoid warnings

}

Hope this helps.

Inspiring
March 3, 2012

i'm getting

ArgumentError: Error #3500: The extension context does not have a method with the name getInterfaces.

          at flash.external::ExtensionContext/call()

          at com.adobe.nativeExtensions.Networkinfo::NetworkInfo/findInterfaces()[/Users/gangwar/Documents/Adobe Flash Builder 4.5/NetworkInfoActionScriptLibrary/src/com/adobe/nativeExtensions/Networkinfo/NetworkInfo.as:70]

          at com.adobe.nativeExtensions.Networkinfo::NetworkInfo/findInterfaces()

Anyone else getting this?

Pahup
Adobe Employee
PahupCorrect answer
Adobe Employee
July 22, 2011

Hi,

You'll have to iterate throught all the interfaces available and then find the hardwareAddress.

NetworkInfo.networkInfo.findInterfaces()[1].hardwareAddress.toString( );

Also, for Android, you'll have to specify ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE permissions also in your application descriptor as following.

<android>

        <manifestAdditions><![CDATA[

     <manifest android:installLocation="auto">

   <uses-permission android:name="android.permission.INTERNET"/>

   <!--The ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE permissions should be toggled

together in order to use AIR's NetworkInfo APIs-->

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

</manifest>

]]></manifestAdditions>

    </android>

-Pahup

OMA2kAuthor
Inspiring
July 22, 2011

Thanks for your answer. I already had both of those permissions enabled in AIR Android, but I assumed since both iPad and Android devices I'm targetting are WiFi only (no 3G), that there would be only one network interface. I've tried looking for all network interfaces, and I could find the MAC of the Android device, but could not do the same in the iPad 2.

This is the code I've used:

var vNetworkInterfaces:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();

for each (var networkInterface:NetworkInterface in vNetworkInterfaces) {

     try {trace(networkInterface.hardwareAddress);}

     catch (e:Error) {}

}

That way I got the MAC in Android, but still, in the iPad 2 I get this error in the first line (the one that executes "findInterfaces();"):

Error #1009: Cannot access a property or method of a null object reference.

So it seems networkInfo.findInterfaces() is not implemented in iOS. I tried getting the value of:

NetworkInfo.isSupported

And the value of that is true in Android but false in the iPad 2. So it seems NetworkInfo is not supported in iOS

Is there any other way in iOS to get a unique identifier for the device? (it doesn't have to be the MAC)

Pahup
Adobe Employee
Adobe Employee
July 23, 2011

yes, NetworkInfo on iOS AIR is not supported.

Request you to log a request for it @ ideas.adobe.com

-Thanks

Pahup