为什么WifiP2pDeviceList对等体为空

时间:2018-07-02 15:39:22

标签: android android-wifi

我有2部电话连接到wify。其中1部运行代码。有wify路由器,但是为什么WifiP2pDeviceList对等体为空,为什么WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)没有被调用。它打印“ CCC”,“ AAA”,“ SSS”,但不打印“ LLL”

代码如下

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

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是如果将maxSDKVersion更改为API 24-25(7.0牛轧糖),它将看到您的设备。 我认为这与主题Background optimizations有关,某些后台功能将仅在广播接收器中被禁用。 这是您不需要关心的Implicit Broadcast Exceptions列表。

编辑

没关系,我在这里发布的Android O +中遇到了这个问题 Peer discovery Issue with Oreo (Android 8+),解决方案是您需要获得SCAN_RESULTS_AVAILABLE_ACTION return empty list in Android 6.0此处提到的ACCESS_COARSE_LOCATION的许可。