What/where is a WiFiDirectActivity

后端 未结 2 537
醉酒成梦
醉酒成梦 2021-01-25 07:37

I am attempting to understand and implement the (incomplete) code described in \"Creating P2P Connections with Wi-Fi\". This is to add as a separate class to my existing activit

2条回答
  •  不思量自难忘°
    2021-01-25 07:51

    /**
     * A BroadcastReceiver that notifies of important wifi p2p events.
     */
    public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
    
        private WifiP2pManager manager;
        private Channel channel;
        private WiFiDirectActivity activity;
    
        /**
         * @param manager WifiP2pManager system service
         * @param channel Wifi p2p channel
         * @param activity activity associated with the receiver
         */
        public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
                WiFiDirectActivity activity) {
            super();
            this.manager = manager;
            this.channel = channel;
            this.activity = activity;
        }
    
        /*
         * (non-Javadoc)
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
         * android.content.Intent)
         */
        @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);
                } else {
                    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()
                if (manager != null) {
                    manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
                            .findFragmentById(R.id.frag_list));
                }
                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
    
                    DeviceDetailFragment fragment = (DeviceDetailFragment) activity
                            .getFragmentManager().findFragmentById(R.id.frag_detail);
                    manager.requestConnectionInfo(channel, fragment);
                } else {
                    // It's a disconnect
                    activity.resetData();
                }
            } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
                DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                        .findFragmentById(R.id.frag_list);
                fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                        WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
    
            }
        }
    }
    

    Here is complete android sdk sample of WIFI Direct at github GITHUB

提交回复
热议问题