FileDocCategorySizeDatePackage
Messenger.javaAPI DocphoneME MR2 API (J2ME)8353Wed May 02 18:00:32 BST 2007com.sun.midp.jsr82emul

Messenger

public final class Messenger extends Object
Represents JSR 82 emulation protocol, namely messages and codes recognized by emulation client and/or server. It is not a part of JSR 82 implementation and is only used within JSR 82 emulation mode. The emulation mode allows running tets without real native Bluetooth libraries or hardware. Emulation server and client communicate thru a socket by means of sending data packets of the following format:
1 byte 1 byte as defined by the previous field
packet type code length of information in bytes information bytes

Fields Summary
public static final String
ENCODING
Keeps encoding for messages.
public static final byte
ERROR
Responce code that identifies a failure.
public static final byte
REGISTER_DEVICE
Device registration request code.
public static final byte
REGISTERED
Successfull registration response.
public static final byte
DONE
Notification that client does not require server any more.
public static final byte
SPECIFIC_MESSAGE
Code for specific messages that are only recognized by specific handlers at server side.
public static final byte
REGISTER_SERVICE
Code for starting advertising service in the ether.
public static final byte
UNREGISTER_SERVICE
Code for unregistering service.
public static final byte
CONNECT_TO_SERVICE
Request for service connection.
public static final byte
SERVICE_AT
Respond that provides service connection details.
public static final byte
START_INQUIRY
Request for inquiry start.
public static final byte
INQUIRY_COMPLETED
Respond on inquiry completion.
public static final byte
UPDATE_DEVICE_STATE
Command to update device state that includes discoverable mode and device class.
private byte
code
Keeps code value retrieved by last receive() invocation.
private byte[]
bytes
Keeps bytes retrieved by last receive() invocation.
Constructors Summary
public Messenger()
Constructs an instance. Always use different instances for different clients/servers to make sure data stored in message and code is appropriate.

    
                                
      
    
Methods Summary
public byte[]getBytes()
Provides bytes read by the last receive() invocation.

return
bytes received last time.

        return bytes;
    
public bytegetCode()
Provides code read by the last receive() invocation.

return
the code received last time, -1 if there is no valid value.

        return code;
    
public intgetInt()
Retrieves integer represented by bytes read by the last receive() invocation. Throws IllegalArgument exception if those bytes do not represent an integer.

return
integer represented by bytes received last time.
exception
EmulationException if no valid value read

        if (bytes == null || bytes.length != 4) {
            throw new EmulationException();
        }
        
        int res = 0;
        for (int i = 3; i >= 0; i--) {
            res = (res << 8) | (bytes[i] & 0xff); 
        }
        
        return res;
    
public java.lang.StringgetMessage()
Provides message read by the last receive() invocation.

return
string message received last time.
exception
EmulationException if no valid value read

        try {
            return new String(bytes, 0, bytes.length, ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new EmulationException();
        }
    
public voidreceive(java.io.InputStream in)
Receives a packet from input stream given saving retrieved data in code and message.

param
in the input stream to read from.
exception
IOException if one is issued by in methods.

        code = -1;
        bytes = null;
        
        if (in == null) {
            throw new IllegalArgumentException();
        }
        
        synchronized (in) {
            code = (byte) in.read();
            int length = in.read();
            
            if (length == -1) {
                throw new IOException();
            }
            
            bytes = new byte[length];
            
            if (length != in.read(bytes)) {
                throw new IOException();
            }
        }
    
public voidsend(java.io.OutputStream out, byte code, java.lang.String message)
Forms a packet with given code and message and sends it to given output stream.

param
out the output stream to send to.
param
code the code to send.
param
message the message to send.
exception
IOException if one is issued by out methods.

        if (out == null) {
            throw new IllegalArgumentException();
        }
        
        if (message == null) {
            message = "";
        }
        
        byte[] messageBytes = message.getBytes(ENCODING);
        
        sendBytes(out, code, messageBytes);
    
public voidsendBytes(java.io.OutputStream out, byte code, byte[] info)
Forms a packet with given code and bytes to send and sends it to given output stream.

param
out the output stream to send to
param
code the code to send
param
info byte array to be sent entirely
exception
IOException if one is issued by out methods.

        
        if (info.length > Byte.MAX_VALUE) {
            throw new IllegalArgumentException();
        }
        
        synchronized (out) {
            out.write(code);
            out.write((byte)info.length);
            out.write(info);
            out.flush();
        }
    
public voidsendInt(java.io.OutputStream out, byte code, int value)
Forms a packet with given code and bytes that represent given integer, then sends it to given output stream.

param
out the output stream to send to
param
code the code to send
param
value integer to send
exception
IOException if one is issued by out methods.

            
        byte[] intBytes = new byte[4];
        for (int i = 0; i < 4; i++) {
            intBytes[i] = (byte) (value & 0xff);
            value >>= 8;
        }
        
        sendBytes(out, code, intBytes);