Why do I always get BUSY when using WifiP2pManager?

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I am trying to connect two Android-Devices using Wi-Fi Direct.

On my HTC-Phone (One SV) it seems to work, but with my second device a LG Optimus 4xhd it doesnt work.

In my onResume() function I start the following thread:

new Thread(){         private int count=0;         public void run()         {             mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {                  @Override                 public void onSuccess() {                     Log.d(tag,"SUCCESS - started discovering peers");                  }                  @Override                 public void onFailure(int reason) {                     count++;                      String err=new String();                     if(reason==WifiP2pManager.BUSY) err="BUSY";                     if(reason==WifiP2pManager.ERROR)err="ERROR";                     if(reason==WifiP2pManager.P2P_UNSUPPORTED) err="P2P_UNSUPPORTED";                     Log.d(tag,"FAIL - couldnt start to discover peers code: "+err+" ("+count+")");                     try {                         Thread.sleep(1000);                     } catch (InterruptedException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                     }                     if(count>=20)return;                     mManager.discoverPeers(mChannel, this);                 }             });         }     }.start(); 

As I said before: this works fine on my HTC. But with the LG I just get "FAIL - couldnt start to discover peers code: BUSY" (20 times).

I also tried WifiP2pManager.createGroup() with the same result.

Another thing that I realized is, that my BroadcastReceiver with the following filters doesnt receive anything on my LG (HTC gets some broadcasts). Filters:

 filter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);     // Indicates a change in the list of available peers.     filter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);     // Indicates the state of Wi-Fi P2P connectivity has changed.     filter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);     // Indicates this device's details have changed.     filter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); 

I am not trying any fancy stuff. I'm just following the Api-Guides for Wi-Fi Direct on http://developer.android.com/guide/topics/connectivity/wifip2p.html

Is there anything I can do? I cant understand why the LG-Phone should work that different.

Just to make one last thing sure: In my onCreate() I get and Init my Manager as followed:

 mManager = (WifiP2pManager) getSystemService(MainActivity.WIFI_P2P_SERVICE);     mChannel = mManager.initialize(this, getMainLooper(), receiver); 

And it seems to work on both devices. (I get a Channel back)

回答1:

you using mainLooper so don't create any thread and do all the , and do some thing like this:

public boolean onOptionsItemSelected(MenuItem item) {         // TODO Auto-generated method stub          switch(item.getItemId()){         case R.id.search_devices:             if(!isWifiP2pEnabled){                 Toast.makeText(this, "enable wifi p2p", Toast.LENGTH_LONG).show();                 return true;             }             onInitiateDiscovery();             manager.discoverPeers(channel, MainActivity.this);              default:                  return super.onOptionsItemSelected(item);          } 


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