Example usage for android.content Context WIFI_P2P_SERVICE

List of usage examples for android.content Context WIFI_P2P_SERVICE

Introduction

In this page you can find the example usage for android.content Context WIFI_P2P_SERVICE.

Prototype

String WIFI_P2P_SERVICE

To view the source code for android.content Context WIFI_P2P_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.net.wifi.p2p.WifiP2pManager for handling management of Wi-Fi peer-to-peer connections.

Usage

From source file:test.microsoft.com.wifipairing.WifiBase.java

public boolean Start() {

    boolean ret = false;
    //just making sure that when we start we have the Wifi on,
    // after this point we'll respect if it is turned off
    WifiManager wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }// w w  w .  j  a  va 2  s .  c  om

    p2p = (WifiP2pManager) this.context.getSystemService(Context.WIFI_P2P_SERVICE);

    if (p2p == null) {
        Log.d("WifiBase", "This device does not support Wi-Fi Direct");
    } else {
        ret = true;
        channel = p2p.initialize(this.context, this.context.getMainLooper(), this);
    }

    mBRReceiver = new MainBCReceiver();
    filter = new IntentFilter();
    filter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    LocalBroadcastManager.getInstance(this.context).registerReceiver((mBRReceiver), filter);

    return ret;
}

From source file:comingle.directory.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialization stuff for P2P WiFi
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    // Indicates a change in the list of available peers.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_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);

    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);

    // wifiDir = new WifiDirectDirectory(this, ADMIN_PORT, REQ_CODE);
    wifiDir = new WifiDirectComingleDirectory(this, ADMIN_PORT, REQ_CODE);

    wifiDir.addDefaultRoleEstablishedAction();
    wifiDir.addDefaultConnectionEstablishedAction();

    final MainActivity self = this;
    DirectoryChangedListener dir_listener = new DirectoryChangedListener() {
        @Override/*from  www .  j a  v  a2  s  . com*/
        public void doDirectoryChangedAction(List<NodeInfo> peers, List<NodeInfo> added, List<NodeInfo> dropped,
                int role) {
            self.refresh();
        }
    };
    wifiDir.addDirectoryChangedListener(dir_listener);

    wifiDirList = new PeerInfoListFragment(R.layout.device_list, R.layout.row_devices, R.id.row_name,
            R.id.row_location, R.id.row_ip, R.id.row_role, R.id.my_name, R.id.my_location, R.id.my_ip,
            R.id.my_role, wifiDir);

    LocalNodeInfoAvailableListener node_listener = new LocalNodeInfoAvailableListener() {
        @Override
        public void doLocalNodeInfoAvailableAction(NodeInfo local, int role) {
            wifiDirList.refreshMyDevice();
        }
    };
    wifiDir.addLocalNodeInfoAvailableListener(node_listener);

    getSupportFragmentManager().beginTransaction().replace(R.id.frag_list, wifiDirList).commit();

}

From source file:edu.nust.distributed.downloader.ClientActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);

    wifichannel = wifiManager.initialize(this, getMainLooper(), null);
    wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);

    wifiClientReceiverIntentFilter = new IntentFilter();
    ;/*from   w  w  w.j  a  va2s  . co  m*/
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    connectedAndReadyToSendFile = false;
    filePathProvided = false;
    fileToSend = null;
    transferActive = false;
    clientServiceIntent = null;
    targetDevice = null;
    wifiInfo = null;

    registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);

    setClientFileTransferStatus("Client is currently idle");

    //setTargetFileStatus("testing");
}

From source file:com.example.locationproject.wifidirect.WiFiDirectActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*w w w .  ja  v a 2  s . co  m*/

    // add necessary intent values to be matched.

    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) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
    Log.d(TAG, "Deleting healthplus folder at slave");
    File f = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                    + "/location");
    if (f.exists()) {
        try {
            FileUtils.deleteDirectory(f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        Log.d("WifiDirectActivity", "downloads folder for slave is empty");
    }
    Log.d("WifiDirectACtivity", "calling FileLocatoe service");
    Intent i = new Intent(this, FileLocator.class);
    // Add extras to the bundle
    //i.putExtra("foo", "bar");
    // Start the service
    startService(i);
}

From source file:com.example.healthplus.wifidirect.WiFiDirectActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from ww w  .  j  a v  a2 s  .  c o m

    // add necessary intent values to be matched.

    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) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
    Log.d(TAG, "Deleting healthplus folder at slave");
    File f = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                    + "/healthplus");
    if (f.exists()) {
        try {
            FileUtils.deleteDirectory(f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        Log.d("WifiDirectActivity", "downloads folder for slave is empty");
    }
    Intent i = new Intent(this, FileLocator.class);
    // Add extras to the bundle
    //i.putExtra("foo", "bar");
    // Start the service
    startService(i);
}

From source file:android.csulb.edu.wifidirecttest.WiFiDirectActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);/*from w w w .  j a va2  s. c o m*/
    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = { Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.INTERNET,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE };

    ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);

    // add necessary intent values to be matched.

    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) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
}

From source file:com.example.ramesh.p2pfileshare.ClientActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    sendFileButton = (Button) findViewById(R.id.send_file_button);
    discoverPeersButton = (Button) findViewById(R.id.discover_peers_button);

    wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);

    wifichannel = wifiManager.initialize(this, getMainLooper(), null);
    wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);

    wifiClientReceiverIntentFilter = new IntentFilter();
    ;/*from  w  w  w .j  a va 2 s.co  m*/
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    connectedAndReadyToSendFile = false;
    filePathProvided = false;
    fileToSend = null;
    transferActive = false;
    clientServiceIntent = null;
    targetDevice = null;
    wifiInfo = null;

    registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);

    setClientFileTransferStatus("Client is currently idle");

    //setTargetFileStatus("testing");
}

From source file:org.physical_web.physicalweb.FileBroadcastService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mFileBroadcastServer != null) {
        mFileBroadcastServer.stop();/*from   ww  w . j  av a2  s  .c  om*/
    }
    mType = intent.getStringExtra(MIME_TYPE_KEY);
    Log.d(TAG, mType);
    mUri = Uri.parse(intent.getStringExtra(FILE_KEY));
    Log.d(TAG, mUri.toString());
    mTitle = intent.getStringExtra(TITLE_KEY);
    mTitle = mTitle == null ? "Share" : mTitle;
    mPort = Utils.getWifiDirectPort(this);
    try {
        mFile = Utils.getBytes(getContentResolver().openInputStream(mUri));
    } catch (FileNotFoundException e) {
        Log.d(TAG, e.getMessage());
        stopSelf();
        return START_STICKY;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        stopSelf();
        return START_STICKY;
    }
    mNotificationManager = NotificationManagerCompat.from(this);
    mFileBroadcastServer = new FileBroadcastServer(mPort, mType, mFile);
    try {
        mFileBroadcastServer.start();
        Utils.createBroadcastNotification(this, stopServiceReceiver, BROADCASTING_NOTIFICATION_ID,
                getString(R.string.wifi_direct_notification_title), Integer.toString(mPort), "myFilter2");
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        stopSelf();
        return START_STICKY;
    }
    sendBroadcast(new Intent("server"));
    WifiP2pManager mManager = (WifiP2pManager) this.getSystemService(Context.WIFI_P2P_SERVICE);
    WifiP2pManager.Channel mChannel = mManager.initialize(this, this.getMainLooper(), null);
    changeWifiName();
    mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.d(TAG, "discovering");
        }

        @Override
        public void onFailure(int reasonCode) {
            Log.d(TAG, "discovery failed " + reasonCode);
        }
    });
    Toast.makeText(this, R.string.wifi_direct_broadcasting_confirmation, Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

From source file:com.prgpascal.qrdatatransfer.TransferActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Prevent from Screen rotation
    MyCustomScreenOrientationManager.lockScreen(TransferActivity.this);

    // First, get the Intent Extras...
    checkAndGetIntentExtras(getIntent().getExtras());

    // Add the required intent filter values for the WIFI Direct connection
    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);

    // Initialize the WiFiP2PManager and get the Channel
    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);

    // Delete previous persistent Wifi Direct groups
    deletePreviousPersistentGroups();/*from   w  w w  . j  a va 2  s .  com*/

    // Create the layout
    createLayout();
}

From source file:com.example.android.wifidirect.WiFiDirectActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*from   ww w . j  a v a  2  s  . c o  m*/
    setOverflowShowingAlways();
    dm = getResources().getDisplayMetrics();
    pager = (ViewPager) findViewById(R.id.pager);
    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    //ViewPagerAdapter
    mypagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(mypagerAdapter);
    //ViewPagerFragment
    pager.setOffscreenPageLimit(2);
    int i = mypagerAdapter.getCount();
    // ViewPagerPagerSlidingTabStrip
    tabs.setViewPager(pager);
    // PagerSlidingTabStrip?
    setTabsValue();

    // add necessary intent values to be matched.

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
    filetransmitfragment = new FileTransmitFragment(manager, channel, this); //,intentFilter

    //?fragmenthandler??
    filelistfragment = new NewFileListFragment();

}