LIFX Developer Zone

Unable to read GetPower in Java-based Android app using LAN Protocol

Hi,

This is likely an issue with my implementation as I’ve tried sending the same packet using Packet Sender with success on my desktop and the phone I’m testing on. I’m also new to this kind of network programming stuff.

I’m trying to read whether the bulb is on and off so that I can update an icon in the app and know whether to send the turn off or turn on packet to toggle the bulb’s power.

However, whenever I send the packet to the bulb from the app (using the code below), I don’t appear to receive the last 4 characters of hex. I do, however, receive all of the characters leading up to them.

private void updateState() {
    final byte[] msg = hexStringToBytes("24 00 00 34 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 03 00 00 00 00 00 00 00 00 74 00 00 00");

    new Thread(new Runnable() {
        public void run() {
            try {
                InetAddress bulbAddress = InetAddress.getByAddress(ipAddr);
                if (!socket.getBroadcast()) socket.setBroadcast(true);
                DatagramPacket packet = new DatagramPacket(msg, msg.length, bulbAddress, 56700);
                socket.send(packet);
                DatagramPacket packet1 = new DatagramPacket(msg, msg.length, bulbAddress, 56700);
                socket.receive(packet1);

               TextView textView = (TextView) findViewById(R.id.state);
               String out = new String(packet1.getData(), packet1.getOffset(), packet1.getLength());

               textView.setText(toHex(out));
           } catch (Exception ex) {
              ex.printStackTrace();
           }
        }
    }).start();
}

private static byte[] hexStringToBytes(String input) {
    input = input.toLowerCase(Locale.US);
    int n = input.length() / 2;
    byte[] output = new byte[n];
    int l = 0;
    for (int k = 0; k < n; k++) {
        char c = input.charAt(l++);
        byte b = (byte) ((c >= 'a' ? (c - 'a' + 10) : (c - '0')) << 4);
        c = input.charAt(l++);
        b |= (byte) (c >= 'a' ? (c - 'a' + 10) : (c - '0'));
        output[k] = b;
    }
    return output;
}

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes()));
}

Using a similar method minus the second Datagram Packet and call to socket.Receive, I have been able to turn the bulb on and off without any issues. So it appears I’m able to write, but not read all of the data I need.

I’m unfamiliar with Java but judging from your code, you’re sending 36 bytes and then trying to read 36 bytes.

A GetLightPower is 36 bytes (which you seem to be creating correctly), but the response of a StatePower is 38 bytes, so your packet1 variable needs to have length 38 bytes.

1 Like

Hi @delfick,

That’s a good point. However, if I change it to be a hard-coded 38, I no longer seem to get a response. Here’s what I’m now using:

DatagramPacket packet1 = new DatagramPacket(msg, 38, bulbAddress, 56700);

Maybe the first variable given to new DatagramPacket needs to be a new byte array that’s 38 bytes long? rather than the existing 36 byte long one that has the GetLightPower packet in it?

1 Like

Okay,

It’s looking like that worked. I knew it had to be something silly that I overlooked.

Thank you for your help :slight_smile:

Yay! that’s awesome :slight_smile:

1 Like