gpt4 book ai didi

java - Java 中的 Android 局域网唤醒

转载 作者:行者123 更新时间:2023-11-30 03:32:10 25 4
gpt4 key购买 nike

我正在尝试用 Java(适用于 Android)制作 Wake On Lan 应用程序,所以我搜索并找到了这段代码:

import java.io.*;
import java.net.*;

public class WakeOnLan {

public static final int PORT = 9;

public static void main(String[] args) {

if (args.length != 2) {
System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
System.exit(1);
}

String ipStr = args[0];
String macStr = args[1];

try {
byte[] macBytes = getMacBytes(macStr);
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);
}

InetAddress address = InetAddress.getByName(ipStr);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();

System.out.println("Wake-on-LAN packet sent.");
}
catch (Exception e) {
System.out.println("Failed to send Wake-on-LAN packet: + e");
System.exit(1);
}

}

private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
String[] hex = macStr.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);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit in MAC address.");
}
return bytes;
}


}

我在我的 PC 上试过了,它有效,但它不适用于 Android :( 我试图编辑它,但仍然没有 :( 这是我的 Android 代码:

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.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.editText1);
EditText mactext = (EditText)findViewById(R.id.editText2);
String mac = mactext.toString();
String broadcastIP = iptext.toString();
MainActivity.wakeup(broadcastIP, mac);
}
private static byte[] getMacBytes(String mac) throws IllegalArgumentException {
// TODO Auto-generated method stub
byte[] bytes = new byte[6];
if (mac.length() != 12)
{
throw new IllegalArgumentException("Invalid MAC address...");
}
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);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit...");
}
return bytes;
}

public static void wakeup(String broadcastIP, String mac) {
if (mac == null) {
return;
}

try {
byte[] macBytes = getMacBytes(mac);
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);
}

InetAddress address = InetAddress.getByName(broadcastIP);
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 5555);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();



}
catch (Exception e) {
}

}
}

每次我输入 ip 和 mac 地址时,它什么都不做。那么,你能帮帮我吗? :)是的,对不起我的英语 :D

最佳答案

我不确定其余代码,但您应该使用 String mac = mactext.getText().toString(); 而不是 String mac = mactext.toString( ); 因为如果不进行此更改,您将转换为整个 EditText 的字符串,而不仅仅是它的值。附言我知道问题已经过去 10 个月了,对于任何会来这里寻找 WOL android 代码的人来说,它应该是有用的

关于java - Java 中的 Android 局域网唤醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17273956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com