Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


Java
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) {
// Inflate the menu; this adds items to the action bar if it is present.
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");
// TODO Auto-generated method stub
     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): &lt;����������
05-30 06:26:15.617: D/GetMacbytes(1755): calculated
05-30 06:26:15.657: D/GetMacBytes (bytes)(1755): &lt;���������
05-30 06:26:15.657: D/GetMacbytes(1755): calculated
05-30 06:26:15.657: D/GetMacBytes (bytes)(1755): &lt;�������
05-30 06:26:15.667: D/GetMacbytes(1755): calculated
05-30 06:26:15.667: D/GetMacBytes (bytes)(1755): &lt;������
05-30 06:26:15.667: D/GetMacbytes(1755): calculated
05-30 06:26:15.667: D/GetMacBytes (bytes)(1755): &lt;��=��
05-30 06:26:15.667: D/GetMacbytes(1755): calculated
05-30 06:26:15.667: D/GetMacBytes (bytes)(1755): &lt;��=�
05-30 06:26:15.667: D/wakeup (bytes)(1755): &lt;��=�
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
Posted
Updated 31-May-15 2:17am
v2
Comments
Richard MacCutchan 31-May-15 8:21am    
Judging by your log messages your mac and IP addresses have not been set correctly. You also need to fix the messages that are trying to print from binary values.
Member 11385584 1-Jun-15 4:14am    
It would be nice if you show me how to set those ip and mac, because I tried to find other ways in Google but couldn't find any. And what's wrong with those messages?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900