FileDocCategorySizeDatePackage
GenericBluetoothStack.javaAPI DocphoneME MR2 API (J2ME)5100Wed May 02 18:00:30 BST 2007com.sun.midp.jsr082.bluetooth

GenericBluetoothStack

public class GenericBluetoothStack extends BluetoothStack
BluetoothStack implementation which relies on HCI command/event flow.

Fields Summary
private final int
MAX_HCI_PACKET_SIZE
Maximum HCI packet size as defined by Bluetooth 1.1 specification.
private final int
HCI_INQUIRY_COMPLETE
HCI Inquiry Complete event code.
private final int
HCI_INQUIRY_RESULT
HCI Inquiry Result event code.
private final int
HCI_AUTH_COMPLETE
HCI Authentication Complete event code.
private final int
HCI_NAME_COMPLETE
HCI Remote Name Request Complete event code.
private final int
HCI_ENCRYPT_CHANGE
HCI Encrypt Change event code.
private byte[]
buffer
Internal byte array for storing incoming HCI events.
Constructors Summary
Methods Summary
protected java.lang.StringextractAddress(int offset)
Extracts Bluetooth address from the internal buffer.

param
offset offset in the internal buffer
return
Bluetooth address consisting of 12 hexadecimal characters


                               
        
        byte[] addr = new byte[BluetoothUtils.BTADDR_SIZE];
        System.arraycopy(buffer, offset, addr, 0, BluetoothUtils.BTADDR_SIZE);
        return BluetoothUtils.getAddressString(addr);
    
protected intextractShort(int offset)
Extracts 2 octets from the internal buffer.

param
offset offset in the internal buffer
return
16 bits of data from the given offset

        return ((int)buffer[offset] & 0xff) |
                (((int)buffer[offset + 1] & 0xff) << 8);
    
protected java.lang.StringextractString(int offset)
Extracts string (e.g. friendly name) from the internal buffer. The string is stored in UTF-8 format and is terminated by NULL character.

param
offset offset in the internal buffer
return
Java string retrieved from binary event data

        int length = 0;
        while (offset + length < MAX_HCI_PACKET_SIZE &&
               buffer[offset + length] != 0) {
            length++;
        }
        // IMPL_NOTE: UTF-8 encoding support for Java strings?
//      return new String(buffer, offset, length, "UTF-8");
        return stringUTF8(buffer, offset, length);
    
protected BluetoothEventretrieveEvent()
Retrieves Bluetooth event from HCI event data containing in the internal buffer.

return
BluetoothEvent subclass instance

        readData(buffer);
        int code = buffer[0];
        int len = buffer[1];
        switch (code) {
            case HCI_INQUIRY_COMPLETE:
                return new InquiryCompleteEvent(buffer[2] == 0);
            case HCI_INQUIRY_RESULT:
                int num = buffer[2];
                int offset = 3;
                InquiryResult[] results = new InquiryResult[num];
                for (int i = 0; i < num; i++) {
                    String addr = extractAddress(offset);
                    int cod = 0;
                    for (int j = 0; j < 3; j++) {
                        cod |= ((int)buffer[offset + 9 + j] & 0xff) <<
                            (16 - j * 8);
                    }
                    results[i] = new InquiryResult(addr, cod);
                    offset += 14; // 14 is the size of the response structure
                }
                return new InquiryResultEvent(results);
            case HCI_AUTH_COMPLETE:
                return new AuthenticationCompleteEvent(extractShort(3),
                        buffer[2] == 0);
            case HCI_NAME_COMPLETE:
                return new NameResultEvent(extractAddress(3), extractString(9));
            case HCI_ENCRYPT_CHANGE:
                return new EncryptionChangeEvent(extractShort(3),
                        buffer[2] == 0, buffer[5] == 1);
            default:
                return null;
        }