Android - Runnable variable

0

I'm developing a class for UDP communication with Android + an ESP82666, I only have one problem in it.

My Runnable "sender" can not get the List toSend updated, so the toSend is always empty.

What can I do?

package gameutil2d.project;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import android.os.Handler;
import android.util.Log;

import java.util.logging.LogRecord;

/**
 * Created by Henrique Becker on 08/04/2017.
 */
public class UDP {
    private DatagramSocket socket;
    private volatile List<String> toSend;
    private volatile List<String> received;
    private Context ctx;
    private Handler h1;
    private Handler h2;
    private String ip;
    private int port;
    public UDP(String ip, int port, Context ctx){
        toSend = new ArrayList<String>();
        received = new ArrayList<String>();
        try {
            this.ip=ip;
            this.port=port;
            socket = new DatagramSocket(port);
            //socket.bind(new InetSocketAddress(ip, port));
            //socket.connect(new InetSocketAddress(ip, port));
            this.ctx=ctx;
            h1 = new Handler();
            h2 = new Handler();
            h1.post(sender);
            h2.post(receiver);
        } catch (SocketException e) {
            Log.e("UDP",e.toString());
            e.printStackTrace();
        }
    }

    private Runnable sender = new Runnable(){
        @Override
        public void run() {
            try {
                ConnectivityManager connManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if(!toSend.isEmpty() && mWifi.isConnected()){
                    byte[] data = toSend.get(0).getBytes();
                    toSend.remove(0);
                    DatagramPacket packet = new DatagramPacket(data,data.length,InetAddress.getByName(ip),port);
                    try {
                        socket.send(packet);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            catch (Exception e){
                Log.e("UDP","sender:"+e.toString());
            }
            finally {
                h1.postDelayed(sender,100);
            }
        }
    };

    private String getFirstSend(){
        if(toSend.isEmpty())
            return null;
        String s = toSend.get(0);
        toSend.remove(0);
        return s;
    }

    private Runnable receiver = new Runnable() {
        @Override
        public void run() {
            try {
                /*byte data[] = new byte[128];
                DatagramPacket packet = new DatagramPacket(data, data.length);
                if(socket!=null && received.size()<100){
                    socket.receive(packet);
                    if(packet.getLength()>0){
                        received.add(new String(packet.getData()));
                    }
                }*/
                Log.i("I","a");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                h2.postDelayed(receiver,100);
            }
        }
    };

    public void sendMessage(String s){
        try{
            if(toSend.size()<100)
                toSend.add(s);
            Log.e("M:",toSend.get(toSend.size()-1));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
    
asked by anonymous 08.04.2017 / 22:43

0 answers