我想用asynctask发送一个数据报包,但是不行!!
我已经添加了所有可能需要的权限!
我认为问题可能出在
socket.send(packet);
请帮我解决这个问题:)
"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