Hello, I want to send udp packets by wifi. I use below code.
But I get error in LogCat: error message in sending null
BroadcastThread.java:
public class BroadcastThread extends Thread{
final static private String TAG = "Chat";
final static boolean D = true;
final static int PORT = 6666;
boolean okbroadcast=false;
Handler mHandler;
Context mContext;
DatagramSocket serverSocket;
static public boolean socketOK=true;
InetAddress myBcastIPAddress;
InetAddress myIPAddress;
public BroadcastThread(Context currentContext, Handler handler)
{
mContext = currentContext;
mHandler = handler;
try{
serverSocket = new DatagramSocket(PORT);
serverSocket.setBroadcast(true);
Log.i(TAG,"server sicket is created.");
} catch(Exception e){
Log.e(TAG,"Cannot open socket! "+e.getMessage());
socketOK = false;
return;
}
try{
getMyWiFiBcastAndIPAddress();
Log.i(TAG,"My IP address:"+myIPAddress);
Log.i(TAG,"My Broadcast IP address:"+myBcastIPAddress);
}catch(Exception e){
Log.e(TAG,"Cannot get my own Broadcast IP address");
}
}
public void closeSocket(){
serverSocket.close();
}
boolean socketIsOK(){
return socketOK;
}
public void run(){
if (okbroadcast==true)
mHandler.obtainMessage(1,okbroadcast).sendToTarget();
if(!socketOK)
closeSocket();
}
public void sendMessage(String msg) throws IOException {
Log.i(TAG,"string in thread= "+msg);
byte[] sendData = new byte[1024];
sendData = msg.getBytes();
Log.i(TAG,"to byte[] in thread= "+sendData);
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, myBcastIPAddress, PORT);
Log.i(TAG,"byte[] length in thread= "+sendData.length);
if(sendData==null)
Log.i(TAG,"sendData is null! "+sendData);
if(serverSocket==null)
Log.i(TAG,"serversocket is null! "+serverSocket);
serverSocket.send(sendPacket);
Log.i(TAG,"after sending........ ");
okbroadcast=true;
Log.i(TAG,"Sent packet: "+msg);
closeSocket();
}
private void getMyWiFiBcastAndIPAddress() throws UnknownHostException{
WifiManager mWifi = (WifiManager) (mContext.getSystemService(Context.WIFI_SERVICE));
WifiInfo info = mWifi.getConnectionInfo();
if(info==null){
if(D) Log.e(TAG,"Cannot Get WiFi Info");
return;
}
else{
if(D) Log.d(TAG,"\n\nWiFi Status: " + info.toString());
}
DhcpInfo dhcp = mWifi.getDhcpInfo();
if (dhcp == null) {
Log.d(TAG, "Could not get dhcp info");
return;
}
int myIntegerIPAddress = dhcp.ipAddress;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((myIntegerIPAddress>> k * 8) & 0xFF);
myIPAddress = InetAddress.getByAddress(quads);
int myIntBroadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((myIntBroadcast >> k * 8) & 0xFF);
myBcastIPAddress=InetAddress.getByAddress(quads);
}
}
and in activity Directory.java:
public class Directory extends Activity {
Utility ut=new Utility();
BroadcastThread broadThread;
ArrayAdapter <String>receivedMessages;
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String incomingMessage = (String) msg.obj;
Toast.makeText(getApplicationContext(), "in hendler: "+incomingMessage, Toast.LENGTH_LONG).show();
Log.i("vote", "in hendler: "+incomingMessage);
if(!incomingMessage.matches(""))
{
BroadcastThread.socketOK=false;
}
}
};
btn_create_session.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
broadThread = new BroadcastThread(getApplicationContext(),mHandler);
if (!broadThread.socketIsOK()){
Log.e("vote","Server NOT STARTED");
Toast.makeText(getApplicationContext(), "Cannot Start Server ", Toast.LENGTH_LONG).show();
return;
}
broadThread.start();
Log.i("vote","Server Started");
try{
broadThread.sendMessage("namekey");
}catch(Exception e){
Toast.makeText(Directory.this, "Cannot send message"+e.getMessage(), Toast.LENGTH_LONG).show();
Log.e("vote","error message in sending: "+e.getMessage());
}
finally{
broadThread.closeSocket();
}
}
});