Android获取已连接wifi的热点信息

简介: Android获取已连接wifi的热点信息

   最近要做些物联网类的应用,所以就必须要把这个搞懂,才能做一些实用的物联网设备的控制。点击获取WIFI热点信息最终效果如下:

640.jpg640.jpg

对比查看手机的WIFI热点信息,开发的app获取的信息和手机是一致的。


简单的怎么创建一个Android app的工程就不说了,接下来说一下我的获取步骤:

1、设置用户权限

   因为我们要操作Android的一些管理服务,所以一定要有权限才能去操作它。打开AndroidManifest.xml,添加权限

640.jpg

 当然也可以通过界面来添加

640.jpg

2、画界面,添加布局

640.jpg

1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2    xmlns:tools="http://schemas.android.com/tools"
 3    android:layout_width="match_parent"
 4    android:layout_height="match_parent"
 5    android:paddingBottom="@dimen/activity_vertical_margin"
 6    android:paddingLeft="@dimen/activity_horizontal_margin"
 7    android:paddingRight="@dimen/activity_horizontal_margin"
 8    android:paddingTop="@dimen/activity_vertical_margin"
 9    tools:context=".MainActivity" >
10
11    <TextView
12        android:id="@+id/textView1"
13        android:layout_width="match_parent"
14        android:layout_height="wrap_content"
15        android:layout_alignLeft="@+id/button1"
16        android:layout_alignParentTop="true"
17        android:layout_marginTop="21dp"
18        android:text="@string/connect_tip" />
19
20    <Button
21        android:id="@+id/button1"
22        android:layout_width="match_parent"
23        android:layout_height="wrap_content"
24        android:layout_below="@+id/textView1"
25        android:layout_centerHorizontal="true"
26        android:layout_marginTop="27dp"
27        android:text="@string/Get_AP_INFO" />
28
29    <TextView
30        android:id="@+id/textView2"
31        android:layout_width="match_parent"
32        android:layout_height="wrap_content"
33        android:layout_alignLeft="@+id/button1"
34        android:layout_alignParentRight="true"
35        android:layout_below="@+id/button1"
36        android:layout_marginTop="30dp"
37        android:text="" />
38
39    <TextView
40        android:id="@+id/textView3"
41        android:layout_width="match_parent"
42        android:layout_height="wrap_content"
43        android:layout_alignLeft="@+id/textView2"
44        android:layout_below="@+id/textView2"
45        android:layout_marginTop="35dp"
46        android:text="" />
47
48    <TextView
49        android:id="@+id/textView4"
50        android:layout_width="match_parent"
51        android:layout_height="wrap_content"
52        android:layout_alignLeft="@+id/textView3"
53        android:layout_below="@+id/textView3"
54        android:layout_marginTop="33dp"
55        android:text="" />
56
57    <TextView
58        android:id="@+id/textView6"
59        android:layout_width="match_parent"
60        android:layout_height="wrap_content"
61        android:layout_alignLeft="@+id/textView5"
62        android:layout_centerVertical="true"
63        android:text="" />
64
65    <TextView
66        android:id="@+id/textView7"
67        android:layout_width="match_parent"
68        android:layout_height="wrap_content"
69        android:layout_alignLeft="@+id/textView6"
70        android:layout_below="@+id/textView6"
71        android:layout_marginTop="25dp" />
72
73    <TextView
74        android:id="@+id/textView5"
75        android:layout_width="match_parent"
76        android:layout_height="wrap_content"
77        android:layout_alignLeft="@+id/textView4"
78        android:layout_below="@+id/textView4"
79        android:layout_marginTop="29dp" />
80
81</RelativeLayout>

3、编写Android代码

1package com.example.android_get_ap_info;
 2
 3import android.net.DhcpInfo;
 4import android.net.wifi.WifiManager;
 5import android.os.Bundle;
 6import android.app.Activity;
 7import android.view.Menu;
 8import android.widget.Button;
 9import android.view.View;
10import android.widget.TextView;
11
12//由于点击按键需要onClick方法,而这个方法是一个接口,所以要实现该接口
13public class MainActivity extends Activity implements View.OnClickListener {
14
15    private TextView wifi_addr,wifi_mask,wifi_gateway,wifi_dns1,wifi_dns2,wifi_server ;
16    private Button Get_info ;
17
18
19    private WifiManager __WifiManager;
20    private DhcpInfo __DhcpInfo;
21
22    @Override
23    protected void onCreate(Bundle savedInstanceState) {
24        super.onCreate(savedInstanceState);
25        setContentView(R.layout.activity_main);
26
27        //获取控件ID===>R.id.xxx 就是对应布局里的控件
28        Get_info     =  (Button)findViewById(R.id.button1);
29        wifi_addr    =  (TextView)findViewById(R.id.textView2);
30        wifi_mask    =  (TextView)findViewById(R.id.textView3);
31        wifi_gateway =  (TextView)findViewById(R.id.textView4);
32        wifi_server  =  (TextView)findViewById(R.id.textView5);
33        wifi_dns1    =  (TextView)findViewById(R.id.textView6);
34        wifi_dns2    =  (TextView)findViewById(R.id.textView7);
35
36        //获取系统服务==>wifi
37        __WifiManager = ((WifiManager) getSystemService("wifi"));
38        //获取动态节点信息
39        __DhcpInfo = __WifiManager.getDhcpInfo();
40
41        //设置按钮监听
42        Get_info.setOnClickListener(this);
43    }
44
45
46    @Override
47    public boolean onCreateOptionsMenu(Menu menu) {
48        // Inflate the menu; this adds items to the action bar if it is present.
49        getMenuInflater().inflate(R.menu.main, menu);
50        return true;
51    }
52
53
54    @Override
55    public void onClick(View arg0) {
56        // TODO Auto-generated method stub
57        if(R.id.button1 ==  arg0.getId())
58        {
59            wifi_addr.setText("IP地址:" + intToIp(__DhcpInfo.ipAddress));
60            wifi_mask.setText("掩码:" + intToIp(__DhcpInfo.netmask));
61            wifi_gateway.setText("网关:" + intToIp(__DhcpInfo.gateway));
62            wifi_server.setText("服务器:" + intToIp(__DhcpInfo.serverAddress));
63            wifi_dns1.setText("DNS1:" + intToIp(__DhcpInfo.dns1));
64            wifi_dns2.setText("DNS2:" + intToIp(__DhcpInfo.dns2));
65        }
66
67    }
68
69    //转换地址
70    private String intToIp(int paramInt) {
71        return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
72                + (0xFF & paramInt >> 24);
73    }  
74}

4、连接手机,运行apk到手机上

略。

5、总结

   为什么要先知道这些东西呢?因为它可以解决我目前设计APP的一个弊端,众观市面上一些别人写的例程,通常要我去输入一个ip和端口号(一般端口号直接固定为8080),例如下图所示。为了避免人为去输入增加时间成本,使用Systemserver直接获取服务器ip这样偷懒的方法就可以避免去输入这样的麻烦步骤了。

640.jpg

目录
相关文章
|
2月前
|
Android开发
如何用Airtest脚本无线连接Android设备?
如何用Airtest脚本无线连接Android设备?
|
2月前
|
存储 网络安全 Python
WiFi连接树莓派【2月更文挑战第2天】
要通过WiFi连接树莓派并显示其摄像头视频信息,您需要按照以下步骤操作: 1. 确保您的电脑和树莓派已连接到同一个WiFi网络。 2. 安装Python和必要的库:在您的电脑上安装Python 3.6或更高版本,并安装pip(Python包管理器)。然后,通过pip安装以下库: 【2月更文挑战第2天】
24 1
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
3月前
|
定位技术 Android开发
uniapp获取wifi连接状态
uniapp获取wifi连接状态
122 0
|
4天前
|
运维 网络协议 Linux
Android 双网卡配置为连接到Android主机的PC提供外网访问(1)
Android 双网卡配置为连接到Android主机的PC提供外网访问(1)
16 0
|
5天前
|
测试技术 Android开发
Android App获取不到pkgInfo信息问题原因
Android App获取不到pkgInfo信息问题原因
14 0
|
1月前
|
前端开发 安全 数据安全/隐私保护
WPA_CLI 的介绍:介绍如何使用wpa_cli连接WiFi的方法
WPA_CLI 的介绍:介绍如何使用wpa_cli连接WiFi的方法
50 2
|
2月前
|
数据库 Android开发 数据库管理
【Android】使用android studio查看内置数据库信息
【Android】使用android studio查看内置数据库信息
76 0
|
3月前
|
开发工具 Android开发
Android获取SDK的版本信息
Android获取SDK的版本信息
39 0
|
4月前
|
XML API Android开发
Android WIFI使用简述(上)
Android WIFI使用简述(上)