Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:56:59+00:00 2026-06-17T16:56:59+00:00

I want to use asynctask to send a datagram packet, but it not work!!

  • 0

I want to use asynctask to send a datagram packet, but it not work!!

I have add all permission that may need!

I think that the problem may be in the asynctask of

      socket.send(packet);

please help me solve this 🙂

  "In my XML file"
  <uses-permission 
    android:name="android.permission.INTERNET"></uses-permission>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-   permission>
 <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"></uses-permission>



public class DroneMain extends Activity {
String at_cmd = "";
String action = "";
FloatBuffer fb=null;
IntBuffer ib=null;
int   PORT=5556;
float speed = (float)0.1;
int   seq = 1;
public Handler handler ;
InetAddress    inet_addr;
DatagramSocket socket;
public Button btnForward,btnBackward,btnLeft,btnRight
       ,btnEmrgency,btnTakeoff,btnLanding,btnUp,btnDown;
   ///////////////////////////////////////MAIN//////////////////////////////////////
   @Override
       public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_drone_main);

 /////////////////////Button Finder//////////////////////
       btnForward=(Button)findViewById(R.id.forwardBtn);
       btnBackward=(Button)findViewById(R.id.backwardBtn);
       btnLeft=(Button)findViewById(R.id.LeftBtn);
       btnRight=(Button)findViewById(R.id.downBtn);
       btnUp=(Button)findViewById(R.id.upBtn);
       btnDown=(Button)findViewById(R.id.downBtn);
       btnEmrgency=(Button)findViewById(R.id.emergencyBtn);
       btnTakeoff=(Button)findViewById(R.id.takeoffBtn);
       btnLanding=(Button)findViewById(R.id.LandingBtn);
 /////////////////////Button Listener///////////////////////
       btnForward.setOnClickListener(btnForwardListener);
       btnBackward.setOnClickListener(BackwardListener);
       btnLeft.setOnClickListener(btnLeftListener);
       btnRight.setOnClickListener(btnRightListener);
       btnUp.setOnClickListener(btnUpListener);
       btnDown.setOnClickListener(btnDownListener);
       btnEmrgency.setOnClickListener(btnEmrgencyListener);
       btnTakeoff.setOnClickListener(btnTakeoffListener);
       btnLanding.setOnClickListener(btnLandingListener);
       Log.e("///","end of on create");
}
 ///////////////////////////////////////end of on create
 public int intOfFloat(float f) {
    /*fb.put(0, f);
    return ib.get(0);*/
    if(fb != null) fb.put(0, f);
    if(ib != null) return ib.get(0);
    else return 0;
 }

 public Button.OnClickListener btnForwardListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        Log.e("///button","forward_enter1");
        action = "Go Forward (pitch+)";         
            at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(speed)+ ",0,0,0";                
        new ATcommandThread().execute();            
    }

};

public Button.OnClickListener BackwardListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        action = "Go Backward (pitch-)";
            at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(-speed) + ",0,0,0";      
        //AT*PCMD=1,0,1036831949,0,0 'backward
            new ATcommandThread().execute();            

    }

};

public Button.OnClickListener btnLeftListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
         action = "Go Left (roll-)";
         // at_cmd = "AT*PCMD=" + (seq++) + ",1,0," + intOfFloat(-speed) + ",0,0";
         at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(-speed)+",0,0,0";
         //AT*PCMD=1,-1110651699,0,0,0 'safe left
         new ATcommandThread().execute();           
    }

};

public Button.OnClickListener btnRightListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
         action = "Go Right (roll+)";
         at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(+speed) + ",0,0,0";
         //AT*PCMD=1,1036831949,0,0,0 ' safe right
         Log.e("AT*PCMD",at_cmd);
         new ATcommandThread().execute();           

    }

};

public Button.OnClickListener btnUpListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
          action = "Go Up (gaz+)";
          at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0," + intOfFloat(speed) + ",0";
          new ATcommandThread().execute();          
    }

};

public Button.OnClickListener btnDownListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
         action = "Go Down (gaz-)";
         at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0," + intOfFloat(-speed) + ",0";
         //Move the drone Sets the reference for the horizontal plane
         new ATcommandThread().execute();           

    }

};

public Button.OnClickListener btnEmrgencyListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        action = "Emergency";
        at_cmd = "AT*REF=" + (seq++) + ",290717952";
        new ATcommandThread().execute();            

    }

};

public Button.OnClickListener btnTakeoffListener=
    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        action = "Takeoff";
        at_cmd = "AT*REF=" + (seq++) + ",290718208";
        new ATcommandThread().execute();            

    }

};

public Button.OnClickListener btnLandingListener=

    new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        action = "Landing";
        at_cmd = "AT*REF=" + (seq++) + ",290717696";
        new ATcommandThread().execute();            

    }

};
    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_drone_main, menu);     
    return true;
}

public class ATcommandThread extends AsyncTask<String, Integer, Boolean>{

    @Override
    protected Boolean doInBackground(String... arg0) {
        byte [] ip_bytes = new byte[] {(byte)192,(byte)168,(byte)1,(byte)1};
        try {
            inet_addr = InetAddress.getByAddress(ip_bytes );
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.e("AT command: ","at_cmd)");    
        byte[] buffer = (at_cmd + "\r").getBytes();
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length,     inet_addr, PORT);
            try {
                socket.send(packet);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        Log.e("///send at command","at command topic");
        return null;
    } 
}

}

 01-24 10:57:26.449: E/Trace(1356): error opening trace file: No such file or directory (2)
 01-24 10:57:26.948: E////(1356): end of on create
 01-24 10:57:27.109: D/gralloc_goldfish(1356): Emulator without GPU emulation detected.
 01-24 10:57:27.189: I/ActivityManager(160): Displayed  com.example.dronedivideandroid/.DroneMain: +1s232ms
 01-24 10:57:36.419: E////button(1356): forward_enter1
 01-24 10:57:36.419: E/AT command:(1356): at_cmd) 
 01-24 10:57:36.429: W/dalvikvm(1356): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
 01-24 10:57:36.439: E/AndroidRuntime(1356): FATAL EXCEPTION: AsyncTask #1
 01-24 10:57:36.439: E/AndroidRuntime(1356): java.lang.RuntimeException: An error occured while executing doInBackground()
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.lang.Thread.run(Thread.java:856)
 01-24 10:57:36.439: E/AndroidRuntime(1356): Caused by: java.lang.NullPointerException
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at com.example.dronedivideandroid.DroneMain$ATcommandThread.doInBackground(DroneMain.java:201)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at com.example.dronedivideandroid.DroneMain$ATcommandThread.doInBackground(DroneMain.java:1)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 01-24 10:57:36.439: E/AndroidRuntime(1356):    ... 5 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:57:00+00:00Added an answer on June 17, 2026 at 4:57 pm

    Yes, well you need to actually allocate the socket, the exception is a null pointer because you declared the socket but did not allocate it before using it. Now, this should work, but you will be constantly allocating a new socket. You should take the line: socket = new DatagramSocket() that I inserted below and put that in the onCreate most likely.

    protected Boolean doInBackground(String... arg0) {
            byte [] ip_bytes = new byte[] {(byte)192,(byte)168,(byte)1,(byte)1};
            try {
                inet_addr = InetAddress.getByAddress(ip_bytes );
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.e("AT command: ","at_cmd)");    
            byte[] buffer = (at_cmd + "\r").getBytes();
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length,     inet_addr, PORT);
                try {
                    socket= new DatagramSocket();
                    socket.send(packet);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            Log.e("///send at command","at command topic");
            return null;
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using AsyncTask and want to use getApplication() to work with class Application.
I have a backgorund thread that extends AsyncTask and which I use in activity
Is it at all possible, from within an AsyncTask that is NOT an inner
All I want to do is use AsyncTask to do a http request. This
I want to lock back button while my data process. I use AsyncTask to
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I want use a single php file to handle all of my voting requests.
i want use some data from a website with web service. i have a
I want use JQuery mobile for the front-end of my mobile application, but I
I use an AsyncTask for loading operations that I implemented as an inner class.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.