1

I would like to send data to my server from a Raspberry.

The Raspberry is connected by Wi-Fi to a network, and by Ethernet to my server.

When I try to ping my server (at 198.168.1.30) I must specify the interface :

ping -I eth0 198.168.1.30 otherwise, I have an error message : Network unreachable.

In my Android Things App, I have the same error. I think I have to "say" to my raspberry to send the request on eth0.

Is it possible to specify on which interface we want to contact an address on Android Things App ?

0

As far as I can tell, you want to use the ConnectivityManager. It has a method called [requestNetwork](https://developer.android.com/reference/android/net/ConnectivityManager.html#requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback)) which lets you select the type of network you want based on a NetworkRequest object which lets you filter available interfaces based on properties like TRANSPORT_ETHERNET.

You will get a callback with your Network object, which you can then use to create a URLConnection. You will probably get back an HttpURLConnection (or an HttpsURLConnection) and then you can follow the code snippets there to see how to make a basic request to a server.

  • Thank you ! By the way, don't you know how to make an Android Thing answer to ping requests please ? – Maxime.D Jan 30 '18 at 8:33
  • I don't know for ping. It does not connect over normal ssh. – Nick Felker Jan 30 '18 at 18:31
  • I used ConnectivityManager.getActiveNetworkInfo(); and displayed the result in my app log, to know more about the current network. It appears that my ActiveNetwork is : [type: Ethernet[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: b8:27:eb:b3:07:30, failover: false, available: true, roaming: false, metered: false] Do I need to use requestNetwork() ? – Maxime.D Jan 31 '18 at 10:10
  • I'm sorry to unpick your answer but it doesn't change anything to use ConnectivityManager.requestNetwork. Moreover, I need to use UDP and URLConnection works only with HTTP – Maxime.D Jan 31 '18 at 11:32
  • There's both HttpURLConnection and HttpsURLConnection for either protocol type. – Nick Felker Jan 31 '18 at 17:57
0

So I found the problem : The OS, Android things, cannot hanlde two interfaces at the same time. The problem is not comming from the app.

Android Things needs to get its IP address from a DHCP server : you can try to assign a static address with ifconfig conmmand, but Android Things won't use the interface anyway.
Android will use : the first interface connected who had its IP through a DHCP service. If both Ethernet and Wi-Fi are available at boot, Wi-Fi gets the priority.
Lastly, if you connect Wi-Fi (with DHCP) after Ethernet, the Android Things device will use the Wi-Fi but only for answering ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.