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); 
     } 

    } 

トースト "のCAマルシェ" 画面に表示されることはありません。 私を助けてください、ありがとう。

答えて

0

放送受信機を適切なIntentFilterに登録してください。例えば

  1. 新しいインテントフィルタを作成します。IntentFilter tmpFilter = new IntentFilter();
  2. はでご興味のあるアクションを追加します。tmpFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
  3. 放送受信機(好ましくはonResume())でそれを登録します。registerReceiver(p2PBroadcastReceiver, tmpFilter)

詳細な例は次のとおりです。 https://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

Goodluck。