android.net.wifi.p2p.WifiP2pManager Java Examples

The following examples show how to use android.net.wifi.p2p.WifiP2pManager. 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: P2pReceiver.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
public void connect(WifiP2pDevice device)
{
    WifiP2pConfig config=new WifiP2pConfig();

    config.deviceAddress=device.deviceAddress;
    config.wps.setup= WpsInfo.PBC;

    _wifiP2pManager.connect(_channel, config, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {

        }

        @Override
        public void onFailure(int reason) {

        }
    });
}
 
Example #2
Source File: Salut.java    From Salut with MIT License 6 votes vote down vote up
public void cancelConnecting() {
    manager.cancelConnect(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.v(TAG, "Attempting to cancel connect.");
        }

        @Override
        public void onFailure(int reason) {
            Log.v(TAG, "Failed to cancel connect, the device may not have been trying to connect.");
        }
    });

    stopServiceDiscovery(true);
    connectingIsCanceled = true;
}
 
Example #3
Source File: WifiUrlDeviceDiscoverer.java    From physical-web with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void stopScanImpl() {
  mIsRunning = false;
  mContext.unregisterReceiver(mReceiver);
  mManager.stopPeerDiscovery(mChannel, new WifiP2pManager.ActionListener() {
    @Override
    public void onSuccess() {
      Log.d(TAG, "stop discovering");
    }

    @Override
    public void onFailure(int reasonCode) {
      Log.d(TAG, "stop discovery failed " + reasonCode);
    }
  });
}
 
Example #4
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    String action = intent.getAction();

    if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
        // The list of discovered peers has changed
        handlePeersChanged(intent);
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
        // The state of Wi-Fi P2P connectivity has changed
        handleConnectionChanged(intent);
    } else if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
        // Indicates whether Wi-Fi P2P is enabled
        handleP2pStateChanged(intent);
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
        // Indicates this device's configuration details have changed
        handleThisDeviceChanged(intent);
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
        handleWifiStateChanged(intent);
    } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
        handleScanResultsAvailable(intent);
    }
}
 
Example #5
Source File: ActivityManageP2P.java    From nfcspy with GNU General Public License v3.0 6 votes vote down vote up
void handleBroadcast(Intent intent) {
	closeProgressDialog();

	final String action = intent.getAction();
	if (WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
		int state = intent.getIntExtra(EXTRA_WIFI_STATE, -1);
		if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
			p2p.isWifiP2pEnabled = true;
		} else {
			showMessage(R.string.event_p2p_disable);
			resetData();
		}
	} else if (WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
		new Wifip2pRequestPeers(eventHelper).execute(p2p);

	} else if (WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
		WifiP2pDevice me = (WifiP2pDevice) intent
				.getParcelableExtra(EXTRA_WIFI_P2P_DEVICE);

		thisDevice.setText(getWifiP2pDeviceInfo(me));
	}
}
 
Example #6
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
/**
 * Initiates a connection to a service
 * @param service The service to connect to
 */
public void initiateConnectToService(DnsSdService service) {
    // Device info of peer to connect to
    WifiP2pConfig wifiP2pConfig = new WifiP2pConfig();
    wifiP2pConfig.deviceAddress = service.getSrcDevice().deviceAddress;
    wifiP2pConfig.wps.setup = WpsInfo.PBC;

    // Starts a peer-to-peer connection with a device with the specified configuration
    wifiP2pManager.connect(channel, wifiP2pConfig, new WifiP2pManager.ActionListener() {
        // The ActionListener only notifies that initiation of connection has succeeded or failed

        @Override
        public void onSuccess() {
            Log.i(TAG, "Initiating connection to service");
        }

        @Override
        public void onFailure(int reason) {
            Log.e(TAG, "Failure initiating connection to service: " + FailureReason.fromInteger(reason).toString());
        }
    });
}
 
Example #7
Source File: Salut.java    From Salut with MIT License 6 votes vote down vote up
public void createGroup(final SalutCallback onSuccess, final SalutCallback onFailure) {
    manager.createGroup(channel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.v(TAG, "Successfully created group.");
            Log.d(TAG, "Successfully created " + thisDevice.serviceName + " service running on port " + thisDevice.servicePort);
            isRunningAsHost = true;
            if (onSuccess != null) {
                onSuccess.call();
            }
        }

        @Override
        public void onFailure(int reason) {
            Log.e(TAG, "Failed to create group. Reason :" + reason);
            if (onFailure != null)
                onFailure.call();
        }
    });

}
 
Example #8
Source File: Salut.java    From Salut with MIT License 6 votes vote down vote up
private void connectToDevice(final SalutDevice device, final SalutCallback onFailure) {

        WifiP2pConfig config = new WifiP2pConfig();
        config.deviceAddress = device.macAddress;
        manager.connect(channel, config, new WifiP2pManager.ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "Attempting to connect to another device.");
                lastConnectedDevice = device;
            }

            @Override
            public void onFailure(int reason) {
                onFailure.call();
                Log.e(TAG, "Failed to connect to device. ");
            }
        });
    }
 
Example #9
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
private void clearServiceDiscoveryRequests() {
    if (serviceRequest != null) {
        wifiP2pManager.clearServiceRequests(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                serviceRequest = null;
                Log.i(TAG, "Service discovery requests cleared");
            }

            @Override
            public void onFailure(int reason) {
                Log.e(TAG, "Failure clearing service discovery requests: " + FailureReason.fromInteger(reason).toString());
            }
        });
    }
}
 
Example #10
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
/**
 * Removes a registered local service.
 */
public void removeService() {
    if(wifiP2pServiceInfo != null) {
        Log.i(TAG, "Removing local service");
        wifiP2pManager.removeLocalService(channel, wifiP2pServiceInfo, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                wifiP2pServiceInfo = null;
                Intent intent = new Intent(Action.SERVICE_REMOVED);
                localBroadcastManager.sendBroadcast(intent);
                Log.i(TAG, "Local service removed");
            }

            @Override
            public void onFailure(int reason) {
                Log.e(TAG, "Failure removing local service: " + FailureReason.fromInteger(reason).toString());
            }
        });
        wifiP2pServiceInfo = null;
    } else {
        Log.w(TAG, "No local service to remove");
    }
}
 
Example #11
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
/**
 * Removes the current WifiP2pGroup in the WifiP2pChannel.
 */
public void removeGroup() {
    if (wifiP2pGroup != null) {
        wifiP2pManager.removeGroup(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                wifiP2pGroup = null;
                groupFormed = false;
                isGroupOwner = false;
                Log.i(TAG, "Group removed");
            }

            @Override
            public void onFailure(int reason) {
                Log.e(TAG, "Failure removing group: " + FailureReason.fromInteger(reason).toString());
            }
        });
    }
}
 
Example #12
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 #13
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
/**
 * Removes persistent/remembered groups
 *
 * Source: https://android.googlesource.com/platform/cts/+/jb-mr1-dev%5E1%5E2..jb-mr1-dev%5E1/
 * Author: Nick  Kralevich <[email protected]>
 *
 * WifiP2pManager.java has a method deletePersistentGroup(), but it is not accessible in the
 * SDK. According to Vinit Deshpande <[email protected]>, it is a common Android paradigm to
 * expose certain APIs in the SDK and hide others. This allows Android to maintain stability and
 * security. As a workaround, this removePersistentGroups() method uses Java reflection to call
 * the hidden method. We can list all the methods in WifiP2pManager and invoke "deletePersistentGroup"
 * if it exists. This is used to remove all possible persistent/remembered groups. 
 */
private void removePersistentGroups() {
    try {
        Method[] methods = WifiP2pManager.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals("deletePersistentGroup")) {
                // Remove any persistent group
                for (int netid = 0; netid < 32; netid++) {
                    methods[i].invoke(wifiP2pManager, channel, netid, null);
                }
            }
        }
        Log.i(TAG, "Persistent groups removed");
    } catch(Exception e) {
        Log.e(TAG, "Failure removing persistent groups: " + e.getMessage());
        e.printStackTrace();
    }
}
 
Example #14
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * WiFiP2pHelperインスタンスをシステムに登録
 */
public synchronized void register() {
	if (DEBUG) Log.v(TAG, "register:");
	final Context context = mWeakContext.get();
	if ((context != null) & (mReceiver == null)) {
		mChannel = mWifiP2pManager.initialize(context,
			context.getMainLooper(), mChannelListener);
		mReceiver = new WiFiDirectBroadcastReceiver(mWifiP2pManager, mChannel, this);
		final IntentFilter intentFilter = new IntentFilter();
		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);
		context.registerReceiver(mReceiver, intentFilter);
	}
}
 
Example #15
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定した機器へ接続を試みる
 * @param config
 * @throws IllegalStateException
 */
public void connect(@NonNull final WifiP2pConfig config) throws IllegalStateException {
	if (DEBUG) Log.v(TAG, "connect:config=" + config);
	if (mChannel != null) {
		mWifiP2pManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
			@Override
			public void onSuccess() {
				// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
			}
			@Override
			public void onFailure(int reason) {
				callOnError(new RuntimeException("failed to connect, reason=" + reason));
			}
		});
	} else {
		throw new IllegalStateException("not registered");
	}
}
 
Example #16
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 切断する
 */
protected void internalDisconnect(final WifiP2pManager.ActionListener listener) {
	if (DEBUG) Log.v(TAG, "internalDisconnect:");
	if (mWifiP2pManager != null) {
		if ((mWifiP2pDevice == null)
			|| (mWifiP2pDevice.status == WifiP2pDevice.CONNECTED)) {
			// 接続されていないか、既に接続済みの時
			if (mChannel != null) {
				mWifiP2pManager.removeGroup(mChannel, listener);
			}
		} else if (mWifiP2pDevice.status == WifiP2pDevice.AVAILABLE
			|| mWifiP2pDevice.status == WifiP2pDevice.INVITED) {

			// ネゴシエーション中の時
			mWifiP2pManager.cancelConnect(mChannel, listener);
		}
	}
}
 
Example #17
Source File: WiFiDirectActivity.java    From Demo_Public with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    this.setContentView(R.layout.main);
    //��ʼ��WifiP2pManager
    mManager = (WifiP2pManager)getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    
    //������Ҫ������action
    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);
    
    
}
 
Example #18
Source File: WiFiDirectActivity.java    From Demo_Public with MIT License 6 votes vote down vote up
@Override
public void cancelDisconnect() {
    if(mManager != 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){
            mManager.cancelConnect(mChannel, new WifiP2pManager.ActionListener() {
                
                @Override
                public void onSuccess() {
                    
                }
                
                @Override
                public void onFailure(int reason) {
                    
                }
            });
        }
    }
}
 
Example #19
Source File: Salut.java    From Salut with MIT License 6 votes vote down vote up
protected void forceDisconnect() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        WifiP2pManager.ActionListener doNothing = new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onFailure(int reason) {

            }
        };

        stopServiceDiscovery(false);
        manager.cancelConnect(channel, doNothing);
        manager.clearLocalServices(channel, doNothing);
        manager.clearServiceRequests(channel, doNothing);
        manager.stopPeerDiscovery(channel, doNothing);
    }
}
 
Example #20
Source File: WiFiDirectActivity.java    From Demo_Public with MIT License 6 votes vote down vote up
@Override
public void disconnect() {
    final DeviceDetailFragment fragment = (DeviceDetailFragment)getFragmentManager().findFragmentById(R.id.frag_detail);
    fragment.resetViews();
    mManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {
        
        @Override
        public void onSuccess() {
            fragment.getView().setVisibility(View.GONE);
        }
        
        @Override
        public void onFailure(int reason) {
            Log.e(WiFiDirectActivity.TAG, "disconnect faile reason: "+reason);
        }
    });
}
 
Example #21
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
/**
 * Registers a WifiDirectBroadcastReceiver with an IntentFilter listening for P2P Actions
 */
public void registerP2pReceiver() {
    p2pBroadcastReceiver = new WifiDirectBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();

    // Indicates a change in the list of available peers
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    // Indicates a change in the Wi-Fi P2P status
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    // Indicates the state of Wi-Fi P2P connectivity has changed
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    // Indicates this device's details have changed.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    registerReceiver(p2pBroadcastReceiver, intentFilter);
    Log.i(TAG, "P2P BroadcastReceiver registered");
}
 
Example #22
Source File: WifiP2PServiceImpl.java    From Wifi-Connect with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void handleOnPeersChangedResponse() {
    manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(final WifiP2pDeviceList wifiP2pDeviceList) {
            if(wifiP2PConnectionCallback != null) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        wifiP2PConnectionCallback.onPeerAvailable(wifiP2pDeviceList);
                    }
                });
            }
        }
    });
}
 
Example #23
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 切断する
 */
public synchronized void disconnect() {
	if (DEBUG) Log.v(TAG, "disconnect:");
	internalDisconnect(new WifiP2pManager.ActionListener() {
		@Override
		public void onSuccess() {
		}
		@Override
		public void onFailure(final int reason) {
			callOnError(new RuntimeException("failed to disconnect, reason=" + reason));
		}
	});
}
 
Example #24
Source File: Salut.java    From Salut with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void stopNetworkService(final boolean disableWiFi) {
    if (isRunningAsHost) {
        Log.v(TAG, "Stopping network service...");
        stopServiceDiscovery(true);
        closeDataSocket();
        closeRegistrationSocket();

        if (manager != null && channel != null && serviceInfo != null) {

            manager.removeLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() {
                @Override
                public void onFailure(int reason) {
                    Log.d(TAG, "Could not end the service. Reason : " + reason);
                }

                @Override
                public void onSuccess() {
                    Log.v(TAG, "Successfully shutdown service.");
                    if (disableWiFi) {
                        disableWiFi(dataReceiver.context); //Called here to give time for request to be disposed.
                    }
                    isRunningAsHost = false;
                }
            });

            respondersAlreadySet = false;
        }

    } else {
        Log.d(TAG, "Network service is not running.");
    }
}
 
Example #25
Source File: WiFiP2pHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
   * @param manager WifiP2pManager system service
   * @param channel Wifi p2p channel
   * @param parent associated with the receiver
   */
  public WiFiDirectBroadcastReceiver(
  	@NonNull final WifiP2pManager manager, @NonNull final WifiP2pManager.Channel channel,
@NonNull final WiFiP2pHelper parent) {

      super();
      mManager = manager;
      mChannel = channel;
      mParent = parent;
  }
 
Example #26
Source File: Salut.java    From Salut with MIT License 5 votes vote down vote up
private void deleteGroup(WifiP2pManager manager, WifiP2pManager.Channel channel, WifiP2pGroup wifiP2pGroup) {
    try {
        Method getNetworkId = WifiP2pGroup.class.getMethod("getNetworkId");
        Integer networkId = (Integer) getNetworkId.invoke(wifiP2pGroup);
        Method deletePersistentGroup = WifiP2pManager.class.getMethod("deletePersistentGroup",
                WifiP2pManager.Channel.class, Integer.class, WifiP2pManager.ActionListener.class);
        deletePersistentGroup.invoke(manager, channel, networkId, null);
    } catch (Exception ex) {
        Log.v(Salut.TAG, "Failed to delete persistent group.");
    }
}
 
Example #27
Source File: Salut.java    From Salut with MIT License 5 votes vote down vote up
public Salut(SalutDataReceiver dataReceiver, SalutServiceData salutServiceData, SalutCallback deviceNotSupported) {
    WifiManager wifiMan = (WifiManager) dataReceiver.context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMan.getConnectionInfo();

    this.dataReceiver = dataReceiver;
    this.deviceNotSupported = deviceNotSupported;
    this.TTP = salutServiceData.serviceData.get("SERVICE_NAME") + TTP;

    thisDevice = new SalutDevice();
    thisDevice.serviceName = salutServiceData.serviceData.get("SERVICE_NAME");
    thisDevice.readableName = salutServiceData.serviceData.get("INSTANCE_NAME");
    thisDevice.instanceName = "" + wifiInfo.getMacAddress().hashCode();
    thisDevice.macAddress = wifiInfo.getMacAddress();
    thisDevice.TTP = thisDevice.serviceName + TTP;
    thisDevice.servicePort = Integer.valueOf(salutServiceData.serviceData.get("SERVICE_PORT"));
    thisDevice.txtRecord = salutServiceData.serviceData;

    foundDevices = new ArrayList<>();

    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) dataReceiver.context.getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(dataReceiver.context, dataReceiver.context.getMainLooper(), new WifiP2pManager.ChannelListener() {
        @Override
        public void onChannelDisconnected() {
            Log.d(TAG, "Attempting to reinitialize channel.");
            channel = manager.initialize(Salut.this.dataReceiver.context, Salut.this.dataReceiver.context.getMainLooper(), this);
        }
    });

    receiver = new SalutBroadcastReceiver(this, manager, channel);
}
 
Example #28
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 #29
Source File: PeerActivity.java    From android-p2p with MIT License 5 votes vote down vote up
private void connectPeer(WifiP2pDevice peer) {
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = peer.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    this.Application.P2pHandler.Manager.connect(this.Application.P2pHandler.Channel, config, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Toast.makeText(PeerActivity.this, "Connecting to peer ...", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFailure(int reason) {
            Toast.makeText(PeerActivity.this, "Peer connection failed with code " + Integer.toString(reason), Toast.LENGTH_SHORT).show();
        }
    });
}
 
Example #30
Source File: WiFiDirectBroadcastReceiver.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
/**
 * @param manager  WifiP2pManager system service
 * @param activity activity associated with the receiver
 */
public WiFiDirectBroadcastReceiver(WifiP2pManager manager,
                                   WiFiDirectManagementFragment activity) {
    super();
    this.manager = manager;
    this.activity = activity;
}