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

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

Introduction

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

Prototype

public void discoverPeers(Channel c, ActionListener listener) 

Source Link

Document

Initiate peer discovery.

Usage

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 www.  ja  va2 s.  c o m
    }
    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;
}