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)
有帮助吗?

解决方案

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

其他提示

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.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top