Wifip2p 发现对等点不工作
Wifip2p discover peers not working
我看到了代码Here。 public void displayPeers(final WifiP2pDeviceList peers) 如何在这里工作。
当我 运行 在设备中时,它显示在列表视图中的设备中,但是它如何在没有初始化方法的情况下获取设备列表,即。 displaypeers() 不打电话?
之后我更改了我的项目的代码,Displaypeers 方法不工作/调用。但是当我导入它的完整项目时它起作用了。它是如何工作的?请帮我 。
如果我犯了错误,谁能告诉我如何调用该方法?
在 oncreate 函数本身中显示对等点(最终的 WifiP2pDeviseList 对等点)。
谢谢 。抱歉我的英语不好。
public void searchForPeers(View view) {
//Discover peers, no call back method given
wifiManager.discoverPeers(wifichannel, null);
}
.....
....
public void displayPeers(final WifiP2pDeviceList peers)
{
//Dialog to show errors/status
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("WiFi Direct File Transfer");
//Get list view
ListView peerView = (ListView) findViewById(R.id.peers_listview);
//Make array list
ArrayList<String> peersStringArrayList = new ArrayList<String>();
//Fill array list with strings of peer names
for(WifiP2pDevice wd : peers.getDeviceList())
{
peersStringArrayList.add(wd.deviceName);
}
//Set list view as clickable
peerView.setClickable(true);
//Make adapter to connect peer data to list view
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, peersStringArrayList.toArray());
//Show peer data in listview
peerView.setAdapter(arrayAdapter);
peerView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
//Get string from textview
TextView tv = (TextView) view;
WifiP2pDevice device = null;
//Search all known peers for matching name
for(WifiP2pDevice wd : peers.getDeviceList())
{
if(wd.deviceName.equals(tv.getText()))
device = wd;
}
if(device != null)
{
//Connect to selected peer
connectToPeer(device);
}
else
{
dialog.setMessage("Failed");
dialog.show();
}
}
// TODO Auto-generated method stub
});
}
编辑:Oncreate 方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
// getActionBar().setDisplayHomeAsUpEnabled(true);
wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
wifichannel = wifiManager.initialize(this, getMainLooper(), null);
wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);
wifiClientReceiverIntentFilter = new IntentFilter();
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
connectedAndReadyToSendFile = false;
filePathProvided = false;
fileToSend = null;
transferActive = false;
clientServiceIntent = null;
targetDevice = null;
wifiInfo = null;
registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);
setClientFileTransferStatus("Client is currently idle");
//setTargetFileStatus("testing");
}
你应该看看整个项目。 类 不仅仅是 ClientActivity。它们都相互作用以形成一个工作应用程序。
displayPeers() 从 WiFiClientBroadcastReceiver 调用。我建议您在这里查看所有文件:
我看到了代码Here。 public void displayPeers(final WifiP2pDeviceList peers) 如何在这里工作。 当我 运行 在设备中时,它显示在列表视图中的设备中,但是它如何在没有初始化方法的情况下获取设备列表,即。 displaypeers() 不打电话?
之后我更改了我的项目的代码,Displaypeers 方法不工作/调用。但是当我导入它的完整项目时它起作用了。它是如何工作的?请帮我 。 如果我犯了错误,谁能告诉我如何调用该方法? 在 oncreate 函数本身中显示对等点(最终的 WifiP2pDeviseList 对等点)。 谢谢 。抱歉我的英语不好。
public void searchForPeers(View view) {
//Discover peers, no call back method given
wifiManager.discoverPeers(wifichannel, null);
}
..... ....
public void displayPeers(final WifiP2pDeviceList peers)
{
//Dialog to show errors/status
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("WiFi Direct File Transfer");
//Get list view
ListView peerView = (ListView) findViewById(R.id.peers_listview);
//Make array list
ArrayList<String> peersStringArrayList = new ArrayList<String>();
//Fill array list with strings of peer names
for(WifiP2pDevice wd : peers.getDeviceList())
{
peersStringArrayList.add(wd.deviceName);
}
//Set list view as clickable
peerView.setClickable(true);
//Make adapter to connect peer data to list view
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, peersStringArrayList.toArray());
//Show peer data in listview
peerView.setAdapter(arrayAdapter);
peerView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
//Get string from textview
TextView tv = (TextView) view;
WifiP2pDevice device = null;
//Search all known peers for matching name
for(WifiP2pDevice wd : peers.getDeviceList())
{
if(wd.deviceName.equals(tv.getText()))
device = wd;
}
if(device != null)
{
//Connect to selected peer
connectToPeer(device);
}
else
{
dialog.setMessage("Failed");
dialog.show();
}
}
// TODO Auto-generated method stub
});
}
编辑:Oncreate 方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
// getActionBar().setDisplayHomeAsUpEnabled(true);
wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
wifichannel = wifiManager.initialize(this, getMainLooper(), null);
wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);
wifiClientReceiverIntentFilter = new IntentFilter();
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
connectedAndReadyToSendFile = false;
filePathProvided = false;
fileToSend = null;
transferActive = false;
clientServiceIntent = null;
targetDevice = null;
wifiInfo = null;
registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);
setClientFileTransferStatus("Client is currently idle");
//setTargetFileStatus("testing");
}
你应该看看整个项目。 类 不仅仅是 ClientActivity。它们都相互作用以形成一个工作应用程序。
displayPeers() 从 WiFiClientBroadcastReceiver 调用。我建议您在这里查看所有文件: