kader990 / android-xbmcremote

Automatically exported from code.google.com/p/android-xbmcremote
1 stars 0 forks source link

Implement WOL properly #239

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Feature Suggestion:
When pressing the power on, WoL; Instead of wait 40 sec and then try to
connect once. Try to connect every 3 sec or so, under 1-2 min time. And get
rid of the 40 sec setting. One setting less, yey.

Original issue reported on code.google.com by jenny1.l...@gmail.com on 29 Apr 2010 at 7:01

GoogleCodeExporter commented 8 years ago
oops. wrong login. Thats me.

Original comment by bolin.andreas@gmail.com on 29 Apr 2010 at 7:08

GoogleCodeExporter commented 8 years ago
Greetings to Jenny then. ;)

the_alien & mikkle, any idea?

Original comment by phree...@gmail.com on 5 May 2010 at 6:55

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Issue 308 has been merged into this issue.

Original comment by phree...@gmail.com on 15 Aug 2011 at 1:25

GoogleCodeExporter commented 8 years ago
Issue 424 has been merged into this issue.

Original comment by phree...@gmail.com on 15 Aug 2011 at 1:25

GoogleCodeExporter commented 8 years ago
Issue 513 has been merged into this issue.

Original comment by phree...@gmail.com on 15 Aug 2011 at 1:25

GoogleCodeExporter commented 8 years ago
Issue 399 has been merged into this issue.

Original comment by phree...@gmail.com on 15 Aug 2011 at 1:26

GoogleCodeExporter commented 8 years ago
Issue 542 has been merged into this issue.

Original comment by phree...@gmail.com on 15 Aug 2011 at 1:26

GoogleCodeExporter commented 8 years ago
Please check out all the issues merged into this one.

Original comment by phree...@gmail.com on 15 Aug 2011 at 1:26

GoogleCodeExporter commented 8 years ago
I'll re-invent the WoL code from scratch-ish to suss out all these defects.

Original comment by mukkenb...@gmail.com on 8 Oct 2011 at 9:52

GoogleCodeExporter commented 8 years ago
Sorry didnt see that other issues was merged into this... My small contribution:

Same issue here, as a new and happy xbmc-remote user, only function not working 
for me was the power on button :-(
I implemented this fastfix, and recompiled the source, everything is OK now. 
Please consider broadcast as a feature in upcoming releases.

my modified version of WakoOnLan.java:
----
/*
 *      Copyright (C) 2005-2009 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC Remote; see the file license.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

package org.xbmc.android.util;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.StringTokenizer;

import android.widget.Toast;

/**
 * The WoL implementation
 * 
 * @author Team XBMC
 */
public class WakeOnLan {
    public Boolean sendMagicPacket(String macStr, String ipStr, int PORT) {              
        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(getBroadcastIP());
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
            DatagramSocket socket = new DatagramSocket();
            socket.send(packet);
            socket.close();
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    public Boolean sendMagicPacket(String macStr, int PORT) {
        return sendMagicPacket(macStr, "255.255.255.255", PORT);
    }

    public Boolean sendMagicPacket(String macStr) {
        return sendMagicPacket(macStr, "255.255.255.255", 9);
    }

    public String getLocalIpAddress() { 
        try { 
            for (Enumeration<NetworkInterface> en = NetworkInterface 
                    .getNetworkInterfaces(); en.hasMoreElements();) { 
                NetworkInterface intf = en.nextElement(); 
                for (Enumeration<InetAddress> enumIpAddr = intf 
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) { 
                    InetAddress inetAddress = enumIpAddr.nextElement(); 
                    if (!inetAddress.isLoopbackAddress()) { 
                        return inetAddress.getHostAddress().toString(); 
                    } 
                } 
            } 
        } catch (SocketException ex) {} 
        return null; 
    } 

    public String getBroadcastIP(){ 
        String myIP = getLocalIpAddress(); 
        StringTokenizer tokens = new StringTokenizer(myIP, "."); 
        int count = 0; 
        String broadcast = ""; 
        while (count < 3) { 
            broadcast += tokens.nextToken() + "."; 
            count++; 
        } 
        return broadcast+"255"; 
    } 
  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;
    }
 }

Original comment by r.o.niel...@gmail.com on 13 Mar 2012 at 8:20