Example usage for android.net.wifi.p2p WifiP2pManager WIFI_P2P_STATE_CHANGED_ACTION

List of usage examples for android.net.wifi.p2p WifiP2pManager WIFI_P2P_STATE_CHANGED_ACTION

Introduction

In this page you can find the example usage for android.net.wifi.p2p WifiP2pManager WIFI_P2P_STATE_CHANGED_ACTION.

Prototype

String WIFI_P2P_STATE_CHANGED_ACTION

To view the source code for android.net.wifi.p2p WifiP2pManager WIFI_P2P_STATE_CHANGED_ACTION.

Click Source Link

Document

Broadcast intent action to indicate whether Wi-Fi p2p is enabled or disabled.

Usage

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

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

        // UI update to indicate wifi p2p status.
        int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
        if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
            // Wifi Direct mode is enabled
            activity.setIsWifiP2pEnabled(true); //Wi-Fi direct??
        } else {//from w  w w .j a v  a 2s.  c o  m
            activity.setIsWifiP2pEnabled(false);
            activity.resetData();

        }
        Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

        // request available peers from the wifi p2p manager. This is an
        // asynchronous call and the calling activity is notified with a
        // callback on PeerListListener.onPeersAvailable()
        //Wi-Fi P2P??PeerListListener.onPeersAvailable()?
        if (manager != null) {
            FileTransmitFragment fileTransmitfragment = (FileTransmitFragment) activity
                    .getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":0");
            DeviceListFragment fragment = (DeviceListFragment) fileTransmitfragment.getFragmentManager()
                    .findFragmentById(R.id.frag_list);
            manager.requestPeers(channel, (PeerListListener) fragment);
        }
        Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

        if (manager == null) {
            return;
        }

        NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

        if (networkInfo.isConnected()) {

            // we are connected with the other device, request connection
            // info to find group owner IP
            FileTransmitFragment fileTransmitfragment = (FileTransmitFragment) activity
                    .getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":0");
            DeviceDetailFragment fragment = (DeviceDetailFragment) fileTransmitfragment.getFragmentManager()
                    .findFragmentById(R.id.frag_detail);
            manager.requestConnectionInfo(channel, fragment); //??onConnectionInfoAvailable
        } else {
            // It's a disconnect       
            activity.resetData();
        }
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { //????
        //DeviceListFragment fragment = (DeviceListFragment) activity.getSupportFragmentManager().findFragmentByTag("devicelistfragment");
        FileTransmitFragment fileTransmitfragment = (FileTransmitFragment) activity.getSupportFragmentManager()
                .findFragmentByTag("android:switcher:" + R.id.pager + ":0");
        DeviceListFragment fragment = (DeviceListFragment) fileTransmitfragment.getFragmentManager()
                .findFragmentById(R.id.frag_list);
        fragment.updateThisDevice(
                (WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
        //activity.getSupportFragmentManager().beginTransaction().add(R.id.list_fragment, fragment).commit();

        //DeviceDetailFragment fragment2 = (DeviceDetailFragment) fileTransmitfragment.getFragmentManager().findFragmentById(R.id.frag_detail);
        //fragment2.getrootView().setVisibility(View.VISIBLE);

        /*Log.d(WiFiDirectActivity.TAG,"Fragment2:"+fragment2);
        Log.d(WiFiDirectActivity.TAG,"Fragment2.getView():"+fragment2.getView());
        Log.d(WiFiDirectActivity.TAG,"Fragment2.getrootView():"+fragment2.getrootView());
        Log.d(WiFiDirectActivity.TAG, "Fragment:"+fileTransmitfragment);
        Log.d(WiFiDirectActivity.TAG, "device:"+(WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));*/
    }
}

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  w w w . java  2s  . co  m*/
        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: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  .ja  v a2  s.c o m

    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:com.example.locationproject.wifidirect.WiFiDirectActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/* w  w  w  . j av 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()
                    + "/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  w w  w.  j  a v  a2s  .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  ww  .  j a v a2  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: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 ww  .  ja v a  2s .  c o 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.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 ww  w.  ja v  a 2  s.  c  om

    // Create the layout
    createLayout();
}

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();
    ;/*  ww  w . j  a  v a 2  s  .  c  om*/
    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.monkey.entonado.MainActivity.java

/**
 * Inicializa la actividad//from  www  . j  a  v  a  2  s  .  com
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    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);

    btnBuscar = (TextView) findViewById(R.id.btnBuscar);
    btnBuscar.setOnClickListener(this);

    radioTitulo = (Button) findViewById(R.id.btnTitulo);
    radioArtista = (Button) findViewById(R.id.btnArtista);

    campoBusqueda = (EditText) findViewById(R.id.campoBusqueda);

    busqueda = (TextView) findViewById(R.id.txtBusqueda);

    out = null;
    in = null;

    canciones = new ArrayList();
    milista = new ArrayList();

    mensajeConexion = "No se estableci la conexion";
    intentoConectar = false;
    ip = "";

}