3

我需要显示所有可能显示的列表,然后选择其中一个与 Miracast 连接。这是我的代码:

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);
    }

编译此代码后,我按下 buttonScan,它显示 0 显示在区域中。但是我附近有一个显示器。但后来我连接到显示器(电视),进入我的应用程序,按下按钮扫描,它向我显示,我周围有 2 个显示器(电视和手机显示器)。但它很糟糕,我需要在连接之前扫描所有可能的显示器以进行连接......那么,我做错了什么?

4

0 回答 0