Android Open Source - GlassToPhoneBluetooth Connection Command






From Project

Back to project page GlassToPhoneBluetooth.

License

The source code is released under:

MIT License

If you think the Android project GlassToPhoneBluetooth listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ktlab.lib.connection;
// www  .ja v a 2s .  com
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ConnectionCommand {

    // Header (type + optionLen) length
    public static final int HEADER_LENGTH = Integer.SIZE / Byte.SIZE + 1;

    // Command fields
    public byte type;
    public int optionLen;
    public byte[] option;

    /**
     * Constructor Create BTCommand without option.
     *
     * @param type
     *            Commands type
     */
    public ConnectionCommand(byte type) {
        this(type, null);
    }

    /**
     * Constructor Create BTCommand with option.
     *
     * @param type
     *            Commands type
     * @param option
     *            Commands option
     */
    public ConnectionCommand(byte type, byte[] option) {
        this.type = type;
        if (option != null) {
            optionLen = option.length;
            this.option = new byte[option.length];
            System.arraycopy(option, 0, this.option, 0, option.length);
        } else {
            optionLen = 0;
            this.option = new byte[0];
        }
    }

    /**
     * Convert BTCommand to byte array
     *
     * @param command
     *            target command
     * @param order
     *            byte order
     * @return byte array
     * @hide
     */
    protected static byte[] toByteArray(ConnectionCommand command, ByteOrder order) {
        byte[] ret = new byte[command.optionLen + HEADER_LENGTH];
        // set byte order
        ByteBuffer bf = ByteBuffer.wrap(ret).order(order);
        bf.put(command.type);
        bf.putInt(command.optionLen);
        bf.put(command.option);

        return ret;
    }

    /**
     * Convert byte array to BTCommand
     *
     * @param data
     *            byte array
     * @param order
     *            Byte order
     * @return BTCommand
     * @hide
     */
    protected static ConnectionCommand fromByteArray(byte[] data, ByteOrder order) {
        ByteBuffer bf = ByteBuffer.wrap(data).order(order);
        byte type = bf.get();
        int len = bf.getInt();
        byte[] option = new byte[len];
        bf.get(option);

        return new ConnectionCommand(type, option);
    }

    /**
     * create BTCommand from Header and Option
     *
     * @param header
     *            header(byte array)
     * @param option
     *            option(byte array)
     * @return BTCommand
     * @hide
     */
    protected static ConnectionCommand fromHeaderAndOption(byte[] header, byte[] option,
            ByteOrder order) {
        byte[] data = new byte[header.length + option.length];

        System.arraycopy(header, 0, data, 0, header.length);
        System.arraycopy(option, 0, data, header.length, option.length);

        return fromByteArray(data, order);
    }
}




Java Source Code List

com.space150.android.glass.camerazoom.DeviceSelectActivity.java
com.space150.android.glass.camerazoom.MainActivity.java
com.zlglassworks.glassbluetoothtest.DeviceSelectDialogFragment.java
com.zlglassworks.glassbluetoothtest.MainActivity.java
ktlab.lib.connection.CommandReceiveThread.java
ktlab.lib.connection.CommandReceiveThread.java
ktlab.lib.connection.CommandSendThread.java
ktlab.lib.connection.CommandSendThread.java
ktlab.lib.connection.ConnectionCallback.java
ktlab.lib.connection.ConnectionCallback.java
ktlab.lib.connection.ConnectionCommand.java
ktlab.lib.connection.ConnectionCommand.java
ktlab.lib.connection.ConnectionThread.java
ktlab.lib.connection.ConnectionThread.java
ktlab.lib.connection.Connection.java
ktlab.lib.connection.Connection.java
ktlab.lib.connection.bluetooth.BluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.BluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.BluetoothConnection.java
ktlab.lib.connection.bluetooth.BluetoothConnection.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnection.java
ktlab.lib.connection.bluetooth.ClientBluetoothConnection.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnectionThread.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnection.java
ktlab.lib.connection.bluetooth.ServerBluetoothConnection.java