3

I need to show list of all possible displays , and after that select one of them to connect with miracast. Here is my code:

private Context context;
    private DisplayManager mDisplayManager;
    private WifiP2pManager wifiP2pManager;
    private ArrayList<WifiP2pDevice> devices;
    private WifiP2pManager.Channel channel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        context = this;

        devices = new ArrayList<WifiP2pDevice>();
        buttonScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startScan();
            }
        });


        mDisplayManager = (DisplayManager)this.getSystemService(Context.DISPLAY_SERVICE);


        wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        channel  = wifiP2pManager.initialize(this, getMainLooper(), new WifiP2pManager.ChannelListener() {
            public void onChannelDisconnected() {
                channel = null;
                Logger.makeLog("onChannelDisconnected");
            }
        });


    }


    WifiP2pManager.PeerListListener myPeerListListener = new WifiP2pManager.PeerListListener() {
                @Override
                public void onPeersAvailable(WifiP2pDeviceList peerList) {

                    // Out with the old, in with the new.
                    devices.clear();
                    devices.addAll(peerList.getDeviceList());

                    Logger.makeLog("devices size " + devices.size());
                    Toast.makeText(context, "devices size " + devices.size(), Toast.LENGTH_LONG).show();

                    if (devices.size() == 0) {
                        Logger.makeLog("No devices found");
                        return;
                    }
                }

            };


    private void startScan() {
        wifiP2pManager.requestPeers(channel, myPeerListListener);
    }

After compiling this code, i press buttonScan, and it show, that 0 displays in area. But one display was near me. But then i connected to the display (TV), entered my app, pressed buttonScan, and it shows me, that 2 displays around me (TV and display of mobile). But its bad, i need to scan all possible displays to connect BEFORE connect... So, what am I doing wrong?

Alex D.
  • 1,424
  • 15
  • 40

0 Answers0