2016-12-01 96 views
0

我已經成功實現了wifi直接示例演示,並且雙向圖像共享正常工作,但是我的問題是當一個設備A發送邀請以連接到設備B並且設備B取消該邀請,我無法處理該事件的取消回調,因爲它是由Android生成的。Android中的對等設備上的WiFi直接圖像共享

任何人都可以幫我解決我的問題。

以下是我的廣播reciever碼和連接碼:

/** 
* A BroadcastReceiver that notifies of important wifi p2p events. 
*/ 
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver { 

    private WifiP2pManager manager; 
    private Channel channel; 
    private WiFiDirectActivity activity; 

    /** 
    * @param manager WifiP2pManager system service 
    * @param channel Wifi p2p channel 
    * @param activity activity associated with the receiver 
    */ 
    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, 
      WiFiDirectActivity activity) { 
     super(); 
     this.manager = manager; 
     this.channel = channel; 
     this.activity = activity; 
    } 

    /* 
    * (non-Javadoc) 
    * @see android.content.BroadcastReceiver#onReceive(android.content.Context, 
    * android.content.Intent) 
    */ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { 

      // UI update to indicate wifi p2p status. 
      int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); 
      if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { 
       // Wifi Direct mode is enabled 
       activity.setIsWifiP2pEnabled(true); 
      } else { 
       activity.setIsWifiP2pEnabled(false); 
       activity.resetData(); 

      } 
      Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state); 
     } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { 

      // request available peers from the wifi p2p manager. This is an 
      // asynchronous call and the calling activity is notified with a 
      // callback on PeerListListener.onPeersAvailable() 
      if (manager != null) { 
       manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager() 
         .findFragmentById(R.id.frag_list)); 
      } 
      Log.d(WiFiDirectActivity.TAG, "P2P peers changed"); 
     } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 

      if (manager == 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 

       DeviceDetailFragment fragment = (DeviceDetailFragment) activity 
         .getFragmentManager().findFragmentById(R.id.frag_detail); 
       manager.requestConnectionInfo(channel, fragment); 
      } else { 
       // It's a disconnect 
       activity.resetData(); 
      } 
     } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { 
      DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager() 
        .findFragmentById(R.id.frag_list); 
      fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
        WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)); 

     } 
    } 
} 

對於連接,我用下面的代碼:

WifiP2pConfig config = new WifiP2pConfig(); 
     config.deviceAddress = device.deviceAddress; 
     config.wps.setup = WpsInfo.PBC; 
     if (progressDialog != null && progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
     } 
     device.wps.setup = WpsInfo.PBC; 
     device.groupOwnerIntent = 0; // I want this device to become the owner 
     manager.connect(channel, device, new ActionListener() { 

      @Override 
      public void onSuccess() { 
       // WiFiDirectBroadcastReceiver will notify us. Ignore for now. 
      } 

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

回答

0

類需要實現ConnectionInfoListener。並且在函數onConnectionInfoAvailable(最終的WifiP2pInfo信息)中,您可以檢查是否已建立成功的連接。類型爲WifiP2pInfo的info參數包含有關連接的信息。它包含一個叫做groupFormed的布爾值,它指示一個p2p組是否已經成功形成。你也可以從中檢索設備是否groupOwner和groupOwner的IP等

@覆蓋 公共無效onConnectionInfoAvailable(最終WifiP2pInfo資訊){

// After the group negotiation, we check if this device 
    // is acting as group owner 

    if (info.groupFormed && !info.isGroupOwner) { 

     // Do whatever you want the group owner to do 

    } else if (info.groupFormed) { 
     // The device now act as the slave device, 
     // and the other connected device is group owner 
} 

谷歌提供了一個很好的演示應用程序使用WiFi Direct在兩臺設備之間發送圖像。檢查它的實現並嘗試構建它的頂部。鏈接:http://developer.android.com/guide/topics/connectivity/wifip2p.html

希望這會有所幫助。如果您有任何問題,請告訴我。

得到它:

Check whether android wifip2p connection was successful?