下面列出了怎么用android.net.wifi.p2p.WifiP2pDeviceList的API类实例代码及写法,或者点击链接到github查看源代码。
private void requestPeers() {
mWifiP2pManager.requestPeers(mWifiP2pChannel, new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peers) {
if (DEBUG) {
Slog.d(TAG, "Received list of peers.");
}
mAvailableWifiDisplayPeers.clear();
for (WifiP2pDevice device : peers.getDeviceList()) {
if (DEBUG) {
Slog.d(TAG, " " + describeWifiP2pDevice(device));
}
if (isWifiDisplay(device)) {
mAvailableWifiDisplayPeers.add(device);
}
}
if (mDiscoverPeersInProgress) {
handleScanResults();
}
}
});
}
@Override
public synchronized void handleOnPeersChangedResponse() {
manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(final WifiP2pDeviceList wifiP2pDeviceList) {
if(wifiP2PConnectionCallback != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
wifiP2PConnectionCallback.onPeerAvailable(wifiP2pDeviceList);
}
});
}
}
});
}
@Override
public void onPeersAvailable(WifiP2pDeviceList list) {
Log.d(TAG, list.toString());
for (WifiP2pDevice device : list.getDeviceList()) {
Utils.WifiDirectInfo info = Utils.parseWifiDirectName(device.deviceName);
if (info != null) {
String name = info.title;
int port = info.port;
reportUrlDevice(createUrlDeviceBuilder("WifiDirect" + name,
device.deviceAddress + ":" + port)
.setWifiAddress(device.deviceAddress)
.setWifiPort(port)
.setTitle(name)
.setDescription("")
.setDeviceType(Utils.WIFI_DIRECT_DEVICE_TYPE)
.build());
}
}
}
public void onDeviceAvailable(final WifiP2pDeviceList wifiP2pDeviceList) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(recyclerView.isEnabled()) {
progressView.setVisibility(View.GONE);
emptyView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
swipeRefreshLayout.setVisibility(View.VISIBLE);
wifiListAdapter.addDevices(new ArrayList<>(wifiP2pDeviceList.getDeviceList()));
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(activity, DeviceListFragment.this));
}
}
});
}
@Override
public void onReceive(Context context, Intent intent) {
mWifiP2pManager.requestPeers(mWifiDirectChannel,
new WifiP2pManager.PeerListListener() {
public void onPeersAvailable(WifiP2pDeviceList peers) {
// TODO Update UI with new list of peers.
}
});
}
@Override
public void onPeersAvailable(WifiP2pDeviceList peers) {
for(WifiP2pDevice device:peers.getDeviceList())
{
Log.i(TAG,device.toString());
}
_p2pDeviceList.clear();
_p2pDeviceList.addAll(peers.getDeviceList());
}
@Override
public void onPeersAvailable(final WifiP2pDeviceList peers) {
if (DEBUG) Log.v(TAG, "onPeersAvailable:peers=" + peers);
final Collection<WifiP2pDevice> devices = peers.getDeviceList();
synchronized (mAvailableDevices) {
mAvailableDevices.clear();
mAvailableDevices.addAll(devices);
}
callOnUpdateDevices(mAvailableDevices);
}
@Override
public void onPeersAvailable(WifiP2pDeviceList peersList) {
if(progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
peers.clear();
peers.addAll(peersList.getDeviceList());
((WiFiPeerListAdapter)getListAdapter()).notifyDataSetChanged();
if(peers.size() == 0){
Log.e(WiFiDirectActivity.TAG, "the peers size is 0");
return;
}
}
/** Constructor **/
public WifiDirectHandler() {
super(ANDROID_SERVICE_NAME);
dnsSdTxtRecordMap = new HashMap<>();
dnsSdServiceMap = new HashMap<>();
peers = new WifiP2pDeviceList();
}
@Override
public void onPeersAvailable(WifiP2pDeviceList deviceList) {
Collection<WifiP2pDevice> foundDevices = deviceList.getDeviceList();
boolean devicesRemoved = this.Devices.retainAll(foundDevices);
boolean devicesAdded = this.Devices.addAll(foundDevices);
if (devicesRemoved || devicesAdded) {
this.updateDevices();
}
}
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
Log.d(TAG, "onPeersAvailable");
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
peers.clear();
peers.addAll(peerList.getDeviceList());
((WiFiPeerListAdapter)getListAdapter()).notifyDataSetChanged();
if (peers.size() == 0) {
Log.d(TAG, "No devices found");
}
}
@Override
public void onDeviceAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
Fragment fragment = getCurrentFragment();
if(fragment instanceof DeviceListFragment) ((DeviceListFragment)fragment).onDeviceAvailable(wifiP2pDeviceList);
}
@Override
public void onPeerAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
onDeviceAvailable(wifiP2pDeviceList);
}
@Override
public void onPeersAvailable(WifiP2pDeviceList peers) {
onFinished(STA_SUCCESS, peers.getDeviceList());
}
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
Collection<WifiP2pDevice> deviceList = peerList.getDeviceList();
for (WifiP2pDevice device : deviceList)
{
boolean trusted = false; //not sure how to do this with wifi
if (!GilgaService.mapToNickname(device.deviceAddress).startsWith(mLocalAddressHeader)) //not me
mService.processInboundMessage(device.deviceName,device.deviceAddress,trusted);
}
}
@Override
public void onPeerAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
}
@Override
public void onPeersAvailable(WifiP2pDeviceList peers) {
}
void onPeerAvailable(WifiP2pDeviceList wifiP2pDeviceList);
void onDeviceAvailable(final WifiP2pDeviceList wifiP2pDeviceList);