{"id":2063,"date":"2010-09-19T12:33:16","date_gmt":"2010-09-19T16:33:16","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2063"},"modified":"2010-09-19T12:33:16","modified_gmt":"2010-09-19T16:33:16","slug":"android-programming-how-to-obtain-the-wifis-corresponding-networkinterface","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/android-programming-how-to-obtain-the-wifis-corresponding-networkinterface\/","title":{"rendered":"Android: How to obtain the WiFi&#8217;s corresponding NetworkInterface"},"content":{"rendered":"<p>Let&#8217;s say for some odd reason in the world you do need to get the corresponding NetworkInterface object of the Wifi on your android, in my case I needed to have my WiFi device send multicast packets, and I wanted my MulticastSocket to only send packets through the WiFi device (not 3g, or maybe even ethernet). The android API does not provide functionality to know what &#8220;NetworkInterface&#8221; your WiFi is using.<\/p>\n<p>Here&#8217;s a solution proven in tens of different android phones, seems to work 100%.<\/p>\n<p>[java]<br \/>\npublic static NetworkInterface getWifiNetworkInterface(WifiManager manager) {<\/p>\n<p>    Enumeration&lt;NetworkInterface&gt; interfaces = null;<br \/>\n    try {<br \/>\n        \/\/the WiFi network interface will be one of these.<br \/>\n        interfaces = NetworkInterface.getNetworkInterfaces();<br \/>\n    } catch (SocketException e) {<br \/>\n        return null;<br \/>\n    }<\/p>\n<p>    \/\/We&#8217;ll use the WiFiManager&#8217;s ConnectionInfo IP address and compare it with<br \/>\n    \/\/the ips of the enumerated NetworkInterfaces to find the WiFi NetworkInterface.<\/p>\n<p>    \/\/Wifi manager gets a ConnectionInfo object that has the ipAdress as an int<br \/>\n    \/\/It&#8217;s endianness could be different as the one on java.net.InetAddress<br \/>\n    \/\/maybe this varies from device to device, the android API has no documentation on this method.<br \/>\n    int wifiIP = manager.getConnectionInfo().getIpAddress();<\/p>\n<p>    \/\/so I keep the same IP number with the reverse endianness<br \/>\n    int reverseWifiIP = Integer.reverseBytes(wifiIP); \t\t<\/p>\n<p>    while (interfaces.hasMoreElements()) {<\/p>\n<p>        NetworkInterface iface = interfaces.nextElement();<\/p>\n<p>        \/\/since each interface could have many InetAddresses&#8230;<br \/>\n        Enumeration&lt;InetAddress&gt; inetAddresses = iface.getInetAddresses();<br \/>\n        while (inetAddresses.hasMoreElements()) {<br \/>\n            InetAddress nextElement = inetAddresses.nextElement();<br \/>\n            int byteArrayToInt = byteArrayToInt(nextElement.getAddress(),0);<\/p>\n<p>            \/\/grab that IP in byte[] form and convert it to int, then compare it<br \/>\n            \/\/to the IP given by the WifiManager&#8217;s ConnectionInfo. We compare<br \/>\n            \/\/in both endianness to make sure we get it.<br \/>\n            if (byteArrayToInt == wifiIP || byteArrayToInt == reverseWifiIP) {<br \/>\n                return iface;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return null;<br \/>\n}<\/p>\n<p>public static final int byteArrayToInt(byte[] arr, int offset) {<br \/>\n    if (arr == null || arr.length &#8211; offset &lt; 4)<br \/>\n        return -1;<\/p>\n<p>    int r0 = (arr[offset] &amp; 0xFF) &lt;&lt; 24;<br \/>\n    int r1 = (arr[offset + 1] &amp; 0xFF) &lt;&lt; 16;<br \/>\n    int r2 = (arr[offset + 2] &amp; 0xFF) &lt;&lt; 8;<br \/>\n    int r3 = arr[offset + 3] &amp; 0xFF;<br \/>\n    return r0 + r1 + r2 + r3;<br \/>\n}<br \/>\n[\/java] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say for some odd reason in the world you do need to get the corresponding NetworkInterface object of the Wifi on your android, in my case I needed to have my WiFi device send multicast packets, and I wanted my MulticastSocket to only send packets through the WiFi device (not 3g, or maybe even [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,15],"tags":[1434,282],"class_list":["post-2063","post","type-post","status-publish","format-standard","hentry","category-android","category-code","tag-android","tag-coding"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-xh","jetpack-related-posts":[{"id":2873,"url":"https:\/\/www.gubatron.com\/blog\/get-frostwire-1-0-for-android\/","url_meta":{"origin":2063,"position":0},"title":"Get FrostWire 1.0 for Android","author":"gubatron","date":"November 22, 2012","format":false,"excerpt":"If you are getting an Android device this holiday season, we've spent the last 5 months building a must-have app for it. Install FrostWire via Google Play. Download the .apk installer. Here's a few things you can do with our completely re-designed and easy to use file sharing client and\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1556,"url":"https:\/\/www.gubatron.com\/blog\/ipad-ipass\/","url_meta":{"origin":2063,"position":1},"title":"iPad, iPass","author":"gubatron","date":"January 27, 2010","format":false,"excerpt":"Beautiful device, beautiful looking software but way too expensive for the minimum storage capabilities that a multimedia\/gaming device should have. If you're giving me a great screen for video, powerful graphic processing capabilities and only WiFI connectivity you must think I will want to have a few movies, pictures and\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2557,"url":"https:\/\/www.gubatron.com\/blog\/how-babies-watch-cartoons-in-2011\/","url_meta":{"origin":2063,"position":2},"title":"How babies watch cartoons in 2011","author":"gubatron","date":"November 10, 2011","format":false,"excerpt":"Here's my 18 month old, watching Polish cartoons on YouTube right after waking up. Think of all the technology* that had to come together to make this happen and how technology helps to preserve and spread culture. Just 10 years ago this wasn't possible. She would've had to be in\u2026","rel":"","context":"In &quot;Ayer Hoy y Ma\u00c3\u00b1ana&quot;","block_context":{"text":"Ayer Hoy y Ma\u00c3\u00b1ana","link":"https:\/\/www.gubatron.com\/blog\/category\/ayer-hoy-y-manana\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":377,"url":"https:\/\/www.gubatron.com\/blog\/zune-and-wifi-enabled-mp3-players-will-finish-the-radio\/","url_meta":{"origin":2063,"position":3},"title":"Zune and Wifi Enabled MP3 Players will finish the Radio.","author":"gubatron","date":"October 3, 2006","format":false,"excerpt":"Some people say FM radio is already dead, but still its a medium where a lot of companies put their money. I want to write this post to make a little more emphasis on why I think the Wifi feature of the Zune portable media player should be the hottest\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":433,"url":"https:\/\/www.gubatron.com\/blog\/iphone-si-es-lo-que-promete-seria-el-telefono-perfecto\/","url_meta":{"origin":2063,"position":4},"title":"iPhone, si es lo que promete, seria el telefono perfecto","author":"gubatron","date":"January 9, 2007","format":false,"excerpt":"O por lo menos el mejor telefono o pda que ha salido hasta el momento. Analizemos el potencial de este aparato con nombrar solo algunas de sus caracteristicas, y obviando toda la innovacion de la pantalla multitouch, y de que va a tener hasta 8gb de almacenamiento. Corre MacOS X\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3020,"url":"https:\/\/www.gubatron.com\/blog\/how-to-avoid-android-compilationbuilding-issues-setting-up-the-eclipse-buildpath-correctly\/","url_meta":{"origin":2063,"position":5},"title":"How to avoid Android compilation\/building issues &#8211; Setting up the Eclipse Buildpath correctly","author":"gubatron","date":"May 21, 2013","format":false,"excerpt":"This post is the product of about 48 hours of not being able to work on what I was supposed to be working because I had to deal with build issues in Eclipse. Hopefully If you follow these steps you will be able to solve some of the issues you\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"Screen Shot 2013-05-21 at 12.13.46 PM","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2063"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2063\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}