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

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

Introduction

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

Prototype

public Channel initialize(Context srcContext, Looper srcLooper, ChannelListener listener) 

Source Link

Document

Registers the application with the Wi-Fi framework.

Usage

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

private void changeWifiName() {
    String deviceName = "PW-" + mTitle + "-" + mPort;
    if (deviceName.length() > MAX_DEVICE_NAME_LENGTH) {
        deviceName = DEFAULT_DEVICE_NAME + mPort;
    }/*from   w ww  .  ja v  a2  s .c  o  m*/
    try {
        WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
        Class[] paramTypes = new Class[3];
        paramTypes[0] = WifiP2pManager.Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = WifiP2pManager.ActionListener.class;
        Method setDeviceName = manager.getClass().getMethod("setDeviceName", paramTypes);
        setDeviceName.setAccessible(true);

        Object arglist[] = new Object[3];
        arglist[0] = channel;
        arglist[1] = deviceName;
        arglist[2] = new WifiP2pManager.ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "setDeviceName succeeded");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "setDeviceName failed");
            }
        };
        setDeviceName.invoke(manager, arglist);

    } catch (NoSuchMethodException e) {
        Log.d(TAG, e.getMessage());
    } catch (IllegalAccessException e) {
        Log.d(TAG, e.getMessage());
    } catch (IllegalArgumentException e) {
        Log.d(TAG, e.getMessage());
    } catch (InvocationTargetException e) {
        Log.d(TAG, e.getMessage());
    }
}

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  w w  w.  j ava2 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;
}

From source file:rocks.stalin.android.app.MusicService.java

@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");

    timeProvider = new LocalOffsetService();
    executorService = ServiceLocator.getInstance().getService(TaskExecutor.class);
    scheduler = ServiceLocator.getInstance().getService(TaskScheduler.class);

    mMusicProvider = new MusicProvider(new ExternalStorageSource(getApplicationContext()));
    //mMusicProvider = new MusicProvider();
    // To make the app more responsive, fetch and cache catalog information now.
    // This can help improve the response time in the method
    // {@link #onLoadChildren(String, Result<List<MediaItem>>) onLoadChildren()}.
    mMusicProvider.retrieveMediaAsync(null /* Callback */);

    mPackageValidator = new PackageValidator(this);

    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(),
            new QueueManager.MetadataUpdateListener() {
                @Override/*w ww. jav  a  2 s. c om*/
                public void onMetadataChanged(MediaMetadataCompat metadata) {
                    mSession.setMetadata(metadata);
                }

                @Override
                public void onMetadataRetrieveError() {
                    mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata));
                }

                @Override
                public void onCurrentQueueIndexUpdated(int queueIndex) {
                    //TODO: This causes mediaPlayer the mediaplayer to get restarted twice on song change
                    //There's currently a race condition there -JJ 21/05-2017
                    //mPlaybackManager.handlePlayRequest();
                }

                @Override
                public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
                    mSession.setQueue(newQueue);
                    mSession.setQueueTitle(title);
                }
            });

    LogHelper.i(TAG, "Starting manager");

    //TODO: extract port number somewhere else, possible even not set it
    TCPServerConnectionFactory connectionFactory = new TCPServerConnectionFactory(8009, executorService);
    connectionFactory.setListener(this);
    executorService.submit(connectionFactory);

    WifiP2pManager rawManager = getSystemService(WifiP2pManager.class);
    WifiP2pManager.Channel channel = rawManager.initialize(this, getMainLooper(), null);
    WifiP2PManagerFacade manager = new WifiP2PManagerFacade(rawManager, channel);
    server = new WifiP2pServiceAnnouncer(manager, 8009, executorService);
    server.start();

    remotePlayback = new RemotePlayback(this, mMusicProvider, timeProvider, scheduler);
    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, remotePlayback);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, NowPlayingActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    mSessionExtras = new Bundle();
    mSession.setExtras(mSessionExtras);

    mPlaybackManager.updatePlaybackState(null);

    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }

    mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
    mCastSessionManagerListener = new CastSessionManagerListener();
    mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class);

    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
}