android.net.wifi.WifiManager.getConnectionInfo()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(319)

本文整理了Java中android.net.wifi.WifiManager.getConnectionInfo()方法的一些代码示例,展示了WifiManager.getConnectionInfo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WifiManager.getConnectionInfo()方法的具体详情如下:
包路径:android.net.wifi.WifiManager
类名称:WifiManager
方法名:getConnectionInfo

WifiManager.getConnectionInfo介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

代码示例来源:origin: stackoverflow.com

WifiManager wifiMan = (WifiManager) this.getSystemService(
        Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();

代码示例来源:origin: stackoverflow.com

public class WifiReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

   NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
   if(info != null && info.isConnected()) {
    // Do your work. 

    // e.g. To check the Network Name or other info:
    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ssid = wifiInfo.getSSID();
   }
  }
}

代码示例来源:origin: stackoverflow.com

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();

代码示例来源:origin: amitshekhariitbhu/Android-Debug-Database

public static String getAddressLog(Context context, int port) {
  WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
  @SuppressLint("DefaultLocale")
  final String formattedIpAddress = String.format("%d.%d.%d.%d",
      (ipAddress & 0xff),
      (ipAddress >> 8 & 0xff),
      (ipAddress >> 16 & 0xff),
      (ipAddress >> 24 & 0xff));
  return "Open http://" + formattedIpAddress + ":" + port + " in your browser";
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnWifiInfo() {
 assertThat(wifiManager.getConnectionInfo().getClass()).isEqualTo(WifiInfo.class);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnMacAddress() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 shadowOf(wifiInfo).setMacAddress("mac address");
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getMacAddress()).isEqualTo("mac address");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnSSID() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 shadowOf(wifiInfo).setSSID("SSID");
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getSSID()).contains("SSID");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnRssi() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 shadowOf(wifiInfo).setRssi(10);
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getRssi()).isEqualTo(10);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = LOLLIPOP)
public void shouldReturnFrequency() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getFrequency()).isEqualTo(-1);
 shadowOf(wifiInfo).setFrequency(10);
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getFrequency()).isEqualTo(10);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = Build.VERSION_CODES.KITKAT)
public void connect_setsNetworkId_shouldHasNetworkId() throws Exception {
 // WHEN
 wifiManager.connect(123, null);
 // THEN
 assertThat(wifiManager.getConnectionInfo().getNetworkId()).isEqualTo(123);
}

代码示例来源:origin: robolectric/robolectric

@Test
 public void shouldReturnSupplicantState() {
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();

  shadowOf(wifiInfo).setSupplicantState(SupplicantState.COMPLETED);

  wifiInfo = wifiManager.getConnectionInfo();
  assertThat(wifiInfo.getSupplicantState()).isEqualTo(SupplicantState.COMPLETED);

  shadowOf(wifiInfo).setSupplicantState(SupplicantState.DISCONNECTED);

  wifiInfo = wifiManager.getConnectionInfo();
  assertThat(wifiInfo.getSupplicantState()).isEqualTo(SupplicantState.DISCONNECTED);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnLinkSpeed() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getLinkSpeed()).isEqualTo(-1);
 shadowOf(wifiInfo).setLinkSpeed(10);
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getLinkSpeed()).isEqualTo(10);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnBSSID() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getBSSID()).isEqualTo(null);
 shadowOf(wifiInfo).setBSSID("BSSID");
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getBSSID()).isEqualTo("BSSID");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnNetworkId() {
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getNetworkId()).isEqualTo(-1);
 shadowOf(wifiInfo).setNetworkId(10);
 wifiInfo = wifiManager.getConnectionInfo();
 assertThat(wifiInfo.getNetworkId()).isEqualTo(10);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = Build.VERSION_CODES.KITKAT)
public void connect_setsConnectionInfo() throws Exception {
 // GIVEN
 WifiConfiguration wifiConfiguration = new WifiConfiguration();
 wifiConfiguration.SSID = "foo";
 // WHEN
 wifiManager.connect(wifiConfiguration, null);
 // THEN
 assertThat(wifiManager.getConnectionInfo().getSSID()).contains("foo");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldReturnIpAddress() throws Exception {
 String ipAddress = "192.168.0.1";
 int expectedIpAddress = 16820416;
 WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 shadowOf(wifiInfo).setInetAddress(InetAddress.getByName(ipAddress));
 assertThat(wifiInfo.getIpAddress()).isEqualTo(expectedIpAddress);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void setWifiInfo_shouldUpdateWifiInfo() {
 WifiInfo wifiInfo = new WifiInfo();
 shadowOf(wifiManager).setConnectionInfo(wifiInfo);
 assertThat(wifiManager.getConnectionInfo()).isSameAs(wifiInfo);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void
  getConnectionInfo_shouldThrowSecurityExceptionWhenAccessWifiStatePermissionNotGranted()
    throws Exception {
 shadowOf(wifiManager).setAccessWifiStatePermission(false);
 try {
  wifiManager.getConnectionInfo();
  fail("SecurityException not thrown");
 } catch (SecurityException e) {
  // expected
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void reconnect_setsConnectionInfo() {
 // GIVEN
 WifiConfiguration wifiConfiguration = new WifiConfiguration();
 wifiConfiguration.SSID = "SSID";
 int netId = wifiManager.addNetwork(wifiConfiguration);
 wifiManager.enableNetwork(netId, false);
 // WHEN
 wifiManager.reconnect();
 // THEN
 assertThat(wifiManager.getConnectionInfo().getSSID()).contains("SSID");
}

相关文章