Wifi p2p - Not able to execute async task

198 views Asked by At

I am trying to communicate between two android device. Its simple one as to act as server and other as

I am following wifiP2P https://developer.android.com/guide/topics/connectivity/wifip2p.html

    I have three classes 

    package com.example.manojkumar.wifidirect;

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.wifi.WifiManager;
    import android.net.wifi.WpsInfo;
    import android.net.wifi.p2p.WifiP2pConfig;
    import android.net.wifi.p2p.WifiP2pDevice;
    import android.net.wifi.p2p.WifiP2pDeviceList;
    import android.net.wifi.p2p.WifiP2pInfo;
    import android.net.wifi.p2p.WifiP2pManager;
    import android.nfc.Tag;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.net.wifi.p2p.WifiP2pManager.*;
    import android.util.Log;
    import android.widget.Toast;

    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.util.ArrayList;
    import java.util.List;


    public class MainActivity extends AppCompatActivity implements WifiP2pManager.ChannelListener {

        private final IntentFilter intentFilter = new IntentFilter();
        Channel channel;
        WifiP2pManager manager;
        BroadcastReceiverClass temp;
        public List<WifiP2pDevice> peers = new ArrayList();
        public static final String TAG = "Debugging";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            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);

            temp = new BroadcastReceiverClass(manager, channel, MainActivity.this);
            registerReceiver(temp, intentFilter);


            manager.discoverPeers(channel, new ActionListener() {
                @Override
                public void onSuccess() {
                    Log.d(TAG, "Success in discovering peers");
                    Toast.makeText(MainActivity.this, "Success DiscoverPeers ", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFailure(int i) {
                    Log.d(TAG, "Failed to discover Peers " + i);
                }
            });
        }

        @Override
        public void onResume() {
            super.onResume();
            registerReceiver(temp, intentFilter);
        }

        @Override
        public void onPause() {
            super.onPause();
            unregisterReceiver(temp);
        }

        @Override
        public void onChannelDisconnected() {

        }

    }

The second is is basically Broadcast receiver.

package com.example.manojkumar.wifidirect;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Manoj Kumar on 1/1/2017.
 */

public class BroadcastReceiverClass extends BroadcastReceiver {

    private WifiP2pManager wifiP2pManager;
    private WifiP2pManager.Channel channel;
    private Activity activity;
    public List<WifiP2pDevice> peers = new ArrayList();


    public BroadcastReceiverClass(WifiP2pManager wifiP2pManager, WifiP2pManager.Channel channel,
                                  Activity activity){
        this.wifiP2pManager = wifiP2pManager;
        this.channel = channel;
        this.activity = activity;

    }



    @Override
    public void onReceive(final Context context, Intent intent) {

        String action = intent.getAction();

        Log.d(MainActivity.TAG, "ACTION "+action);

        if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){
                Toast.makeText(context,"Wifi is enabled", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(context,"please enable your wifi", Toast.LENGTH_SHORT).show();
            }
        }

        else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){

            Log.d("DISCOVERED", "WIFI_P2P_PEERS_CHANGED_ACTION");
            if(wifiP2pManager !=null){
                wifiP2pManager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
                    @Override
                    public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
                        peers.clear();
                        peers.addAll(wifiP2pDeviceList.getDeviceList());

                        if (peers.size() == 0) {
                            Log.d(MainActivity.TAG, "No Device Found to connect");
                            return;
                        } else {
                            for (int i = 0; i < peers.size(); i++) {
                                Log.d(MainActivity.TAG, "Devices are " + peers.get(i).toString());
                            }


                            WifiP2pDevice device = peers.get(0);

                            final WifiP2pConfig config = new WifiP2pConfig();

                            config.deviceAddress = device.deviceAddress;
                            config.wps.setup = WpsInfo.PBC;

                            wifiP2pManager.connect(channel, config, new WifiP2pManager.ActionListener() {
                                @Override
                                public void onSuccess() {
                                    Log.d(MainActivity.TAG, "Connected -------------- Connected");
                                    DataTrasnsferAysnc dataTrasnsferAysnc = new DataTrasnsferAysnc(activity);
                                    dataTrasnsferAysnc.execute();
                                }

                                @Override
                                public void onFailure(int i) {
                                    Log.d(MainActivity.TAG, "Connect failed. Retry. " + i);
                                    Toast.makeText(activity, "Connect failed. Retry. " + i,
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                });
            }


        }
        else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){


        }
        else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){

        }
    }
}

Third is the asynctask( still working on it)

package com.example.manojkumar.wifidirect;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Manoj Kumar on 1/1/2017.
 */

public class DataTrasnsferAysnc extends AsyncTask<Void, Void, Void> {

    private Context context;
    private String temp = "Manoj Kumar";


    public DataTrasnsferAysnc(Context context){
        Log.d(MainActivity.TAG, "DataTransferAsync");
        this.context = context;
    }
    @Override
    protected Void doInBackground(Void... param) {

        try{

            ServerSocket serverSocket = new ServerSocket(8888);
            while(true) {
                Socket client = serverSocket.accept();
                Log.d(MainActivity.TAG, "Waiting.......");

                InputStream inputStream = client.getInputStream();
                serverSocket.close();
            }

        }
        catch (IOException e) {
            Log.d(MainActivity.TAG, "ServerSocket "+e);
            return null;
        }

    }
}

The problem is I get following logs in loops

01-02 03:34:24.278 8483-8483/com.example.manojkumar.wifidirect D/Debugging: ACTION android.net.wifi.p2p.PEERS_CHANGED
01-02 03:34:24.278 8483-8483/com.example.manojkumar.wifidirect D/DISCOVERED: WIFI_P2P_PEERS_CHANGED_ACTION
01-02 03:34:24.278 8483-8483/com.example.manojkumar.wifidirect D/Debugging: Connected -------------- Connected
01-02 03:34:24.278 8483-8483/com.example.manojkumar.wifidirect D/Debugging: DataTransferAsync
01-02 03:34:24.281 8483-8483/com.example.manojkumar.wifidirect D/Debugging: Devices are Device: Android_fabe
                                                                             deviceAddress: 52:2e:5c:e6:5e:e4
                                                                             primary type: 10-0050F204-5
                                                                             secondary type: null
                                                                             wps: 392
                                                                             grpcapab: 171
                                                                             devcapab: 37
                                                                             status: 1
                                                                             wfdInfo: WFD enabled: falseWFD DeviceInfo: 0
                                                                             WFD CtrlPort: 0
                                                                             WFD MaxThroughput: 0
01-02 03:34:24.286 8483-8483/com.example.manojkumar.wifidirect D/Debugging: Connected -------------- Connected
01-02 03:34:24.286 8483-8483/com.example.manojkumar.wifidirect D/Debugging: DataTransferAsync
01-02 03:34:24.286 8483-8483/com.example.manojkumar.wifidirect D/Debugging: ACTION android.net.wifi.p2p.PEERS_CHANGED
01-02 03:34:24.286 8483-8483/com.example.manojkumar.wifidirect D/DISCOVERED: WIFI_P2P_PEERS_CHANGED_ACTION
01-02 03:34:24.288 8483-8483/com.example.manojkumar.wifidirect D/Debugging: Devices are Device: Android_fabe
                                                                             deviceAddress: 52:2e:5c:e6:5e:e4
                                                                             primary type: 10-0050F204-5
                                                                             secondary type: null
                                                                             wps: 392
                                                                             grpcapab: 171
                                                                             devcapab: 37
                                                                             status: 1
                                                                             wfdInfo: WFD enabled: falseWFD DeviceInfo: 0
                                                                             WFD CtrlPort: 0
                                                                             WFD MaxThroughput: 0

thought I have called

DataTrasnsferAysnc dataTrasnsferAysnc = new DataTrasnsferAysnc(activity);
dataTrasnsferAysnc.execute();

The control doesnt reach doInBackground() fucntion.

1

There are 1 answers

0
JAD On

This can be fixed by calling "executeOnExecutor" instead of execute.

DataTrasnsferAysnc dataTrasnsferAysnc = new DataTrasnsferAysnc(activity);
dataTrasnsferAysnc.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

For more details please check here.

Hope this helps, Goodluck.