android.net.wifi.p2p.WifiP2pInfo Java Examples

The following examples show how to use android.net.wifi.p2p.WifiP2pInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WifiP2PServiceImpl.java    From Wifi-Connect with Apache License 2.0 6 votes vote down vote up
@Override
public void startDataTransfer(final String message) {
    manager.requestConnectionInfo(channel, new WifiP2pManager.ConnectionInfoListener() {
        @Override
        public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
            boolean isGroupOwner = wifiP2pInfo.groupFormed && wifiP2pInfo.isGroupOwner;
            boolean isClient = RECEIVER_PREFS_VALUE.equalsIgnoreCase(PreferenceUtils.getStringValues(activity, PREFS_CLIENT_KEY));

            if(isClient && isGroupOwner) {
                handleOnPeerServer(wifiP2pInfo);

            } else if(!isClient && !isGroupOwner && message != null) {
                handleOnPeerClient(wifiP2pInfo, message);
            }
        }
    });
}
 
Example #2
Source File: P2pChatService.java    From android-p2p with MIT License 6 votes vote down vote up
@Override
public void onConnectionInfoAvailable(WifiP2pInfo p2pInfo) {
    Log.d("P2P", "Received info");
    if (!WifiP2pInfoHelper.areInfosEquals(this.PrevP2pInfo, p2pInfo)) {
        Log.d("P2P", "New info");
        // stop possible previous connection
        if (this.Connection != null) {
            this.Connection.Stop();
        }
        if (p2pInfo != null && p2pInfo.groupFormed) {
            // start new connection
            if (p2pInfo.isGroupOwner) {
                Log.d("P2P", "I'm master");
                this.Connection = new ChatServer(this.MessageReceiver);
                Log.d("P2P", "Server started");
            } else {
                Log.d("P2P", "I'm servant");
                ClientInfo info = new ClientInfo("deviceAddress", "user");
                this.Connection = new ChatClient(p2pInfo.groupOwnerAddress, this.MessageReceiver, info);
                Log.d("P2P", "Client started");
            }
        }
    }
    this.PrevP2pInfo = p2pInfo;
}
 
Example #3
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
public void doWifiDirectConnectionAvailable(WifiP2pInfo info) {
		if (mIsWifiP2pConnected) {
			return;
		}
		
		if (info != null && info.groupOwnerAddress != null) {
			mWifiP2pGroupOwnerAddress = info.groupOwnerAddress.getHostAddress();
			ALog.i(TAG, "doWifiDerectConnectionAvailable mWifiP2pGroupOwnerAddress:" + mWifiP2pGroupOwnerAddress);
		}
		if(info==null){
			mWifiP2pManager.requestConnectionInfo(getChannel(), this);
			return;
		}
		setWifiP2pInfo(info);
		Message msg = mManagerHandler.obtainMessage(Utilities.MSG_WIFI_CONNECTED);
		mManagerHandler.sendMessage(msg);
		mIsWifiP2pConnected = true;
		
		mContext.sendBroadcast(new Intent(WIFI_DIRECT_CONNECTION_INFO_AVAILABLE_ACTION));
/*		if (info.groupFormed && info.isGroupOwner) {
			ALog.i(TAG, "doWifiDirectConnectionAvailable is GroupOwner!!");
		} else if (info.groupFormed) {
		}*/
	}
 
Example #4
Source File: WiFiP2PCommand.java    From nfcspy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onConnectionInfoAvailable(WifiP2pInfo info) {

	if (info.groupFormed) {
		ctx.groupOwnerAddress = info.groupOwnerAddress;
		ctx.isGroupOwner = info.isGroupOwner;
		ctx.isWifiP2pConnected = true;
		onFinished(STA_SUCCESS, info);
	} else {
		ctx.groupOwnerAddress = null;
		ctx.isGroupOwner = false;
		ctx.isWifiP2pConnected = false;
		onFinished(STA_FAIL);
	}
}
 
Example #5
Source File: Salut.java    From Salut with MIT License 5 votes vote down vote up
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
    /* This method is automatically called when we connect to a device.
     * The group owner accepts connections using a server socket and then spawns a
     * client socket for every client. This is handled by the registration jobs.
     * This will automatically handle first time connections.*/

    manager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
        @Override
        public void onGroupInfoAvailable(WifiP2pGroup group) {

            if (isRunningAsHost && !registrationIsRunning) {
                if (info.groupFormed && !group.getClientList().isEmpty()) {
                    startHostRegistrationServer();
                }
            } else if (!thisDevice.isRegistered && !info.isGroupOwner) {
                if (serviceRequest == null) {
                    //This means that discoverNetworkServices was never called and we're still connected to an old host for some reason.
                    Log.e(Salut.TAG, "This device is still connected to an old host for some reason. A forced disconnect will be attempted.");
                    forceDisconnect();
                }
                Log.v(Salut.TAG, "Successfully connected to another device.");
                startRegistrationForClient(new InetSocketAddress(info.groupOwnerAddress.getHostAddress(), SALUT_SERVER_PORT));
            }
        }
    });
}
 
Example #6
Source File: WiFiDirectActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  // Extract the NetworkInfo
  String extraKey = WifiP2pManager.EXTRA_NETWORK_INFO;
  NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra(extraKey);

  // Check if we're connected
  if (networkInfo.isConnected()) {
    mWifiP2pManager.requestConnectionInfo(mWifiDirectChannel,
      new WifiP2pManager.ConnectionInfoListener() {
        public void onConnectionInfoAvailable(WifiP2pInfo info) {
          // If the connection is established
          if (info.groupFormed) {
            // If we're the server
            if (info.isGroupOwner) {
              // TODO Initiate server socket.
            }
            // If we're the client
            else if (info.groupFormed) {
              // TODO Initiate client socket.
            }
          }
        }
      });
  } else {
    Log.d(TAG, "Wi-Fi Direct Disconnected.");
  }
}
 
Example #7
Source File: WifiP2pInfoHelper.java    From android-p2p with MIT License 5 votes vote down vote up
public static boolean areInfosEquals(WifiP2pInfo info1, WifiP2pInfo info2) {
    if (info1 == null || info2 == null) {
        return false;
    } else {
        return (info1.groupFormed == info2.groupFormed) && (info1.isGroupOwner == info2.isGroupOwner) && (info1.groupOwnerAddress.equals(info2.groupOwnerAddress));
    }
}
 
Example #8
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 5 votes vote down vote up
public String p2pInfoToString(WifiP2pInfo wifiP2pInfo) {
    if (wifiP2pInfo != null) {
        String strWifiP2pInfo = "Group formed: " + wifiP2pInfo.groupFormed;
        strWifiP2pInfo += "\nIs group owner: " + wifiP2pInfo.isGroupOwner;
        strWifiP2pInfo += "\nGroup owner address: " + wifiP2pInfo.groupOwnerAddress;
        return strWifiP2pInfo;
    } else {
        Log.e(TAG, "WifiP2pInfo is null");
        return "";
    }
}
 
Example #9
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 5 votes vote down vote up
/**
     * The requested connection info is available
     * @param wifiP2pInfo Wi-Fi P2P connection info
     */
    @Override
    public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
        Log.i(TAG, "Connection info available");

        Log.i(TAG, "WifiP2pInfo: ");
        Log.i(TAG, p2pInfoToString(wifiP2pInfo));
        this.groupFormed = wifiP2pInfo.groupFormed;
        this.isGroupOwner = wifiP2pInfo.isGroupOwner;

        if (wifiP2pInfo.groupFormed) {
            if(stopDiscoveryAfterGroupFormed){
                stopServiceDiscovery();
            }

//            Thread handler;
            if (wifiP2pInfo.isGroupOwner && socketHandler == null) {
                Log.i(TAG, "Connected as group owner");
                try {
                    socketHandler = new OwnerSocketHandler(this.getHandler());
                    socketHandler.start();
                } catch (IOException e) {
                    Log.e(TAG, "Failed to create a server thread - " + e.getMessage());
                    return;
                }
            } else {
                Log.i(TAG, "Connected as peer");
                socketHandler = new ClientSocketHandler(this.getHandler(), wifiP2pInfo.groupOwnerAddress);
                socketHandler.start();
            }

//            localBroadcastManager.sendBroadcast(new Intent(Action.SERVICE_CONNECTED));
        } else {
            Log.w(TAG, "Group not formed");
        }
        localBroadcastManager.sendBroadcast(new Intent(Action.DEVICE_CHANGED));
    }
 
Example #10
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 接続した時のコールバックリスナーを呼び出す
 * @param info
 */
protected void callOnConnect(@NonNull final WifiP2pInfo info) {
	if (DEBUG) Log.v(TAG, "callOnConnect:");
	for (final WiFiP2pListener listener: mListeners) {
		try {
			listener.onConnect(info);
		} catch (final Exception e1) {
			Log.w(TAG, e1);
			mListeners.remove(listener);
		}
	}
}
 
Example #11
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
	if (DEBUG) Log.v(TAG, "onConnectionInfoAvailable:info=" + info);
	if (info != null) {
		callOnConnect(info);
	}
}
 
Example #12
Source File: WifiP2PServiceImpl.java    From Wifi-Connect with Apache License 2.0 4 votes vote down vote up
@Override
public void handleOnPeerServer(WifiP2pInfo wifiP2pInfo) {
    new DataServerTask(wifiP2PConnectionCallback, DEFAULT_WIFI_PORT).execute();
}
 
Example #13
Source File: DeviceDetailFragment.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
    Log.d(TAG, "onConnectionInfoAvailable");
    this.getView().setVisibility(View.VISIBLE);
}
 
Example #14
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
public WifiP2pInfo getWifiP2pInfo()
{
	return mWifiP2pInfo;
}
 
Example #15
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
@Override
public void onConnectionInfoAvailable(WifiP2pInfo info) {
	ALog.i(TAG, "onConnectionInfoAvailable info:" + info + ", mIsWifiP2pConnected:" + mIsWifiP2pConnected);
	doWifiDirectConnectionAvailable(info);
}
 
Example #16
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
public void setWifiP2pInfo(WifiP2pInfo info) {
	mWifiP2pInfo = info;
	ALog.i(TAG, "setWifiP2pInfo mWifiP2pInfo:" + mWifiP2pInfo);
}
 
Example #17
Source File: LeBoxNetworkManager.java    From letv with Apache License 2.0 4 votes vote down vote up
public void saveWifiP2pInfo(WifiP2pInfo wifiP2pInfo) {
    LeBoxWifiDirectModel.getInstance().setWifiP2pInfo(wifiP2pInfo);
}
 
Example #18
Source File: WifiP2PServiceImpl.java    From Wifi-Connect with Apache License 2.0 4 votes vote down vote up
@Override
public void handleOnPeerClient(WifiP2pInfo wifiP2pInfo, String message) {
    Intent serviceIntent = new Intent(activity, ServerDataService.class);
    serviceIntent.setAction(ACTION_SEND_FILE);
    serviceIntent.putExtra(EXTRAS_DATA, message);
    serviceIntent.putExtra(EXTRAS_GROUP_OWNER_ADDRESS, wifiP2pInfo.groupOwnerAddress == null ? "" : wifiP2pInfo.groupOwnerAddress.getHostAddress());
    serviceIntent.putExtra(EXTRAS_GROUP_OWNER_PORT, DEFAULT_WIFI_PORT);
    serviceIntent.putExtra(EXTRAS_RESULT_RECEIVER, new ResultReceiver(null) {
        @Override
        protected void onReceiveResult(int resultCode, final Bundle resultData) {
            if(resultCode == DEFAULT_WIFI_PORT && resultData != null) {
                int status = Integer.valueOf(String.valueOf(resultData.get(EXTRAS_MESSAGE)));
                switch (status) {
                    case STATUS_CONNECTING:
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                wifiP2PConnectionCallback.onDataTransferring();
                            }
                        });
                        break;

                    case STATUS_SUCCESS:
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                wifiP2PConnectionCallback.onDataTransferredSuccess();
                            }
                        });
                        break;

                    case STATUS_FAILURE:
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                wifiP2PConnectionCallback.onDataTransferredFailure();
                            }
                        });
                        break;
                }
            }
        }
    });
    activity.startService(serviceIntent);
}
 
Example #19
Source File: WiFiDirectManagementFragment.java    From commcare-android with Apache License 2.0 3 votes vote down vote up
@Override
public void onConnectionInfoAvailable(WifiP2pInfo info) {

    this.info = info;

    refreshStatusText();

    boolean isOwner = info.isGroupOwner;

    setDeviceConnected(info.groupFormed);

}
 
Example #20
Source File: WiFiP2pListener.java    From libcommon with Apache License 2.0 2 votes vote down vote up
/**
 * 機器へ接続した時のコールバックメソッド
 * @param info
 */
public void onConnect(@NonNull final WifiP2pInfo info);
 
Example #21
Source File: WifiP2PService.java    From Wifi-Connect with Apache License 2.0 votes vote down vote up
void handleOnPeerClient(WifiP2pInfo wifiP2pInfo, String message); 
Example #22
Source File: WifiP2PService.java    From Wifi-Connect with Apache License 2.0 votes vote down vote up
void handleOnPeerServer(WifiP2pInfo wifiP2pInfo);