Java Code Examples for android.net.wifi.p2p.WifiP2pManager#initialize()

The following examples show how to use android.net.wifi.p2p.WifiP2pManager#initialize() . 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: 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 2
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public WifiDisplayController(Context context, Handler handler, Listener listener) {
    mContext = context;
    mHandler = handler;
    mListener = listener;

    mWifiP2pManager = (WifiP2pManager)context.getSystemService(Context.WIFI_P2P_SERVICE);
    mWifiP2pChannel = mWifiP2pManager.initialize(context, handler.getLooper(), null);

    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(mWifiP2pReceiver, intentFilter, null, mHandler);

    ContentObserver settingsObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            updateSettings();
        }
    };

    final ContentResolver resolver = mContext.getContentResolver();
    resolver.registerContentObserver(Settings.Global.getUriFor(
            Settings.Global.WIFI_DISPLAY_ON), false, settingsObserver);
    resolver.registerContentObserver(Settings.Global.getUriFor(
            Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON), false, settingsObserver);
    resolver.registerContentObserver(Settings.Global.getUriFor(
            Settings.Global.WIFI_DISPLAY_WPS_CONFIG), false, settingsObserver);
    updateSettings();
}
 
Example 3
Source File: WiFiDirectActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void initializeWiFiDirect() {
  mWifiP2pManager = (WifiP2pManager)getSystemService(Context.WIFI_P2P_SERVICE);

  mWifiDirectChannel = mWifiP2pManager.initialize(this, getMainLooper(),
    new WifiP2pManager.ChannelListener() {
      public void onChannelDisconnected() {
        Log.d(TAG, "Wi-Fi P2P channel disconnected.");
      }
    }
  );
}
 
Example 4
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 5 votes vote down vote up
/**
 * Registers the application with the Wi-Fi P2P framework
 * Initializes the P2P manager and gets a P2P communication channel
 */
public void registerP2p() {
    // Manages Wi-Fi P2P connectivity
    wifiP2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);

    // initialize() registers the app with the Wi-Fi P2P framework
    // Channel is used to communicate with the Wi-Fi P2P framework
    // Main Looper is the Looper for the main thread of the current process
    channel = wifiP2pManager.initialize(this, getMainLooper(), null);
    Log.i(TAG, "Registered with Wi-Fi P2P framework");
}
 
Example 5
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 6
Source File: WiFiP2PManager.java    From nfcspy with GNU General Public License v3.0 5 votes vote down vote up
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;
	}
}
 
Example 7
Source File: CommCareWiFiDirectActivity.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateSessionSafe(Bundle savedInstanceState) {
    super.onCreateSessionSafe(savedInstanceState);

    setContentView(R.layout.wifi_direct_main);

    myStatusText = this.findViewById(R.id.my_status_text);
    formCountText = this.findViewById(R.id.form_count_text);
    stateStatusText = this.findViewById(R.id.wifi_state_status);
    stateHeaderText = this.findViewById(R.id.wifi_state_header);

    String baseDir = this.getFilesDir().getAbsolutePath();

    baseDirectory = baseDir + "/" + Localization.get("wifi.direct.base.folder");
    // these are the two folders where form record folders can live.
    toBeTransferredDirectory = baseDirectory + "/transfer";
    toBeSubmittedDirectory = baseDirectory + "/submit";
    // these are temporary paths for storing received and unzipped forms
    zipFilePath = baseDirectory + "/formRecordZip.zip";
    receiveDirectory = baseDirectory + "/receive";
    receiveZipDirectory = receiveDirectory + "/zipDest";

    mManager = (WifiP2pManager)getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    if (savedInstanceState == null) {
        showChangeStateDialog();
    }
    uiController.setupUI();
}
 
Example 8
Source File: WifiUrlDeviceDiscoverer.java    From physical-web with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void startScanImpl() {
  mManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
  mChannel = mManager.initialize(mContext, mContext.getMainLooper(), null);
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
  mContext.registerReceiver(mReceiver, intentFilter);
  mIsRunning = true;
  discoverHelper(true);
}
 
Example 9
Source File: WifiController.java    From gilgamesh with GNU General Public License v3.0 5 votes vote down vote up
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);
                 }
             });
     
}
 
Example 10
Source File: WifiP2PServiceImpl.java    From Wifi-Connect with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void onCreate() {
    manager = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(activity.getApplicationContext(), activity.getMainLooper(), null);
    this.wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
}