2017-04-03 89 views
0

我在开始屏幕上有两个按钮,一个用于启动Google地图。还有一个“搜索按钮”,用于寻找对等点并通过WiFi直接连接到它们。当我按搜索按钮时,我希望应用程序直接开始搜索,并显示对等点。当我按HomeScreen上的搜索按钮时,我应该为“WiFiDirectActivity”写入什么内容?如何从我的主屏幕开始我的WifiDirect活动?

我的主屏幕类:

public class HomeScreen extends Activity { 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home_screen); 

    Button mapButton = (Button) findViewById(R.id.mapButton); 

    mapButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(HomeScreen.this, MapsActivity.class); 
      startActivity(intent); 
     } 

    }); 

    Button sensorButton = (Button) findViewById(R.id.sensorBtn); 

    sensorButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(HomeScreen.this, WiFiDirectActivity.class); 
      startActivity(intent); 
     } 

    }); 

} 

} 

我WiFiDirectActivity类:

public class WiFiDirectActivity extends Activity implements WifiP2pManager.ChannelListener, DeviceListFragment.DeviceActionListener { 

public static final String TAG = "wifidirectdemo"; 
private WifiP2pManager manager; 
private boolean isWifiP2pEnabled = false; 
private boolean retryChannel = false; 

private final IntentFilter intentFilter = new IntentFilter(); 
private WifiP2pManager.Channel channel; 
private BroadcastReceiver receiver = null; 

/** 
* @param isWifiP2pEnabled the isWifiP2pEnabled to set 
*/ 
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) { 
    this.isWifiP2pEnabled = isWifiP2pEnabled; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_peers); 

    // add necessary intent values to be matched. 

    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); 
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); 
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); 
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); 

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); 
    channel = manager.initialize(this, getMainLooper(), null); 


    Button sensorButton = (Button) findViewById(R.id.sensorBtn); 

    sensorButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 

      if (!isWifiP2pEnabled) { 
       Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning, 
         Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
        .findFragmentById(R.id.frag_list); 
      fragment.onInitiateDiscovery(); 
      manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { 

       @Override 
       public void onSuccess() { 
        Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated", 
          Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void onFailure(int reasonCode) { 
        Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode, 
          Toast.LENGTH_SHORT).show(); 
       } 

      }); 

     } 

    }); 

} 

/** register the BroadcastReceiver with the intent values to be matched */ 
@Override 
public void onResume() { 
    super.onResume(); 
    receiver = new WiFiDirectBroadcastReceiver(manager, channel, this); 
    registerReceiver(receiver, intentFilter); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    unregisterReceiver(receiver); 
} 

/** 
* Remove all peers and clear all fields. This is called on 
* BroadcastReceiver receiving a state change event. 
*/ 
public void resetData() { 
    DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_list); 
    DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    if (fragmentList != null) { 
     fragmentList.clearPeers(); 
    } 
    if (fragmentDetails != null) { 
     fragmentDetails.resetViews(); 
    } 
} 

@Override 
public void showDetails(WifiP2pDevice device) { 
    DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    fragment.showDetails(device); 

} 

@Override 
public void connect(WifiP2pConfig config) { 
    manager.connect(channel, config, new WifiP2pManager.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(); 
     } 
    }); 
} 

@Override 
public void disconnect() { 
    final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    fragment.resetViews(); 
    manager.removeGroup(channel, new WifiP2pManager.ActionListener() { 

     @Override 
     public void onFailure(int reasonCode) { 
      Log.d(TAG, "Disconnect failed. Reason :" + reasonCode); 

     } 

     @Override 
     public void onSuccess() { 
      fragment.getView().setVisibility(View.GONE); 
     } 

    }); 
} 

@Override 
public void onChannelDisconnected() { 
    // we will try once more 
    if (manager != null && !retryChannel) { 
     Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show(); 
     resetData(); 
     retryChannel = true; 
     manager.initialize(this, getMainLooper(), this); 
    } else { 
     Toast.makeText(this, 
       "Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.", 
       Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void cancelDisconnect() { 

    /* 
    * A cancel abort request by user. Disconnect i.e. removeGroup if 
    * already connected. Else, request WifiP2pManager to abort the ongoing 
    * request 
    */ 
    if (manager != null) { 
     final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
       .findFragmentById(R.id.frag_list); 
     if (fragment.getDevice() == null 
       || fragment.getDevice().status == WifiP2pDevice.CONNECTED) { 
      disconnect(); 
     } else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE 
       || fragment.getDevice().status == WifiP2pDevice.INVITED) { 

      manager.cancelConnect(channel, new WifiP2pManager.ActionListener() { 

       @Override 
       public void onSuccess() { 
        Toast.makeText(WiFiDirectActivity.this, "Aborting connection", 
          Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void onFailure(int reasonCode) { 
        Toast.makeText(WiFiDirectActivity.this, 
          "Connect abort request failed. Reason Code: " + reasonCode, 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 

} 
} 

,我想从一个搜索按钮,重新在菜单中搜索按钮在不同的屏幕上的代码。 (我删除了直接发现)。

public class WiFiDirectActivity extends Activity implements WifiP2pManager.ChannelListener, DeviceListFragment.DeviceActionListener { 

... 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_items, menu); 
    return true; 
} 

/* 
* (non-Javadoc) 
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) 
*/ 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.atn_direct_enable: 
      if (manager != null && channel != null) { 

       // Since this is the system wireless settings activity, it's 
       // not going to send us a result. We will be notified by 
       // WiFiDeviceBroadcastReceiver instead. 

       startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); 
      } else { 
       Log.e(TAG, "channel or manager is null"); 
      } 
      return true; 

     case R.id.atn_direct_discover: 
      Log.d("ADebugTag", "Value: " + isWifiP2pEnabled); 

      if (!isWifiP2pEnabled) { 
       Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning, 
         Toast.LENGTH_SHORT).show(); 
       return true; 
      } 
      final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
        .findFragmentById(R.id.frag_list); 
      fragment.onInitiateDiscovery(); 
      manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { 

       @Override 
       public void onSuccess() { 
        Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated", 
          Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void onFailure(int reasonCode) { 
        Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode, 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

回答

0

您可以开始在执行的onResume搜索如下:

public class WiFiDirectActivity extends Activity implements WifiP2pManager.ChannelListener, DeviceListFragment.DeviceActionListener { 

public static final String TAG = "wifidirectdemo"; 
private WifiP2pManager manager; 
private boolean isWifiP2pEnabled = false; 
private boolean retryChannel = false; 

private final IntentFilter intentFilter = new IntentFilter(); 
private WifiP2pManager.Channel channel; 
private BroadcastReceiver receiver = null; 

/** 
* @param isWifiP2pEnabled the isWifiP2pEnabled to set 
*/ 
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) { 
    this.isWifiP2pEnabled = isWifiP2pEnabled; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_peers); 

    // add necessary intent values to be matched. 

    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); 
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); 
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); 
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); 

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); 
    channel = manager.initialize(this, getMainLooper(), null); 


    Button sensorButton = (Button) findViewById(R.id.sensorBtn); 

    sensorButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 

      if (!isWifiP2pEnabled) { 
       Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning, 
         Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
        .findFragmentById(R.id.frag_list); 
      fragment.onInitiateDiscovery(); 
      manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { 

       @Override 
       public void onSuccess() { 
        Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated", 
          Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void onFailure(int reasonCode) { 
        Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode, 
          Toast.LENGTH_SHORT).show(); 
       } 

      }); 

     } 

    }); 

} 

/** register the BroadcastReceiver with the intent values to be matched */ 
@Override 
public void onResume() { 
    super.onResume(); 
    receiver = new WiFiDirectBroadcastReceiver(manager, channel, this); 
    registerReceiver(receiver, intentFilter); 
    performSearch(); // start searching devices 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    unregisterReceiver(receiver); 
} 

/** 
* Remove all peers and clear all fields. This is called on 
* BroadcastReceiver receiving a state change event. 
*/ 
public void resetData() { 
    DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_list); 
    DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    if (fragmentList != null) { 
     fragmentList.clearPeers(); 
    } 
    if (fragmentDetails != null) { 
     fragmentDetails.resetViews(); 
    } 
} 

@Override 
public void showDetails(WifiP2pDevice device) { 
    DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    fragment.showDetails(device); 

} 

@Override 
public void connect(WifiP2pConfig config) { 
    manager.connect(channel, config, new WifiP2pManager.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(); 
     } 
    }); 
} 

@Override 
public void disconnect() { 
    final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.frag_detail); 
    fragment.resetViews(); 
    manager.removeGroup(channel, new WifiP2pManager.ActionListener() { 

     @Override 
     public void onFailure(int reasonCode) { 
      Log.d(TAG, "Disconnect failed. Reason :" + reasonCode); 

     } 

     @Override 
     public void onSuccess() { 
      fragment.getView().setVisibility(View.GONE); 
     } 

    }); 
} 

@Override 
public void onChannelDisconnected() { 
    // we will try once more 
    if (manager != null && !retryChannel) { 
     Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show(); 
     resetData(); 
     retryChannel = true; 
     manager.initialize(this, getMainLooper(), this); 
    } else { 
     Toast.makeText(this, 
       "Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.", 
       Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void cancelDisconnect() { 

    /* 
    * A cancel abort request by user. Disconnect i.e. removeGroup if 
    * already connected. Else, request WifiP2pManager to abort the ongoing 
    * request 
    */ 
    if (manager != null) { 
     final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
       .findFragmentById(R.id.frag_list); 
     if (fragment.getDevice() == null 
       || fragment.getDevice().status == WifiP2pDevice.CONNECTED) { 
      disconnect(); 
     } else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE 
       || fragment.getDevice().status == WifiP2pDevice.INVITED) { 

      manager.cancelConnect(channel, new WifiP2pManager.ActionListener() { 

       @Override 
       public void onSuccess() { 
        Toast.makeText(WiFiDirectActivity.this, "Aborting connection", 
          Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void onFailure(int reasonCode) { 
        Toast.makeText(WiFiDirectActivity.this, 
          "Connect abort request failed. Reason Code: " + reasonCode, 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 

} 

private void performSearch() { 
    if (!isWifiP2pEnabled) { 
      Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning, 
        Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager() 
       .findFragmentById(R.id.frag_list); 
     fragment.onInitiateDiscovery(); 
     manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { 

      @Override 
      public void onSuccess() { 
       Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated", 
         Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onFailure(int reasonCode) { 
       Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode, 
         Toast.LENGTH_SHORT).show(); 
      } 

     }); 
} 

} 
+0

感谢您的回答,但现在当我启用了WifiP2p,我仍然得到的是“isWifiP2pEnabled” ==假。 – Kspr

+0

setIsWifiP2pEnabled()没有被调用,因此isWifiP2pEnabled变量将始终保持为false。 –

+0

但它在我的WifiBroadcastReceiver类中调用?当我在OnResume()中创建我的“接收器”时,不应该调用它吗? – Kspr

相关问题