我创建了一个应用程序,它首先使用wifi p2p创建了一个群组,所以我现在有一个群组的ssid和密码,现在假设设备正在使用此ssid和密码进行连接,因此我需要此客户端设备的ip地址
MainActivity
package android.com.viram.wifihotspot;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.GroupInfoListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.os.Debug;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Method;
public class MainActivity extends Activity {
Button enableButton, disableButton, enableButton1, disableButton1;
WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
TextView status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = findViewById(R.id.status);
final WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
assert wifi != null;
wifi.setWifiEnabled(true);
status.setText("Creating Group....");
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
mManager.requestGroupInfo(mChannel, new WifiP2pManager.GroupInfoListener() {
public void onGroupInfoAvailable(final WifiP2pGroup group) {
wifi.setWifiEnabled(true);
if (group != null) {
mManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d("success", "first");
Log.d("ssid1", "" + group.getNetworkName());
Log.d("password1", "" + group.getPassphrase());
status.setText("Waiting for Portable Kit....");
}
@Override
public void onFailure(int reason) {
Log.d("first", "" + reason);
}
});
}
@Override
public void onFailure(int reason) {
Log.d("second", "" + reason);
}
});
} else {
mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d("success", "second");
Log.d("ssid2", "" + group.getNetworkName());
Log.d("password2", "" + group.getPassphrase());
status.setText("Waiting for Portable Kit....");
}
@Override
public void onFailure(int reason) {
Log.d("third", "" + reason);
}
});
}
}
});
}
@Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
BroadCast接收器类
package android.com.viram.wifihotspot;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
/**
* Created by Viram on 5/13/2019.
*/
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private Channel mChannel;
private Activity mActivity;
private boolean isWifiP2pEnabled;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
Activity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
this.isWifiP2pEnabled = false;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Determine if Wifi P2P mode is enabled or not, alert
// the Activity.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
setIsWifiP2pEnabled(true);
} else {
setIsWifiP2pEnabled(false);
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// The peer list has changed! We should probably do something about
// that.
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Connection state changed! We should probably do something about
// that.
}
else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
}
}
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}
}
我想要客户端的IP地址 有什么简单的方法可以做到