Android/Java WiFi direct peer list

走远了吗. 提交于 2019-12-03 14:22:05

I can see you did an initial discovery when constructing you WiFiDirectBroadcastReceiver

and then in your Activity.onResume() you first construct WiFiDirectBroadcastReceiver and then registerReceiver().

@Override
public void onResume() {
    super.onResume();
    _broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this, _peerListListener);
    registerReceiver(_broadcastReceiver, _intentFilter);
}

The problem that you do not see the response come back is probably because of the sequence.Try move discoverPeers() to Activity.onResume() should solve the problem.

You're missing a call to DiscoverPeers:

To start searching for nearby devices with Wi-Fi Direct, call discoverPeers().

The specific app logic for when you want that initial discovery to happen will determine where in your code that happens. In the API samples for SDK 17, this happens when an option item is clicked from the menu. This could easily be moved to the onResume method (this will be initiated every time the activity is resumed, or in the click listener for a button on the screen.

For future troubleshooting, I would recommend keeping the WiFiDirectBroadcastReceiver class inside the Activity's class (unless you need to reuse the code) for ease of use with callbacks -- you can access the outer class's methods directly from the inner class and make this a bit easier to troubleshoot.

Ofer Wulf

Try changing this:

_manager.requestPeers(_channel, _peerListListener);

to this:

_manager.requestPeers(_channel, _activityRef._peerListListener);

Google is providing a Wifi Direct example since Oct 2011: https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo

I added this link because it took me a while to find it (original link is reffering to Wifi P2P Demo, not WiFiDirectDemo).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!