如何为Android制作Wake On Lan?

时间:2013-06-30 16:04:12

标签: java android eclipse wake-on-lan

请告诉我,如何为Android制作Wake On Lan应用程序?我在谷歌上搜索了两个星期,尝试了所有内容,从另一个唤醒lan应用程序下载了源代码,并试图找到制作和发送魔术包的代码。看起来每个其他代码都有效,但是当我在我的应用程序中使用它时,它不起作用!我还尝试编辑java代码以使其适用于Android(这个:http://www.jibble.org/wake-on-lan/WakeOnLan.java)。你能帮我吗? 抱歉我的英语不好。 这是我的代码:

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");
     }

 }

 }

这是Logcat输出(我把X代替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)

3 个答案:

答案 0 :(得分:0)

如果没有关于您的应用的更多详细信息,您可能想要尝试的是GCM。您设置为接收GCM消息,然后发送GCM消息并负责唤醒设备,并且可能同样足以满足您尝试实现的任何目的。

答案 1 :(得分:0)

您似乎使用错误的方法从edittext (toString)获取文字,您必须使用gettext

答案 2 :(得分:0)

在Android版本高于3.如果您在主线程中,则无法进行网络连接,在您的情况下,主线程是MainActivity!只需调用线程内的函数:

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

如果您不知道某个主题是什么,请阅读Threads