WiFi P2P导致设备WiFi自动连接功能不起作用?

Posted

技术标签:

【中文标题】WiFi P2P导致设备WiFi自动连接功能不起作用?【英文标题】:WiFi P2P causes device WiFi auto-connect feature not to work? 【发布时间】:2020-08-22 03:10:58 【问题描述】:

我的设备在范围内停止自动连接到 WiFi 网络时遇到问题。我的应用程序有一个在后台运行的服务(即使应用程序被终止),它每分钟注册和取消注册一个服务,以重复检测附近使用 WiFi P2P 的设备。

 private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() 
        @Override
        public void onReceive(Context context, Intent intent) 
            String action = intent.getAction();
            if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) 
                int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
                if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) 
                    Log.d(TAG, "WiFi P2P Enabled");
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
                        return;
                    
                    manager.discoverPeers(channel, new WifiP2pManager.ActionListener() 
                        @Override
                        public void onSuccess() 
                            Log.d(TAG, "Discover Peers Successful");
                        

                        @Override
                        public void onFailure(int i) 
                            Log.d(TAG, "Discover Peers Unsuccessful");
                        
                    );
                 else 
                    Log.d(TAG, "WiFi P2P Disabled");
                
                // Check to see if Wi-Fi is enabled and notify appropriate activity
             else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) 
                Log.d(TAG, "Peers detected");
                if (manager != null) 
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
                        return;
                    
                    Log.d(TAG, "Peers Requested");
                    manager.requestPeers(channel, peerListListener);
                
                // Call WifiP2pManager.requestPeers() to get a list of current peers
             else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) 
                Log.d(TAG, "Connection Changed");
                // Respond to new connection or disconnections
             else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) 
                Log.d(TAG, "WiFi P2P State Changed");
                // Respond to this device's wifi state changing
            
        
    ;

 WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() 
        @Override
        public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) 
            Log.d(TAG, "Peers available");
            String deviceEncrypt = "";

            if (!wifiP2pDeviceList.getDeviceList().equals(peers)) 
                peers.clear();
                peers.addAll(wifiP2pDeviceList.getDeviceList());

                deviceNameArray = new String[wifiP2pDeviceList.getDeviceList().size()];
                deviceArray = new WifiP2pDevice[wifiP2pDeviceList.getDeviceList().size()];
                int index = 0;

                for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList()) 
                    deviceNameArray[index] = device.deviceName;
                    deviceArray[index] = device;
                    index++;

                    try 
                        deviceEncrypt = AESUtils.encrypt(device.deviceAddress);
                        Log.d(TAG, "Encrypted MAC Address discovered: " + deviceEncrypt);
                     catch (Exception e) 
                        e.printStackTrace();
                    
                
             else 
                Log.d(TAG, "No peers found");
            
        
    ;

我意识到这会阻止设备在范围内自动连接到已知的 WiFi 网络。知道如何解决这个问题吗?下面的代码每分钟注册和注销一次broadcastReceiver,以实现一致的peer检测(这是在onStartCommand函数中运行的)。

private void refreshTime() 
        handler.postDelayed(runnable = new Runnable() 
            @Override
            public void run() 
                Log.d(TAG, "Refreshed");
                registerReceiver(broadcastReceiver, intentFilter);
                unregisterReceiver(broadcastReceiver);
                registerReceiver(broadcastReceiver, intentFilter);
                handler.postDelayed(runnable, delay);
            
        , delay);
    

【问题讨论】:

【参考方案1】:

回答我自己的问题。所以 manager.discoverPeers 是阻止 android 自动连接到已知 Wifi 网络的原因。我通过将 refreshTime() 拆分为两个不同的函数来解决此问题。

private void refreshTime() 
        refreshHandler.postDelayed(refreshRunnable = new Runnable() 
            @Override
            public void run() 
                Log.d(TAG, "Refreshed Registered");
                registerReceiver(broadcastReceiver, intentFilter);
                refreshHandler.postDelayed(refreshRunnable, refreshDelay);
            
        , refreshDelay);
    

private void stopDiscovery() 
        peerHandler.postDelayed(peerRunnable = new Runnable() 
            @Override
            public void run() 
                manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener() 
                    @Override
                    public void onSuccess() 
                        Log.d(TAG, "Peer Discovery Stopped");
                        try 
                            unregisterReceiver(broadcastReceiver);
                            Log.d(TAG, "Refreshed Unregistered");
                         catch (IllegalArgumentException e) 
                            Log.e(TAG, e.toString());
                        
                    

                    @Override
                    public void onFailure(int i) 
                        Log.d(TAG, "Peer Discovery Failed to Stop");
                    
                );
                peerHandler.postDelayed(peerRunnable, peerDelay);
            
        , peerDelay);
    

stopDiscovery() 调用 manager.stopPeerDiscovery() 和 onSuccess 它将注销广播接收器。

两者都以两个不同的时间间隔运行。 refreshTime() 每 1 分钟运行一次,而 stopDiscovery() 每 2 分钟运行一次。这会使 discoverPeers() 停止大约一分钟,并允许操作系统自动连接到已知的 Wifi 网络。

再次。如果有人对此有更优雅的方法,请与我分享。谢谢。

【讨论】:

以上是关于WiFi P2P导致设备WiFi自动连接功能不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

在wifi-direct p2p连接中获取对等设备的IP地址[重复]

通过 Wifi Direct (p2p) 服务将 Raspberry 与 Android 连接

通过root自动接受wifi直连

Wifi Direct P2P 无法在 Android 10+ 设备上运行,无法发现设备

带频道的 wifi p2p 网络

我的手机wifi连接后为啥会自动断开