Sample code for 30+ languages & platforms
Android™

Get Local Network Adapter Addresses

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetAdaptersAddresses, which populates a JsonObject with information about local network adapters, including IPv4 addresses, IPv6 addresses, and MAC addresses.

Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.

Background. This provides a portable way to enumerate the host's network interfaces and their addresses.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    CkSocket socket = new CkSocket();

    //  Populate a JsonObject with information about the local network adapters, including IPv4 addresses,
    //  IPv6 addresses, and MAC addresses.
    CkJsonObject json = new CkJsonObject();
    success = socket.GetAdaptersAddresses(json);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    json.put_EmitCompact(false);
    String jsonStr = json.emit();
    Log.i(TAG, jsonStr);
    //  JSON parsing code for this result can be generated at Chilkat's online tool:
    //  https://tools.chilkat.io/jsonParse

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}