3

can you please tell me, how to make Wake On Lan app for Android? I am searching two weeks on Google, tried everything, downloaded source code from another wake on lan app and tried to find code for making and sending magic packet. It looks like every other code works, but when I use it in my app, it doesn't work! And I also tried to edit java code to make it work for Android (this: http://www.jibble.org/wake-on-lan/WakeOnLan.java ). Can you help me, please? And sorry for my bad English. This is my code:

package com.macura.wakemypc;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import com.macura.wakemypc.MainActivity;

import com.macura.wakemypc.R;
import com.macura.wakemypc.MainActivity;

import com.macura.wakemypc.MainActivity;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
public static final int PORT = 9; 





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 }

 @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) {
EditText iptext = (EditText)findViewById(R.id.ipbox);
EditText mactext = (EditText)findViewById(R.id.macbox);
String broadcastIP = String.valueOf(iptext.getText());
String mac = String.valueOf(mactext.getText());
Log.d("Read mac= ", mac);
Log.d("Read ip=", broadcastIP);
MainActivity.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];
try {
    String hex;
    for (int i = 0; i < 6; i++) {
        hex = mac.substring(i*2, i*2+2);
        bytes[i] = (byte) Integer.parseInt(hex, 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();
        Log.d("wakeup", "Magic Packet sent");



         }
         catch (Exception e) {
             Log.e("wakeup", "error");
     }

 }

 }

This is the Logcat output(I put the X instead of mac):

07-07 14:58:36.282: D/Read mac=(7247): XX:XX:XX:XX:XX:XX
07-07 14:58:36.282: D/Read ip=(7247): 192.168.1.255
07-07 14:58:36.282: D/wakeup(7247): method started
07-07 14:58:36.282: D/GetMacBytes(7247): method started
07-07 14:58:36.282: D/GetMacbytes(7247): calculated
07-07 14:58:36.282: D/GetMacBytes (bytes)(7247): ������������
07-07 14:58:36.282: E/GetMacBytes(7247): error
07-07 14:58:36.282: W/System.err(7247): java.lang.NumberFormatException: Invalid int:       ":1"
07-07 14:58:36.282: W/System.err(7247):     at         java.lang.Integer.invalidInt(Integer.java:138)
07-07 14:58:36.282: W/System.err(7247):     at     java.lang.Integer.parse(Integer.java:375)
07-07 14:58:36.282: W/System.err(7247):     at     java.lang.Integer.parseInt(Integer.java:366)
07-07 14:58:36.282: W/System.err(7247):     at   com.macura.wakemypc.MainActivity.getMacBytes(MainActivity.java:57)
07-07 14:58:36.282: W/System.err(7247):     at   com.macura.wakemypc.MainActivity.wakeup(MainActivity.java:77)
07-07 14:58:36.282: W/System.err(7247):     at    com.macura.wakemypc.MainActivity.buttonClick(MainActivity.java:47)
07-07 14:58:36.282: W/System.err(7247):     at    java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.282: W/System.err(7247):     at  java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.282: W/System.err(7247):     at    android.view.View$1.onClick(View.java:3586)
07-07 14:58:36.292: W/System.err(7247):     at   android.view.View.performClick(View.java:4084)
07-07 14:58:36.292: W/System.err(7247):     at   android.view.View$PerformClick.run(View.java:16966)
07-07 14:58:36.292: W/System.err(7247):     at   android.os.Handler.handleCallback(Handler.java:615)
07-07 14:58:36.292: W/System.err(7247):     at   android.os.Handler.dispatchMessage(Handler.java:92)
07-07 14:58:36.292: W/System.err(7247):     at android.os.Looper.loop(Looper.java:137)
07-07 14:58:36.292: W/System.err(7247):     at     android.app.ActivityThread.main(ActivityThread.java:4931)
07-07 14:58:36.292: W/System.err(7247):     at  java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.292: W/System.err(7247):     at   java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.292: W/System.err(7247):     at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-07 14:58:36.292: W/System.err(7247):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-07 14:58:36.292: W/System.err(7247):     at dalvik.system.NativeStart.main(Native     Method)
07-07 14:58:36.292: D/wakeup (bytes)(7247): ������������
07-07 14:58:36.292: D/wakeup(7247): calculating completed, sending...
07-07 14:58:36.292: E/wakeup(7247): error
07-07 14:58:36.292: W/System.err(7247): android.os.NetworkOnMainThreadException
07-07 14:58:36.292: W/System.err(7247):     at    android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-07 14:58:36.302: W/System.err(7247):     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
07-07 14:58:36.302: W/System.err(7247):     at libcore.io.IoBridge.sendto(IoBridge.java:473)
07-07 14:58:36.302: W/System.err(7247):     at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
07-07 14:58:36.302: W/System.err(7247):     at java.net.DatagramSocket.send(DatagramSocket.java:284)
07-07 14:58:36.302: W/System.err(7247):     at com.macura.wakemypc.MainActivity.wakeup(MainActivity.java:91)
07-07 14:58:36.302: W/System.err(7247):     at    com.macura.wakemypc.MainActivity.buttonClick(MainActivity.java:47)
07-07 14:58:36.302: W/System.err(7247):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.302: W/System.err(7247):     at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.302: W/System.err(7247):     at android.view.View$1.onClick(View.java:3586)
07-07 14:58:36.302: W/System.err(7247):     at android.view.View.performClick(View.java:4084)
07-07 14:58:36.302: W/System.err(7247):     at android.view.View$PerformClick.run(View.java:16966)
07-07 14:58:36.302: W/System.err(7247):     at android.os.Handler.handleCallback(Handler.java:615)
07-07 14:58:36.302: W/System.err(7247):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 14:58:36.302: W/System.err(7247):     at android.os.Looper.loop(Looper.java:137)
07-07 14:58:36.312: W/System.err(7247):     at   android.app.ActivityThread.main(ActivityThread.java:4931)
07-07 14:58:36.312: W/System.err(7247):     at  java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:58:36.312: W/System.err(7247):     at  java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:58:36.312: W/System.err(7247):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-07 14:58:36.312: W/System.err(7247):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-07 14:58:36.312: W/System.err(7247):     at dalvik.system.NativeStart.main(Native   Method)
StfgMccx
  • 98
  • 2
  • 9

3 Answers3

0

Without any more details about your app, what you might want to try instead is GCM. You set up to receive GCM messages, then send the GCM message and that will take care of waking up the device, and is probably equally sufficient for any purpose you're trying to achieve.

user1676075
  • 3,056
  • 1
  • 19
  • 26
  • I found something about GCM on android developers, it looks like GCM is for waking Android devices, but I need to wake PC. – StfgMccx Jul 01 '13 at 08:38
0

It looks like you are using the wrong method to get the text from your edittext (toString), you have to use gettext.

ridoy
  • 6,274
  • 2
  • 29
  • 60
adnidor
  • 78
  • 9
  • ok, will this work?: String mac = mactext.getText().toString().trim(); – StfgMccx Jul 01 '13 at 12:38
  • Should this work? Because it doesn't work: String mac = mactext.getText().toString(); – StfgMccx Jul 01 '13 at 12:48
  • Use String mac = String.valueOf(mactext.getText()); – adnidor Jul 01 '13 at 13:02
  • I tried it (also updated the code above), but it still doesn't work :( I have wake on lan enabled on my pc, and apps from Google Play works. – StfgMccx Jul 01 '13 at 13:40
  • The code above is from this site: http://www.jibble.org/wake-on-lan/WakeOnLan.java , I just edited it to make it work for Android – StfgMccx Jul 01 '13 at 14:09
  • No it doesn't, everytime i click "send" button, nothing happens – StfgMccx Jul 01 '13 at 14:28
  • Oh, sorry, there is something like: 07-01 16:39:05.378: E/SpannableStringBuilder(14821): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length – StfgMccx Jul 01 '13 at 14:40
  • Ok, i fixed it (there was unused text fields with wrong input type), but it still does nothing when clicked – StfgMccx Jul 01 '13 at 15:06
  • Try to add logoutput to tell you what it reads from the fields – adnidor Jul 01 '13 at 15:22
  • Ok, I added this to buttonClick method: Log.d("Read mac= ", mac); Log.d("Read ip", broadcastIP); the output was the mac and ip I entered, so that is probably not the problem – StfgMccx Jul 01 '13 at 15:33
  • I think there's a problem with getMacBytes method, because I added Log.d output for bytes (String bytescheck = bytes.toString;) and in logcat there was something like [b@0c2b... i mean many numbers and letters, and Magic Packet probably doesn't looks like this – StfgMccx Jul 01 '13 at 16:06
  • You should'nt use toString to convert Bytes to String, use new String(bytearray); instead – adnidor Jul 01 '13 at 18:40
  • the output is this: 07-02 10:21:12.389: D/GetMacBytes (bytes)(24359): ������������ – StfgMccx Jul 02 '13 at 08:22
  • So, I added the logcat output, when i enter mac address with ":". But without them, theres just an error with sending magic packet – StfgMccx Jul 02 '13 at 12:58
  • Add e.printStackTrace to catch – adnidor Jul 02 '13 at 20:35
  • Ok, the logcat output is there, but I don't know what does that output mean – StfgMccx Jul 07 '13 at 13:05
0

On Android versions higher then 3. you can't do networking if you are in the main thread, in your case main thread is MainActivity! Just call the function inside of a thread:

new Thread(new Runnable() {
    public void run() {
        wakeup(broadcastIP, mac);
    }
}).start();

If you don't know what a thread is, read this about Threads

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
hipokito
  • 413
  • 4
  • 11