Skip to content

Instantly share code, notes, and snippets.

@activetheory
Created January 5, 2016 20:45
Show Gist options
  • Select an option

  • Save activetheory/5315d61a2172ab0bb468 to your computer and use it in GitHub Desktop.

Select an option

Save activetheory/5315d61a2172ab0bb468 to your computer and use it in GitHub Desktop.
private void initialize(JSONObject data) throws JSONException {
if (_active) {
return;
}
_active = true;
Activity activity = Modules.ACTIVITY;
_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) activity.getSystemService(Context.WIFI_P2P_SERVICE);
_channel = _manager.initialize(activity, activity.getMainLooper(), null);
_receiver = new WifiP2PReceiver(_manager, _channel, this);
activity.registerReceiver(_receiver, _intentFilter);
discoverPeers();
}
private void discoverPeers() {
_manager.discoverPeers(_channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//Log.d("WifiP2P", "successfully looking for peers");
}
@Override
public void onFailure(int reasonCode) {
JSInterface.sendError("WifiP2P: There was an error trying to discover peers "+Integer.toString(reasonCode));
}
});
}
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
Log.d("WifiP2P", "got some info about a connection");
String IP = info.groupOwnerAddress.getHostAddress();
if (info.groupFormed && info.isGroupOwner) {
Log.d("WifiP2P", "am group owner");
HOST = true;
if (_server == null) {
_server = new WifiP2PSocketServer(IP, this);
}
} else if (info.groupFormed) {
Log.d("WifiP2P", "need to join the group " + IP);
_client = new WifiP2PSocketClient(IP, this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment