前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android获取已连接wifi的热点信息

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

作者头像
杨源鑫
发布2019-07-04 17:35:24
3.1K0
发布2019-07-04 17:35:24
举报
文章被收录于专栏:嵌入式开发圈嵌入式开发圈

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

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

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

1、设置用户权限

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

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

2、画界面,添加布局

代码语言:javascript
复制
 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代码

代码语言:javascript
复制
 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这样偷懒的方法就可以避免去输入这样的麻烦步骤了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 嵌入式开发圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、设置用户权限
  • 2、画界面,添加布局
  • 3、编写Android代码
  • 4、连接手机,运行apk到手机上
相关产品与服务
物联网
腾讯连连是腾讯云物联网全新商业品牌,它涵盖一站式物联网平台 IoT Explorer,连连官方微信小程序和配套的小程序 SDK、插件和开源 App,并整合腾讯云内优势产品能力,如大数据、音视频、AI等。同时,它打通腾讯系 C 端内容资源,如QQ音乐、微信支付、微保、微众银行、医疗健康等生态应用入口。提供覆盖“云-管-边-端”的物联网基础设施,面向“消费物联”和 “产业物联”两大赛道提供全方位的物联网产品和解决方案,助力企业高效实现数字化转型。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档