Sending Wake on LAN packet from Android to PC

前端 未结 3 1505
小蘑菇
小蘑菇 2020-12-24 04:11

My Android app sends/retrieves data to/from the user\'s own PC using HTTP and it\'s working fine with a handful of beta testers. I now need to consider a situation where the

3条回答
  •  攒了一身酷
    2020-12-24 04:37

    Here is some C# code that I have used in the past. It should be relatively easy to convert into java and send using a DatagramPacket

    namespace WakeOnLan
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                byte[] mac = new byte[] { mac address goes here i.e 0x00, and so on };
                WakeUp(mac);
            }
    
            private static void WakeUp(byte[] mac)
            {
                //
                // WOL packet is sent over UDP 255.255.255.0:40000.
                //
                Console.WriteLine("Waking Up.......");
                UdpClient client = new UdpClient();
                client.Connect(IPAddress.Broadcast, 40000);
    
                //
                // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
                //
                byte[] packet = new byte[17 * 6];
    
                //
                // Trailer of 6 times 0xFF.
                //
                for (int i = 0; i < 6; i++)
                    packet[i] = 0xFF;
    
                //
                // Body of magic packet contains 16 times the MAC address.
                //
                for (int i = 1; i <= 16; i++)
                    for (int j = 0; j < 6; j++)
                        packet[i * 6 + j] = mac[j];
    
                //
                // Submit WOL packet.
                //
                client.Send(packet, packet.Length);
                Console.WriteLine("Machine Woke Up....");
            }
        }
    }
    

    Hope this helps

提交回复
热议问题