@Override public void register(final int port, final boolean discover, Context context) { mP2PManager.initialize(context, context.getMainLooper(), new ChannelListener() { @Override public void onChannelDisconnected() { Log.d(TAG, "Channel disconnected..."); } }); mService.registerReceiver(mWifiReceiver, mWifiIntentFilter); final Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PORT, String.valueOf(port)); mServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, SERVICE_REG_TYPE, record); mP2PManager.addLocalService(mChannel, mServiceInfo, mLocalServiceListener); if (discover) { discover(); } }
void init(Context ctx, ChannelListener lsn) { wifip2p = (WifiP2pManager) ctx.getSystemService(WIFI_P2P_SERVICE); channel = wifip2p.initialize(ctx, ctx.getMainLooper(), lsn); if (wifip2p != null && channel != null) { isWifiP2pEnabled = true; status = STA_P2P_INITED; } else { isWifiP2pEnabled = false; status = STA_P2P_UNINIT; } }
public void init (GilgaService service) { mService = service; mWifiManager = (WifiP2pManager) mService.getSystemService(Context.WIFI_P2P_SERVICE); mWifiChannel = mWifiManager.initialize(mService, mService.getMainLooper(), new ChannelListener() { @Override public void onChannelDisconnected() { Log.d(GilgaService.TAG,"wifi p2p disconnected"); } }); WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance(); mWifiManager.addServiceRequest(mWifiChannel, serviceRequest, new ActionListener() { @Override public void onSuccess() { // Success! Log.d(TAG,"SUCCESS: added service request wifi name service"); } @Override public void onFailure(int code) { // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY Log.d(TAG,"FAILURED: added service request wifi name service: " + code); } }); }
/** * attempt to create channel until it's established */ protected void createChannel() { channel = manager.initialize( context, context.getMainLooper(), new ChannelListener() { @Override public void onChannelDisconnected() { Logg.d(TAG, "Lost wifi channel, reconnecting..."); createChannel(); } }); }