当前位置: 移动技术网 > IT编程>移动开发>Android > Android10自动连接WiFi问题的解决

Android10自动连接WiFi问题的解决

2020年05月11日  | 移动技术网IT编程  | 我要评论

陆雪涵,淳于珊珊老婆,无赖勇者的鬼畜美学动漫

说明:

本文主要说明扫码之后自动连接wifi的一些处理,扫码的流程相对简单,网上教程也比较多,对于目前android各个版本也没有太多变化。

问题描述:

最近在做项目的时候,发现以前的项目有扫描二维码自动连接wifi的功能,设备改了生成二维码的方式,然后发现手机无法自动连接wifi了。

问题原因:

经过代码调试发现:(我都是真机调试)

wifimanager.addnetwork(wificonfiguration);

在添加wifi的时候,这行代码始终返回-1,换用同事手机竟然神奇的可以连接,然后一脸蒙蔽,裂开了,不怕有问题,就怕有的有问题,有的没问题。

问题解决:

区别:我测试手机 小米10 android q(andorid 10)的系统,同事手机荣耀 android p的系统,大胆猜测是不是android 10又搞了什么奇怪的东西

根因:皇天不负有心人,上代码:

/**
   * add a new network description to the set of configured networks.
   * the {@code networkid} field of the supplied configuration object
   * is ignored.
   * <p/>
   * the new network will be marked disabled by default. to enable it,
   * called {@link #enablenetwork}.
   *
   * @param config the set of variables that describe the configuration,
   *      contained in a {@link wificonfiguration} object.
   *      if the {@link wificonfiguration} has an http proxy set
   *      the calling app must be system, or be provisioned as the profile or device owner.
   * @return the id of the newly created network description. this is used in
   *     other operations to specified the network to be acted upon.
   *     returns {@code -1} on failure.
   *
   * @deprecated
   * a) see {@link wifinetworkspecifier.builder#build()} for new
   * mechanism to trigger connection to a wi-fi network.
   * b) see {@link #addnetworksuggestions(list)},
   * {@link #removenetworksuggestions(list)} for new api to add wi-fi networks for consideration
   * when auto-connecting to wifi.
   * <b>compatibility note:</b> for applications targeting
   * {@link android.os.build.version_codes#q} or above, this api will always return {@code -1}.
   */
  @deprecated
  public int addnetwork(wificonfiguration config) {
    if (config == null) {
      return -1;
    }
    config.networkid = -1;
    return addorupdatenetwork(config);
  }

这是wifimanager.class中addnetwork方法的描述,注意注释中最后一行

{@link android.os.build.version_codes#q} or above, this api will always return {@code -1}.

android q或者更高的版本,这个方法始终返回-1,至此问题原因分析完毕,接下来开始解决:官网一顿操作:android 10 的新方案如下连接:

 代码如下:

public void test()
  {
    if (android.os.build.version.sdk_int >= android.os.build.version_codes.q)
    {
      networkspecifier specifier =
          new wifinetworkspecifier.builder()
              .setssidpattern(new patternmatcher("此处wifi名称", patternmatcher.pattern_prefix))
              .setwpa2passphrase("此处wifi密码")
              .build();
 
      networkrequest request =
          new networkrequest.builder()
              .addtransporttype(networkcapabilities.transport_wifi)
              .removecapability(networkcapabilities.net_capability_internet)
              .setnetworkspecifier(specifier)
              .build();
 
      connectivitymanager connectivitymanager = (connectivitymanager)
          context.getsystemservice(context.connectivity_service);
 
      connectivitymanager.networkcallback networkcallback = new connectivitymanager.networkcallback() {
        @override
        public void onavailable(network network) {
          // do success processing here..
        }
 
        @override
        public void onunavailable() {
          // do failure processing here..
        }
      };
      connectivitymanager.requestnetwork(request, networkcallback);
      // release the request when done.
      // connectivitymanager.unregisternetworkcallback(networkcallback);
    }
  }

注:我用的是wpa的 加密模式,亲测可用。至此完结,撒花。

到此这篇关于android10自动连接wifi问题的解决的文章就介绍到这了,更多相关android10自动连接wifi内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网