我有一个非常困惑的问题。如果我在android选项中使用Wifi Direct,它工作得非常好,我可以发现所有其他使用Wifi Direct的设备。但如果我使用我的应用程序,它不会更新我的列表。
主要活动。
PeerListListener peerListListener = new PeerListListener()
{
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList)
{
if(!peerList.getDeviceList().equals(peers))
{
peers.clear();
peers.addAll(peerList.getDeviceList());
deviceNameArray = new String[peerList.getDeviceList().size()];
deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];
index = 0;
for(WifiP2pDevice device : peerList.getDeviceList())
{
deviceNameArray[index] = device.deviceName;
deviceArray[index] = device;
index++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, deviceNameArray);
lstView.setAdapter(adapter);
}
if(peers.size() == 0)
{
Toast.makeText(getApplicationContext(), "no Devices found", Toast.LENGTH_SHORT).show();
}
}
};
BroadcastReceiver:
else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
{
if(p2pManager != null)
{
p2pManager.requestPeers(p2pChannel, p2pActivity.peerListListener);
}
}
问题是,"peerList "是空的。for循环没有给我的列表添加任何东西,因为 "peerList "中没有任何东西可以添加。我只是不明白为什么,因为在android选项中它工作得非常好。