Adobe Air - Detect if connection is WIFI, 3G, or E

2019-03-25 21:01发布

问题:

I need to determine which connection type a device is using. Distinguishing between WIFI and 3G doesn't seem to be a problem on iOS (using the NetworkInfo ANE) and Android (using the native NetworkInfo class) but I've got no clue how to further distinguish between a fast (3G, 4G) and slow (EDGE) connection. Is there a way to do this with Adobe Air?

回答1:

Try this for detecting Mobile vs. Wifi on iOS. This requires the Adobe native extension "NetworkInfo"

import com.adobe.nativeExtensions.Networkinfo.InterfaceAddress;
import com.adobe.nativeExtensions.Networkinfo.NetworkInfo;
import com.adobe.nativeExtensions.Networkinfo.NetworkInterface;

var vNetworkInterfaces:Object; 
if (flash.net.NetworkInfo.isSupported) 
{ 
  vNetworkInterfaces = getDefinitionByName('flash.net.NetworkInfo')['networkInfo']['findInterfaces'](); 
  mytrace("fall 1" );
} 
else 
{ 
  vNetworkInterfaces = getDefinitionByName('com.adobe.nativeExtensions.Networkinfo.NetworkInfo')['networkInfo']['findInterfaces']();
  mytrace("fall 2" );
} 

var hasWifi: Boolean = false;
var hasMobile: Boolean = false;

for each (var networkInterface:Object in vNetworkInterfaces) 
{ 
    if ( networkInterface.active && (networkInterface.name == "en0" || networkInterface.name == "en1") ) hasWifi = true;
    if ( networkInterface.active && (networkInterface.name == "pdp_ip0" || networkInterface.name == "pdp_ip1" || networkInterface.name == "pdp_ip2") ) hasMobile = true;

    mytrace( "active: " + networkInterface.active );
    mytrace( "displayName: " + networkInterface.displayName );
    mytrace( "name: " + networkInterface.name );
    mytrace( "hwAddress: " + networkInterface.hardwareAddress );
    mytrace( "--------------------" ); 
} 

mytrace( "has Mobile Internet: " + hasMobile );
mytrace( "has Wifi Internet: " + hasWifi );


回答2:

I dunno if there's a solution a direct solution but when you're downloading a file you could check the speed at which file is downloaded, by using getTimer function in conjunction with loaded bytes and a ratio of total bytes.

Example

function init()
{
    startTime = getTimer();
    loader = new Loader();
    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderInit);
    loader.load("some-site.com/myasset.png");
}

function onLoaderInit(e:ProgressEvent)
{
    $timePassed = (getTimer() - startTime) * 0.001; //it's in millisecounds convert to secounds
    $newBytes = loader.contentLoaderInfo.bytesLoaded - oldBytes; 
    var $rate:Number = $newBytes * $timePassed; //returns a value of bytes per sec.
    startTime = getTimer();
    oldBytes = loader.contentLoaderInfo.bytesLoaded;    
}


回答3:

Nice... if it works... dunno if there's a solution a direct solution but when you're downloading a file you could check the speed at which file is downloaded, by using getTimer function in conjunction with loaded bytes and a ratio of total bytes