package com.pusherman.networkinfo;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.annotation.NonNull;
import android.util.Log;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class RNNetworkInfo extends ReactContextBaseJavaModule {
    WifiManager wifi;

    public static final String TAG = "RNNetworkInfo";

    public RNNetworkInfo(ReactApplicationContext reactContext) {
        super(reactContext);

        wifi = (WifiManager) reactContext.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public String getName() {
        return TAG;
    }

    @ReactMethod
    public void getSSID(final Callback callback) {
        WifiInfo info = wifi.getConnectionInfo();

        // This value should be wrapped in double quotes, so we need to unwrap it.
        String ssid = info.getSSID();
        if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
            ssid = ssid.substring(1, ssid.length() - 1);
        }

        callback.invoke(ssid);
    }

    @ReactMethod
    public void getBSSID(final Callback callback) {
        callback.invoke(wifi.getConnectionInfo().getBSSID());
    }

    @ReactMethod
    public void getBroadcast(/*@NonNull String ip, */final Callback callback) {
        String ipAddress = null;

        for (InterfaceAddress address : getInetAddresses()) {
            if (!address.getAddress().isLoopbackAddress()/*address.getAddress().toString().equalsIgnoreCase(ip)*/) {
                ipAddress = address.getBroadcast().toString();
            }
        }

        callback.invoke(ipAddress);
    }

    @ReactMethod
    public void getIPAddress(final Callback callback) {
        String ipAddress = null;

        for (InterfaceAddress address : getInetAddresses()) {
            if (!address.getAddress().isLoopbackAddress()) {
                ipAddress = address.getAddress().getHostAddress().toString();
            }
        }

        callback.invoke(ipAddress);
    }

    @ReactMethod
    public void getIPV4Address(final Callback callback) {
        String ipAddress = null;

        for (InterfaceAddress address : getInetAddresses()) {
            if (!address.getAddress().isLoopbackAddress() && address.getAddress() instanceof Inet4Address) {
                ipAddress = address.getAddress().getHostAddress().toString();
            }
        }

        callback.invoke(ipAddress);
    }


    private List<InterfaceAddress> getInetAddresses() {
        List<InterfaceAddress> addresses = new ArrayList<>();
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();

                for (InterfaceAddress interface_address : intf.getInterfaceAddresses()) {
                    addresses.add(interface_address);
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.toString());
        }
        return addresses;
    }
}

@@ -175,16 +175,57 @@
 onNull;%0A
+import android.net.wifi.SupplicantState;%0A
 import a
@@ -1308,22 +1308,173 @@
         
-String
+// https://stackoverflow.com/a/34848930/5732760%0A        String ssid = null;%0A        if (info.getSupplicantState() == SupplicantState.COMPLETED) %7B%0A           
  ssid = 
@@ -1485,24 +1485,28 @@
 .getSSID();%0A
+    
         if (
@@ -1561,24 +1561,28 @@
             
+    
 ssid = ssid.
@@ -1614,16 +1614,30 @@
 ) - 1);%0A
+            %7D%0A
         
@@ -1758,59 +1758,303 @@
     
-callback.invoke(wifi.getConnectionInfo().getBSSID()
+WifiInfo info = wifi.getConnectionInfo();%0A%0A        // https://stackoverflow.com/a/34848930/5732760%0A        String bssid = null;%0A        if (info.getSupplicantState() == SupplicantState.COMPLETED) %7B%0A            bssid = wifi.getConnectionInfo().getBSSID();%0A        %7D%0A%0A        callback.invoke(bssid
 );%0A 

Check SupplicantState Of WifiInfo