2016-12-15 5 views
0

동료를 발견했지만 그 중 하나에 연결할 수 없습니다. 내 BroadcastReceiver는 WIFI_P2P_CONNECTION_CHANGED_ACTION을받지 못한다고 생각합니다. 내부에있는 것을 실행하지 않기 때문입니다.연결 WiFi p2p android, WIFI_P2P_CONNECTION_CHANGED_ACTION

연결() :

public void connect(View v) { 
    // Picking the first device found on the network. 
    WifiP2pDevice device = (WifiP2pDevice) peers.get(0); 

    WifiP2pConfig config = new WifiP2pConfig(); 
    config.deviceAddress = device.deviceAddress; 
    config.wps.setup = WpsInfo.PBC; 
    config.groupOwnerIntent = 0; 


    mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() { 
     @Override 
     public void onSuccess() { 
      // WiFiDirectBroadcastReceiver will notify us. Ignore for now. 
      Toast.makeText(MultiActivity.this, "Connect initiated" , 
        Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
     public void onFailure(int reason) { 
      Toast.makeText(MultiActivity.this, "Connect failed. Retry.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

onReceive() :

else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 
     // Connection state changed! We should probably do something about 
     // that. 
     Toast.makeText(activity, "ca marche", 
       Toast.LENGTH_SHORT).show(); 

     if (mManager == null) { 
      return; 
     } 

     NetworkInfo networkInfo = (NetworkInfo) intent 
       .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); 

     if (networkInfo.isConnected()) { 
      // We are connected with the other device, request connection 
      // info to find group owner IP 
      mManager.requestConnectionInfo(mChannel, connectionListener); 
     } 

    } 

토스트 "캘리포니아 마르쉐는"결코 화면에 나타납니다. 도와주세요, 고마워요.

답변

0

브로드 캐스트 수신기를 적절한 인 텐트 필터에 등록하십시오.

  1. 새 텐트 필터 만들기 : 예를 들어

    IntentFilter tmpFilter = new IntentFilter();

  2. 이 조치를 추가 귀하의 관심에 : registerReceiver(p2PBroadcastReceiver, tmpFilter)
: tmpFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
  • 방송 수신기 (바람직 onResume())에 등록

    자세한 예 : https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

    Goodluck.

  • 관련 문제