コンピューターを起動するアプリを作成しようとしています。解決策はあると思いますが、何らかの理由で機能しません。
以下のスクリプトは 1 つのクラスです。これは、メイン アクティビティから mac とブロードキャスト IP で呼び出されます。
Wake.wakeup(broadcastIP, mac);
public class Wake {
BroadcastIP と mac は文字列になります。
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, 9);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
}
catch (Exception e) {
}
}
Mac からの変換は良いはずです。これは、ウェイクアップが意図されているときに呼び出されます。
private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
byte[] bytes = new byte[6];
if (macStr.length() != 12)
{
throw new IllegalArgumentException("Invalid MAC address...");
}
try {
String hex;
for (int i = 0; i < 6; i++) {
hex = macStr.substring(i*2, i*2+2);
bytes[i] = (byte) Integer.parseInt(hex, 16);
}
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid hex digit...");
}
return bytes;
}
}
あなたが私に与えることができるすべての助け/ヒントに感謝します.