I'm new in Android programming and there is this project I'm working on to be able to turn on my computer using my phone. I have written my codes where by when an item in a list-view is clicked, it will fetch mac address and use it to turn on computer. The problem is that, the packet is never sent, I'm using Emulator. I have tried to search solutions in Google but couldn't find enough break through. Really need your help.
Here are my codes:
package black.cheetah.blackcheetah;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class ComputerTurnOnList extends Activity {
public static final int PORT = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_computer_turn_on_list);
if (android.os.Build.VERSION.SDK_INT > 8) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll()
.build();
StrictMode.setThreadPolicy(policy);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void buttonClick(View view) {
String broadcastIP = "192-168-43-171";
String mac = "3C-97-0E-82-3D-BD";
Log.d("Read mac= ", mac);
Log.d("Read ip=", broadcastIP);
ComputerTurnOnList.wakeup(broadcastIP, mac);
}
private static byte[] getMacBytes(String mac) throws IllegalArgumentException {
Log.d("GetMacBytes", "method started");
byte[] bytes = new byte[6];
String[] hex = mac.split("(\\:|\\-)");
if (hex.length != 6) {
throw new IllegalArgumentException("Invalid MAC address.");
}
try {
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) Integer.parseInt(hex[i], 16);
Log.d("GetMacbytes", "calculated");
Log.d("GetMacBytes (bytes)", new String(bytes));
}
}
catch (NumberFormatException e) {
Log.e("GetMacBytes","error");
}
return bytes;
}
public static void wakeup(String broadcastIP, String mac) {
Log.d("wakeup", "method started");
if (mac == null) {
Log.d("Mac error at wakeup", "mac = null");
return;
}
try {
byte[] macBytes = getMacBytes(mac);
Log.d("wakeup (bytes)", new String(macBytes));
byte[] bytes = new byte[6 + 16 * macBytes.length];
for (int i = 0; i < 6; i++) {
bytes[i] = (byte) 0xff;
}
for (int i = 6; i < bytes.length; i += macBytes.length) {
System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
}
Log.d("wakeup", "calculating completed, sending...");
InetAddress address = InetAddress.getByName(broadcastIP);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length,address,9);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
Toast.makeText(null, "Time up! Couldn't find Computer", Toast.LENGTH_SHORT ).show();
Log.d("wakeup", "Magic Packet sent");
}
catch (Exception e) {
Log.e("wakeup", "error" + e.getMessage());
}
}
}
When I run it here come an error:
05-30 06:26:15.607: D/Read mac=(1755): XX-XX-XX-XX-XX-XX
05-30 06:26:15.617: D/Read ip=(1755): xxx-xxx-xx-xxx
05-30 06:26:15.617: D/wakeup(1755): method started
05-30 06:26:15.617: D/GetMacBytes(1755): method started
05-30 06:26:15.617: D/GetMacbytes(1755): calculated
05-30 06:26:15.617: D/GetMacBytes (bytes)(1755): <����������
05-30 06:26:15.617: D/GetMacbytes(1755): calculated
05-30 06:26:15.657: D/GetMacBytes (bytes)(1755): <���������
05-30 06:26:15.657: D/GetMacbytes(1755): calculated
05-30 06:26:15.657: D/GetMacBytes (bytes)(1755): <�������
05-30 06:26:15.667: D/GetMacbytes(1755): calculated
05-30 06:26:15.667: D/GetMacBytes (bytes)(1755): <������
05-30 06:26:15.667: D/GetMacbytes(1755): calculated
05-30 06:26:15.667: D/GetMacBytes (bytes)(1755): <��=��
05-30 06:26:15.667: D/GetMacbytes(1755): calculated
05-30 06:26:15.667: D/GetMacBytes (bytes)(1755): <��=�
05-30 06:26:15.667: D/wakeup (bytes)(1755): <��=�
05-30 06:26:15.667: D/wakeup(1755): calculating completed, sending...
05-30 06:26:15.667: E/wakeup(1755): errorUnable to resolve host "xxx-xxx-xx-xxx": No address associated with hostname