title: Phonegap Network Connection - Cannot read property 'type' of undefined
body: <p>I have been searching around all day for the answer to this and the Google-sphere has not provided any answers. I have tried everything I know how to do and worked through the suggested solutions and answers and nothing has worked.</p>
  
  <p>In a nutshell I am trying to develop a <code>Phonegap</code> app for <code>Android</code> and <code>Apple</code> Mobile devices and one of the features I need is to detect both the network status and the type of network connection. Below is the code I am using.</p>
  
  <p>The <code>Firing device ready</code> alert fires off and then I get the error <code>Cannot read property 'type' of undefined</code> followed by the looping through of the <code>Navigator</code> object. As I go through each of these properties of the object I do not see the <code>connection</code> property or even the <code>network</code> property as was used in older versions.</p>
  
  <p>Anyone have any ideas?</p>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  
  &lt;!-- jQuery Core --&gt;
  &lt;script src="js/jquery-1.11.1.min.js"&gt;&lt;/script&gt;
  &lt;!-- The main engine for the software. --&gt;
  &lt;script src="js/main.js"&gt;&lt;/script&gt;
  
  &lt;!-- Third party plugins --&gt;
  &lt;script type="text/javascript" src="cordova.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="barcodescanner.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="childbrowser.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/barcode.js"&gt;&lt;/script&gt;
  
  &lt;title&gt;index&lt;/title&gt;
  
  &lt;script&gt;
  document.addEventListener("deviceready", onDeviceReady, false);
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  
  &lt;script&gt;
  barcode_app.initialize();
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>main.js</strong></p>
  
  <pre><code>function onDeviceReady(){
      alert('Firing device ready');
      try{
          var networkState = navigator.connection.type;
  
          var states = {};
          states[Connection.UNKNOWN]  = 'Unknown connection';
          states[Connection.ETHERNET] = 'Ethernet connection';
          states[Connection.WIFI]     = 'WiFi connection';
          states[Connection.CELL_2G]  = 'Cell 2G connection';
          states[Connection.CELL_3G]  = 'Cell 3G connection';
          states[Connection.CELL_4G]  = 'Cell 4G connection';
          states[Connection.NONE]     = 'No network connection';
  
          $("#system_popup").html('Connection type: ' + states[networkState]);
          $("#system_popup").popup("open")
          //alert('Connection type: ' + states[networkState]);
      }catch(e){
          alert(e);
          $.each(navigator, function(key, value){
              alert(key+' =&gt; '+value);
          });
      }
  }
  </code></pre>
  
  <p>And in my config.xml I have:</p>
  
  <pre><code>&lt;plugin
          name="NetworkStatus"
          value="org.apache.cordova.NetworkManager" /&gt;
  
  &lt;gap:config-file platform="android" parent="/manifest"&gt;
          &lt;uses-permission name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;/gap:config-file&gt;
  
      &lt;gap:config-file platform="android" parent="/manifest"&gt;
          &lt;uses-permission name="android.permission.INTERNET" /&gt;
      &lt;/gap:config-file&gt;
  
      &lt;gap:config-file platform="android" parent="/manifest"&gt;
          &lt;uses-permission name="android.permission.READ_PHONE_STATE" /&gt;
      &lt;/gap:config-file&gt;
  
  &lt;feature name="http://api.phonegap.com/1.0/device" /&gt;
      &lt;feature name="NetworkStatus"&gt;
          &lt;param name="android-package" value="org.apache.cordova.NetworkManager" /&gt;
          &lt;param name="ios-package" value="CDVConnection" /&gt;
      &lt;/feature&gt;
  </code></pre>
  
  <p><BR><BR><BR><BR>
  <HR>
  <BR><BR><BR><BR></p>
  
  <p><strong>UPDATE : SOLUTION</strong></p>
  
  <p>A solution was finally formulated through the combined efforts of @Dawson Loudon and @benka. Dawson corrected the plugin I was using which should have been:</p>
  
  <pre><code>&lt;gap:plugin
          name="org.apache.cordova.network-information"
          version="0.2.7" /&gt;
  </code></pre>
  
  <p>And this really only works correctly after implementing the short delay mentioned by @benka. So now the working code looks like this in JavaScript:</p>
  
  <pre><code>function onDeviceReady(){
      try{
          var networkState = navigator.connection &amp;&amp; navigator.connection.type;
  
          setTimeout(function(){
              networkState = navigator.connection &amp;&amp; navigator.connection.type;
  
              var states = {};
              states[Connection.UNKNOWN]  = 'Unknown connection';
              states[Connection.ETHERNET] = 'Ethernet connection';
              states[Connection.WIFI]     = 'WiFi connection';
              states[Connection.CELL_2G]  = 'Cell 2G connection';
              states[Connection.CELL_3G]  = 'Cell 3G connection';
              states[Connection.CELL_4G]  = 'Cell 4G connection';
              states[Connection.NONE]     = 'No network connection';
  
              alert('Connection type: ' + states[networkState]);
          }, 500);
      }catch(e){
          alert(e);
          $.each(navigator, function(key, value){
              alert(key+' =&gt; '+value);
          });
      }
  }
  </code></pre>