首页 > 其他分享 >Wifi BSSID获取

Wifi BSSID获取

时间:2023-02-02 18:47:36浏览次数:41  
标签:java BSSID app Wifi ActivityThread 获取 return android os

代码很简单,通过wifiManager 获取wifiinfo,从而获取bssid,

  public static String getWifiSSID(Context context) {
        String bssid = "";
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
        if (manager == null) {
            return bssid;
        }

        NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting() && activeNetworkInfo.getType() == TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);
            if (wifiManager == null) {
                return bssid;
            }
            WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            bssid = connectionInfo.getBSSID();
        }
        return bssid;
    }

几个注意的地方吧:

1:getActiveNetworkInfo方法时需要添加权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2:直接通过上述方法执行时,会抛出RuntimeException:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zh.wifiinfotest/com.zh.wifiinfotest.MainActivity}: java.lang.SecurityException: WifiService: Neither user 10138 nor current process has android.permission.ACCESS_WIFI_STATE.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3915)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4098)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2475)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8401)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1087)
     Caused by: java.lang.SecurityException: WifiService: Neither user 10138 nor current process has android.permission.ACCESS_WIFI_STATE.
        at android.os.Parcel.createException(Parcel.java:2082)
        at android.os.Parcel.readException(Parcel.java:2050)
        at android.os.Parcel.readException(Parcel.java:1998)
        at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:2374)
        at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:2338)
        at com.zh.wifiinfotest.MainActivity.getWifiSSID(MainActivity.java:39)
        at com.zh.wifiinfotest.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:8121)
        at android.app.Activity.performCreate(Activity.java:8109)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3888)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4098) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2475) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:219) 
        at android.app.ActivityThread.main(ActivityThread.java:8401) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1087) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at android.app.ContextImpl.enforce(ContextImpl.java:2025)
        at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:2053)
        at com.android.server.wifi.WifiServiceImpl.enforceAccessPermission(WifiServiceImpl.java:1130)
        at com.android.server.wifi.WifiServiceImpl.getConnectionInfo(WifiServiceImpl.java:2740)
        at android.net.wifi.IWifiManager$Stub.onTransact(IWifiManager.java:1047)
    callee: null 4649/12306

添加权限android.permission.ACCESS_WIFI_STATE。

3:继续执行。这次获取到了bssid,但是取得的值却是默认值:02:00:00:00:00:00。

这是什么问题?

我们看下获取wifiInfo的方法:

    /**
     * Return dynamic information about the current Wi-Fi connection, if any is active.
     * <p>
     * In the connected state, access to the SSID and BSSID requires
     * the same permissions as {@link #getScanResults}. If such access is not allowed,
     * {@link WifiInfo#getSSID} will return {@link #UNKNOWN_SSID} and
     * {@link WifiInfo#getBSSID} will return {@code "02:00:00:00:00:00"}.
     * {@link WifiInfo#getPasspointFqdn()} will return null.
     * {@link WifiInfo#getPasspointProviderFriendlyName()} will return null.
     *
     * @return the Wi-Fi information, contained in {@link WifiInfo}.
     */
    public WifiInfo getConnectionInfo() {
        try {
            return mService.getConnectionInfo(mContext.getOpPackageName(),
                    mContext.getAttributionTag());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

可以看到注释中有这么一句话,获取bssid和ssid需要getScanResults相同的权限。

   /**
     * Return the results of the latest access point scan.
     * @return the list of access points found in the most recent scan. An app must hold
     * {@link android.Manifest.permission#ACCESS_FINE_LOCATION ACCESS_FINE_LOCATION} permission
     * in order to get valid results.
     */
    public List<ScanResult> getScanResults() {
        try {
            return mService.getScanResults(mContext.getOpPackageName(),
                    mContext.getAttributionTag());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

好吧,需要ACCESS_FINE_LOCATION权限。

添加权限后,获得bssid.

标签:java,BSSID,app,Wifi,ActivityThread,获取,return,android,os
From: https://www.cnblogs.com/zhjing/p/17087024.html

相关文章

  • 从xml读取gps数据获取经纬高
         #!/usr/bin/python#-*-coding:UTF-8-*-fromxml.dom.minidomimportparseimportxml.dom.minidom"""数据输入xmlname文件名字no......
  • 10、获取一个网页的源码
    #获取一个网页的源码https://www.baidu.com/s?wd=%E5%91%A8%E6%9D%B0%E4%BC%A6importurllib.requesturl='https://www.baidu.com/s?wd='#要将周杰伦变为Unicode......
  • 获取txt文件的所有行
    1、获取txt文件的所有行/***获取txt的所有行*@authorxxx*@date2023/2/216:09*@paramfile*@returnList<String>*/......
  • json格式数据,淘宝1688京东商品详情获取,便携式API接口
    商品详情API接口,万邦数据返回值说明,商品详情,关键词搜索,价格监控,卖家买家订单等相关数据接口商品详情获取 测试接口 json格式数据 "item":{ "num_iid":"583191269......
  • 单元测试中获取Method参数为 arg0
    for(Parameterparameter:parameters){if(!parameter.isNamePresent()){System.out.println("NON");continue;}Stringname=paramete......
  • vue 获取 DOM 元素的方法
    1.原生js获取DOM节点document.querySelector(选择器)document.getElementById(id选择器)document.getElementsByClassName(class选择器)....2.vue2中获取当前组......
  • 获取地址最后一个值
    functiongetCaption(url,parameter){varindex=url.lastIndexOf(parameter);url=url.substring(index+1,url.length);retu......
  • QT修改、获取系统时间,以及设置日期
    设置系统时间:QStringm="date-s"2007-08-0314:15:00""QProcess::startDetached(m);//0312表示3月12日,1025表示当前时分QProcess::startDetached("hwclock-w");/......
  • es获取basic license
    #获取临时licensecurl-XPOST"http://elasticsearch:9200/_license/start_basic?acknowledge=true"#查看license是否注册curl-XGET-uuser:password'http://hadoop101:9......
  • jdk的keytool生成jks和获取jks的信息,公匙
     1.生成jks。执行命令:keytool-genkeypair-aliasmytest-keyalg RSA-keypassmypass-keystoremytest.jks-storepassmypass-keystorejks文件保存路径生成......