1. Introduction
This AppStudio blog describes how we can use the Networking component to detect for network connectivity and classify the type of network (LAN, WIFI, Mobile Data). This allows you to build apps that can react differently dependent on what type of network they're on, e.g. ask the user whether they want to download content whilst on an expensive mobile data network.
2. Minimal App - All Network Configurations
Let's start with a minimal app that shows all Network connections whether active or inactive:
<SPAN class="keyword token">import</SPAN> QtQuick <SPAN class="number token">2.7</SPAN>
<SPAN class="keyword token">import</SPAN> QtQuick<SPAN class="punctuation token">.</SPAN>Controls <SPAN class="number token">2.1</SPAN>
<SPAN class="keyword token">import</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>AppFramework <SPAN class="number token">1.0</SPAN>
<SPAN class="keyword token">import</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>AppFramework<SPAN class="punctuation token">.</SPAN>Networking <SPAN class="number token">1.0</SPAN>
App <SPAN class="punctuation token">{</SPAN>
width<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">640</SPAN> <SPAN class="operator token">*</SPAN> AppFramework<SPAN class="punctuation token">.</SPAN>displayScaleFactor
height<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">480</SPAN> <SPAN class="operator token">*</SPAN> AppFramework<SPAN class="punctuation token">.</SPAN>displayScaleFactor
Flickable <SPAN class="punctuation token">{</SPAN>
anchors<SPAN class="punctuation token">.</SPAN>fill<SPAN class="punctuation token">:</SPAN> parent
contentWidth<SPAN class="punctuation token">:</SPAN> textArea<SPAN class="punctuation token">.</SPAN>width
contentHeight<SPAN class="punctuation token">:</SPAN> textArea<SPAN class="punctuation token">.</SPAN>height
TextArea <SPAN class="punctuation token">{</SPAN>
id<SPAN class="punctuation token">:</SPAN> textArea
text<SPAN class="punctuation token">:</SPAN> JSON<SPAN class="punctuation token">.</SPAN><SPAN class="token function">stringify</SPAN><SPAN class="punctuation token">(</SPAN>Networking<SPAN class="punctuation token">.</SPAN>configurations<SPAN class="punctuation token">,</SPAN> undefined<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>On my Windows machine, I get:
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"0"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"objectName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"valid"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch) 2"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"identifier"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"312537767"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeFamily"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Ethernet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"configurationType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"connectTimeout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"purpose"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"roamingAvailable"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"state"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">14</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"1"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"objectName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"valid"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (VMware Virtual Ethernet Adapter for VMnet8 Virtual Switch)"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"identifier"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"287176532"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeFamily"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Ethernet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"configurationType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"connectTimeout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"purpose"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"roamingAvailable"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"state"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">14</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"2"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"objectName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"valid"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (Default Switch)"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"identifier"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"287176527"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeFamily"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Ethernet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"configurationType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"connectTimeout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"purpose"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"roamingAvailable"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"state"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">14</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"3"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"objectName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"valid"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (VMware Virtual Ethernet Adapter for VMnet1 Virtual Switch)"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"identifier"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"312537825"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeFamily"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Ethernet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"configurationType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"connectTimeout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"purpose"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"roamingAvailable"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"state"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">14</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"4"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"objectName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"valid"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Bluetooth Network Connection"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"identifier"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"312537798"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeFamily"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Ethernet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"configurationType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"connectTimeout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"purpose"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"roamingAvailable"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"state"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">2</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"5"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"objectName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"valid"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (Intel(R) 82579LM Gigabit Network Connection Virtual Switch)"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"identifier"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"312537802"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeFamily"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"bearerTypeName"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Ethernet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"configurationType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"connectTimeout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">30000</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"purpose"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"roamingAvailable"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"state"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">14</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The things we observed about Networking.configurations are:
- it's an object, not an array,
- every connection is listed (both active and inactive)
- there are signals on Network.configurations so you can use it in property binding scenarios (i.e. the moment the network changes, e.g. device is put in airplane mode, your app automatically updates)
3. Active Network Configurations
In order to filter the list down quickly, we transform the object to an array, then filter the results using Array.prototype.filter:
<SPAN class="keyword token">import</SPAN> QtQuick <SPAN class="number token">2.7</SPAN>
<SPAN class="keyword token">import</SPAN> QtQuick<SPAN class="punctuation token">.</SPAN>Controls <SPAN class="number token">2.1</SPAN>
<SPAN class="keyword token">import</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>AppFramework <SPAN class="number token">1.0</SPAN>
<SPAN class="keyword token">import</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>AppFramework<SPAN class="punctuation token">.</SPAN>Networking <SPAN class="number token">1.0</SPAN>
App <SPAN class="punctuation token">{</SPAN>
width<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">640</SPAN> <SPAN class="operator token">*</SPAN> AppFramework<SPAN class="punctuation token">.</SPAN>displayScaleFactor
height<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">480</SPAN> <SPAN class="operator token">*</SPAN> AppFramework<SPAN class="punctuation token">.</SPAN>displayScaleFactor
Flickable <SPAN class="punctuation token">{</SPAN>
anchors<SPAN class="punctuation token">.</SPAN>fill<SPAN class="punctuation token">:</SPAN> parent
contentWidth<SPAN class="punctuation token">:</SPAN> textArea<SPAN class="punctuation token">.</SPAN>width
contentHeight<SPAN class="punctuation token">:</SPAN> textArea<SPAN class="punctuation token">.</SPAN>height
TextArea <SPAN class="punctuation token">{</SPAN>
id<SPAN class="punctuation token">:</SPAN> textArea
text<SPAN class="punctuation token">:</SPAN> JSON<SPAN class="punctuation token">.</SPAN><SPAN class="token function">stringify</SPAN><SPAN class="punctuation token">(</SPAN>
<SPAN class="token function">toArray</SPAN><SPAN class="punctuation token">(</SPAN>Networking<SPAN class="punctuation token">.</SPAN>configurations<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">filter</SPAN><SPAN class="punctuation token">(</SPAN>isConfigActive<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">map</SPAN><SPAN class="punctuation token">(</SPAN>summary<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
undefined<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">toArray</SPAN><SPAN class="punctuation token">(</SPAN>o<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> Object<SPAN class="punctuation token">.</SPAN><SPAN class="token function">keys</SPAN><SPAN class="punctuation token">(</SPAN>o<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">map</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN> <SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">return</SPAN> o<SPAN class="punctuation token">[</SPAN>e<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigActive</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">.</SPAN>state <SPAN class="operator token">&</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>StateActive<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">===</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>StateActive<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">summary</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">{</SPAN>
name<SPAN class="punctuation token">:</SPAN> c<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN>
bearerType<SPAN class="punctuation token">:</SPAN> c<SPAN class="punctuation token">.</SPAN>bearerType
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>On my Windows machine, this reduces to an easier to read list:
<SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch) 2"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (VMware Virtual Ethernet Adapter for VMnet1 Virtual Switch)"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (Default Switch)"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (Intel(R) 82579LM Gigabit Network Connection Virtual Switch)"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vEthernet (VMware Virtual Ethernet Adapter for VMnet8 Virtual Switch)"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>On my Android device, when WIFI is enabled:
<SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"WIFI"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>or, when Mobile Data is enabled:
<SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Mobile"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>On my iOS device, when WIFI is enabled:
<SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"en0"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"awdl0"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"bearerType"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"name"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"utun0"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>4. Determining if the Network is LAN, WIFI or MobileData
On Windows, Android and Linux the bearerType can be used to classify the network connection, e.g.
- NetworkConfiguration.BearerEthernet
- NetworkConfiguration.BearerWLAN
- NetworkConfiguraiton.Bearer3G
Whilst on iOS and macOS, the bearerType may not be provided by the operating system resulting in NetworkConfiguration.BearerUnknown. To cover iOS and macOS, we note that the name of the configuration instead:
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigLAN</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">.</SPAN>bearerType <SPAN class="operator token">===</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerEthernet<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>Qt<SPAN class="punctuation token">.</SPAN>platform<SPAN class="punctuation token">.</SPAN>os <SPAN class="operator token">===</SPAN> <SPAN class="string token">"osx"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> c<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">===</SPAN> <SPAN class="string token">"en0"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigWIFI</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">.</SPAN>bearerType <SPAN class="operator token">===</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerWLAN<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>Qt<SPAN class="punctuation token">.</SPAN>platform<SPAN class="punctuation token">.</SPAN>os <SPAN class="operator token">===</SPAN> <SPAN class="string token">"ios"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> c<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">===</SPAN> <SPAN class="string token">"en0"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>Qt<SPAN class="punctuation token">.</SPAN>platform<SPAN class="punctuation token">.</SPAN>os <SPAN class="operator token">===</SPAN> <SPAN class="string token">"osx"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> c<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">===</SPAN> <SPAN class="string token">"awdl0"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigMobileData</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">switch</SPAN> <SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">.</SPAN>bearerType<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>Bearer2G<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerCDMA2000<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerWCDMA<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerHSPA<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerWiMAX<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerEVDO<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>BearerLTE<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>Bearer3G<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">case</SPAN> NetworkConfiguration<SPAN class="punctuation token">.</SPAN>Bearer4G<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>Qt<SPAN class="punctuation token">.</SPAN>platform<SPAN class="punctuation token">.</SPAN>os <SPAN class="operator token">===</SPAN> <SPAN class="string token">"ios"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> c<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">===</SPAN> <SPAN class="string token">"pdp_ip0"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigActiveLAN</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token function">isConfigActive</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">&&</SPAN> <SPAN class="token function">isConfigLAN</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigActiveWIFI</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token function">isConfigActive</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">&&</SPAN> <SPAN class="token function">isConfigWIFI</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">isConfigActiveMobileData</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token function">isConfigActive</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">&&</SPAN> <SPAN class="token function">isConfigMobileData</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Now that we have these useful functions, we can quickly chain them together to determine whether we're on WIFI or MobileData. Because we convert the object to an array, we can make use of Array.prototype.some to quickly lookup a network configuration of a particular type:
property <SPAN class="keyword token">var</SPAN> config<SPAN class="punctuation token">:</SPAN> <SPAN class="token function">toArray</SPAN><SPAN class="punctuation token">(</SPAN>Networking<SPAN class="punctuation token">.</SPAN>configurations<SPAN class="punctuation token">)</SPAN>
property bool isOnline<SPAN class="punctuation token">:</SPAN> isLAN <SPAN class="operator token">||</SPAN> isWIFI <SPAN class="operator token">||</SPAN> isMobileData
property bool isLAN<SPAN class="punctuation token">:</SPAN> config<SPAN class="punctuation token">.</SPAN><SPAN class="token function">some</SPAN><SPAN class="punctuation token">(</SPAN>isConfigActiveLAN<SPAN class="punctuation token">)</SPAN>
property bool isWIFI<SPAN class="punctuation token">:</SPAN> config<SPAN class="punctuation token">.</SPAN><SPAN class="token function">some</SPAN><SPAN class="punctuation token">(</SPAN>isConfigActiveWIFI<SPAN class="punctuation token">)</SPAN>
property bool isMobileData<SPAN class="punctuation token">:</SPAN> config<SPAN class="punctuation token">.</SPAN><SPAN class="token function">some</SPAN><SPAN class="punctuation token">(</SPAN>isConfigActiveMobileData<SPAN class="punctuation token">)</SPAN>
property bool isMobileDataOnly<SPAN class="punctuation token">:</SPAN> isMobileData <SPAN class="operator token">&&</SPAN> <SPAN class="operator token">!</SPAN>isWIFI <SPAN class="operator token">&&</SPAN> <SPAN class="operator token">!</SPAN>isLAN<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
5. Network Check Sample
The "Network Check" app is available for you to try which demonstrates all of the above points. You can find "Network Check" app in AppStudio.
- Launch AppStudio
- Select New App
- Click Search Icon
- Type: Network Check

The "Network Check" source code is also avilable at arcgis-appstudio-samples GitHub.