android wifi-直接连接成功回调呼叫

时间:2015-01-30 11:44:55

标签: android android-intent android-activity android-fragments

我尝试使用wifidirect连接到一个wifi设备,但是当我运行以下代码时,它总是无限地显示连接的设备通知我可能知道我在哪里犯了错误 这是我的代码片段

if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
    final Context ctx  = context;
   //showing this pop up message always can you help me to understand this issue please
    Toast.makeText(ctx, "WIFI_P2P_PEERS_CHANGED_ACTION",Toast.LENGTH_LONG).show();
    if (mManager != null) {

        mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {

                @Override
                public void onPeersAvailable(WifiP2pDeviceList peers) {

                    for (  WifiP2pDevice peer : peers.getDeviceList()) {

                        WifiP2pDevice device=peer;
                        String serverName = "Android_f9c4";
                        if(device.deviceName.equals(serverName)){
                            WifiP2pConfig config = new WifiP2pConfig();
                            config.deviceAddress = device.deviceAddress;


                            mManager.connect(mChannel,config ,new ActionListener() {

                            //whats wrong here
                                @Override
                                public void onSuccess() {
                                    // This fire always 
                                    Toast.makeText(ctx, "connected",Toast.LENGTH_LONG).show();
                                }

                                @Override
                                public void onFailure(int reason) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(ctx, "Not connected",Toast.LENGTH_LONG).show();
                                }

                            });
                        }

                    }

                }
            });
        }

    }
}

1 个答案:

答案 0 :(得分:0)

使用

ArrayList<WifiP2pDevice> list=peers.getDeviceList();
for( WifiP2pDevice peer : list)

取代

for (  WifiP2pDevice peer : peers.getDeviceList()) {