如何在Android上制作Wake On Lan? [英] How to make Wake On Lan for Android?

查看:95
本文介绍了如何在Android上制作Wake On Lan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能告诉我,如何制作Android版Wake On Lan应用程序吗?我在Google上搜索了两个星期,尝试了所有内容,从lan应用程序的另一端下载了源代码,并尝试查找用于制作和发送魔术包的代码.看起来所有其他代码都可以使用,但是当我在应用程序中使用它时,它将无法使用!而且我还尝试编辑Java代码以使其适用于Android(此代码: http://www.jibble.org/wake-on-lan/WakeOnLan.java ).你能帮我吗?对不起,我的英语不好.这是我的代码:

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

 }

 }

这是Logcat的输出(我用X代替了Mac):

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)

推荐答案

您似乎在使用错误的方法从 edittext(toString)中获取文本,因此必须使用 gettext .

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

这篇关于如何在Android上制作Wake On Lan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆