0

I'm implementing an application where I have to discover devices using WiFi p2p and record some data about every detected device. The problem I'm having is that when a device goes from online to offline, the WifiP2pDeviceList is not being updated so that it removes this device. But when a new device a detected it is added normally to the list. So what is the problem with my code?

I already saw this post ( WIFI P2P discovery list is not getting refreshed? ) and it is different from what I have.

 /**
 * A Table of devices that displays all peers and shows the corresponding
 * info about them.
 */
public class DevicesTable extends Fragment implements PeerListListener {

    protected static List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
    View mContentView = null;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.table, null);
        return mContentView;
    }

    /**
     * @return this device
     */
    public WifiP2pDevice getDevice() {
        return device;
    }

    @Override
    public void onPeersAvailable(WifiP2pDeviceList newPeers) {
        ClientActivity x = (ClientActivity) ClientActivity.context;
        if (!x.wifiP2pScanThread.isScanning()) {
            return;
        }
        peers.clear();
        peers.addAll(newPeers.getDeviceList());
        ArrayList<String> onlinemacs = new ArrayList<>();
        if (peers.size() == 0) {
            Log.d(ClientActivity.TAG, "No devices found");
        } else {
            for (WifiP2pDevice dev : peers) {
                //do something
            }
        }
     }
 }
Community
  • 1
  • 1
kamal
  • 96
  • 1
  • 2
  • 10

1 Answers1

0

I've tried many alternatives suggesting re-initializing the wifip2p manager and channel or calling peers.clear() but none of them worked. Then I came across this (perfect) explanation to what's going on, it says that the list will be cleared after a specific period time (roughly a minute, it depends on the phone) and there is no feasible way of doing that using the available software. Note that manually turning off/on the Wifi does reset the list (and almost all other wifi-related settings), but that's not practical at all.

So in a nutshell there is no way to clear the previously-not-available-anymore peers programatically.

Community
  • 1
  • 1
kamal
  • 96
  • 1
  • 2
  • 10