Android Question help wake on lan

netklamath

Member
Licensed User
Longtime User
Is there any way I can do WOL?

How do I tell the socket to the mac sending the packet?
 

DKCERT

Member
Licensed User
Longtime User
You can make your own WOL code and have full control. This is a short description:

A Wake-On-LAN packet is an normal UDP packet with a header and the MAC address of the target computer. The UDP packet is 16 times the byte representation of the MAC address, plus the extra 6 bytes for a header. This makes the total packet size 6 + 16*6 = 102 bytes.
  • The first 6 bytes are filled with 0xFF (-> 255,255,255,255,255,255).
  • The next 6 bytes are the MAC address of the (e.g. 00:1A:2B:3C:4D:5E -> 0,26,43,60,77,94).
  • Each following set of 6 bytes are also filled with the target computer MAC address until the packet is full.
The UDP packet is normally sent to LAN broadcast address (e.g. 192.168.x.255). All computers on your LAN will recieve this, but only the one with a matching MAC address will respond and power on. The WOL UDP packet normally uses port 9. Note: As UDP packets are not guaranteed you may need to send it more than once.

' Target Computer MAC: 00:1A:2B:3C:4D:5E -> 0,26,43,60,77,94:
MagicWOLPacketData() As Byte = Array As Byte(255,255,255,255,255,255,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94,0,26,43,60,77,94)

I hope it is useful.
 
Last edited:
Upvote 0

netklamath

Member
Licensed User
Longtime User
You can make your own WOL code and have full control. This is a short description:

A Wake-On-LAN packet is an normal UDP packet with a header and the MAC address of the target computer. The UDP packet is 16 times the byte representation of the MAC address, plus the extra 6 bytes for a header. This makes the total packet size 6 + 16*6 = 102 bytes.
  • The first 6 bytes are filled with 0xFF (-> 255,255,255,255,255,255).
  • The next 6 bytes are the MAC address of the (e.g. 00:1A:2B:3C:4D:5E -> 0,26,43,60,77,94).
  • Each following set of 6 bytes are also filled with the target computer MAC address until the packet is full.
The UDP packet is normally sent to LAN broadcast address (e.g. 192.168.x.255). All computers on your LAN will recieve this, but only the one with a matching MAC address will respond and power on. The WOL UDP packet normally uses port 9. Note: As UDP packets are not guaranteed you may need to send it more than once.



I hope it is useful.

Thanks for your help. I tried but did not work. Would you be so kind as to get the exact code to copy and paste?
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
A few months ago I wrote this class for WOL.
you can use it as follows.
Add new class module named as Wakeonlan
Copie code in the class module.
Mark Network and Byteconverter in the Libs Tab.

Example:

Dim wol As Wakeonlan
wol.Initialize
wol.Wake (You macadress target) 'Example wol.Wake ("58:53: EF: 6F: 26:29")

Monki

B4X:
'Class module
Private Sub Class_Globals
    Private UDPSocket1 As UDPSocket
    Private bcon As ByteConverter
    Private Packet As UDPPacket
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
UDPSocket1.Initialize("",0, 8000)
End Sub


Public Sub Wake(Mac As String)
    Dim barry() As Byte
    Dim data(102) As Byte
    For t=0 To 5                                        '6x FF to Buffer
        data(t)=Bit.OR(data(t),255)
    Next
    Try
        barry=bcon.HexToBytes(Mac)                        'HEX to Byte
        For x =6 To 96 Step 6                            '16x Mac-Adresse to Buffer
            bcon.ArrayCopy (barry, 0, data,x,6)
        Next
        Packet.Initialize(data,"255.255.255.255",9)
        UDPSocket1.Send(Packet)                            'Magic Packet send
    Catch
        ToastMessageShow("Error "&LastException.Message,True)
    End Try
End Sub
 
Upvote 0

netklamath

Member
Licensed User
Longtime User
A few months ago I wrote this class for WOL.
you can use it as follows.
Add new class module named as Wakeonlan
Copie code in the class module.
Mark Network and Byteconverter in the Libs Tab.

Example:

Dim wol As Wakeonlan
wol.Initialize
wol.Wake (You macadress target) 'Example wol.Wake ("58:53: EF: 6F: 26:29")

Monki

B4X:
'Class module
Private Sub Class_Globals
    Private UDPSocket1 As UDPSocket
    Private bcon As ByteConverter
    Private Packet As UDPPacket
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
UDPSocket1.Initialize("",0, 8000)
End Sub


Public Sub Wake(Mac As String)
    Dim barry() As Byte
    Dim data(102) As Byte
    For t=0 To 5                                        '6x FF to Buffer
        data(t)=Bit.OR(data(t),255)
    Next
    Try
        barry=bcon.HexToBytes(Mac)                        'HEX to Byte
        For x =6 To 96 Step 6                            '16x Mac-Adresse to Buffer
            bcon.ArrayCopy (barry, 0, data,x,6)
        Next
        Packet.Initialize(data,"255.255.255.255",9)
        UDPSocket1.Send(Packet)                            'Magic Packet send
    Catch
        ToastMessageShow("Error "&LastException.Message,True)
    End Try
End Sub


hello again. Sorry. I did as you said and it does not work.

I get this error: java.lang.Exception: hex string has odd number of characters

Would you be so kind to send me an example?. I really appreciate it
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
Hello,
my mistake
The MAC address must be passed as a string without double points.

ex. Example wol.Wake ("5853EF6F2629")
 
Upvote 0
Top