mithun
mithun

Reputation: 121

why is WifiP2pDeviceList peers empty

I have 2 phones connected to wify.1 of which runs the code. There is the wify router.But why is WifiP2pDeviceList peers empty and why is WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action) not called. It prints "CCC", "AAA","SSS", but not "LLL"

Code is as follows

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private WifiScanActivity mActivity;
    private WifiP2pManager.PeerListListener myPeerListListener;
    private Context mContxt;

    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
                                       WifiScanActivity activity) {
        super();
        this.mManager = manager;
        this.mChannel = channel;
        this.mActivity = activity;
        this.mContxt = mActivity.getApplicationContext();
        Toast.makeText(mContxt, "AAA", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            // Check to see if Wi-Fi is enabled and notify appropriate activity
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers
            // request available peers from the wifi p2p manager. This is an
            // asynchronous call and the calling activity is notified with a
            // callback on PeerListListener.onPeersAvailable()
            Toast.makeText(mContxt, "LLL", Toast.LENGTH_SHORT).show();
            WifiP2pDeviceList list = intent.getParcelableExtra(WifiP2pManager.EXTRA_P2P_DEVICE_LIST);

            for (WifiP2pDevice d : list.getDeviceList()) { //...
                Toast.makeText(mContxt, d.deviceAddress, Toast.LENGTH_SHORT).show();
            }

        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
            Toast.makeText(mContxt, "CCC", Toast.LENGTH_SHORT).show();

            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) {
                    // Wifi P2P is enabled
                } else {
                    // Wi-Fi P2P is not enabled
                }
            }
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing
            Toast.makeText(mContxt, "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", Toast.LENGTH_SHORT).show();
            if (mManager != null) {
                Toast.makeText(mContxt, "UUU", Toast.LENGTH_SHORT).show();
                mManager.requestPeers(mChannel,myPeerListListener= new WifiP2pManager.PeerListListener() {
                   @Override
                    public void onPeersAvailable(WifiP2pDeviceList peers) {
                        //Log.d(TAG,String.format("PeerListListener: %d peers available, updating device list", peers.getDeviceList().size()));

                        Toast.makeText(mContxt, "BBB", Toast.LENGTH_SHORT).show();
                        //Toast.makeText(mContxt, peers.toString(), Toast.LENGTH_SHORT).show();
                        //if()Toast.makeText(mContxt, "nullllllllllllllllllll", Toast.LENGTH_SHORT).show();
                       for (  WifiP2pDevice peer : peers.getDeviceList()) {
                           WifiP2pDevice device = peer;
                           //here get the device info
                           String deviceaddr = device.deviceAddress;
                           Toast.makeText(mContxt, deviceaddr, Toast.LENGTH_SHORT).show();
                       }
                        // DO WHATEVER YOU WANT HERE
                        // YOU CAN GET ACCESS TO ALL THE DEVICES YOU FOUND FROM peers OBJECT

                    }
                });
            }
        }
    }


}

WifiScanActivity.java:

public class WifiScanActivity extends AppCompatActivity {

    WifiP2pManager mManager;
    WifiP2pManager.Channel mChannel;
    BroadcastReceiver mReceiver;
    IntentFilter mIntentFilter;
    Context mCntxt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mCntxt = this.getApplicationContext();
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);

        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        mCntxt.registerReceiver(mReceiver, mIntentFilter);
        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(mCntxt, "SSS", Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onFailure(int reasonCode) {
                Toast.makeText(mCntxt, "FFF", Toast.LENGTH_SHORT).show();
            }
        });


    }

    /* register the broadcast receiver with the intent values to be matched */
    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, mIntentFilter);
    }
    /* unregister the broadcast receiver */
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }
}

Upvotes: 1

Views: 1132

Answers (1)

Supportic
Supportic

Reputation: 159

I don't know why but if you change the maxSDKVersion to API 24-25 (7.0 Nougat) it will see your devices. I think is has something to do with the topic Background optimizations, that some background functions will simply be disabled in a broadcast receiver. Here is a list of Implicit Broadcast Exceptions which you don't have to care about.

EDIT:

Nevermind on my search journey I came to this problem in Android O+ issued here Peer discovery Issue with Oreo (Android 8+) and the solution is that you need the permission for ACCESS_COARSE_LOCATION mentioned here SCAN_RESULTS_AVAILABLE_ACTION return empty list in Android 6.0.

Upvotes: 2

Related Questions