Implementing the idle state

To implement the idle state:

  1. Create the IdleState.java class file.

  2. Import the required classes.

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.obex.Operation;
    import javax.obex.ResponseCodes;
  3. Set IdleState to extend ExchangerState.

    public class IdleState extends ExchangerState {
  4. Create the IdleState class constructor.

        public IdleState(ExchangerStateParent _parent) {
            super(_parent);
    
        }
  5. Create a method for sending the user's business card when the user initiates a card exchange.

        public int onGet(Operation op) {
            try {
                OutputStream out = op.openOutputStream();
                byte[] vCard = parent.getListener().getOwnBC(); // getting the
                // own card
    
                int vlen = vCard.length;
                byte[] tmpBuf = new byte[vlen + 4];
                System.arraycopy(vCard, 0, tmpBuf, 4, vlen);
                tmpBuf[0] = (byte) ((vlen >>> 24) & 0xff);
                tmpBuf[1] = (byte) ((vlen >>> 16) & 0xff);
                tmpBuf[2] = (byte) ((vlen >>> 8) & 0xff);
                tmpBuf[3] = (byte) ((vlen >>> 0) & 0xff);
    
                out.write(tmpBuf); // sending data
    
                op.close();
    
                return ResponseCodes.OBEX_HTTP_OK;
            } catch (Exception e) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
        }
  6. Create a method for receiving a business card from an incoming Bluetooth connection.

        public int onPut(Operation op) {
            try {
                InputStream in = op.openInputStream();
                byte[] fullResult = null;
                {
                    byte[] buf = new byte[256];
                    ByteArrayOutputStream bout = new ByteArrayOutputStream(2048);
                    for (int len = in.read(buf); len >= 0; len = in.read(buf)) {
                        bout.write(buf, 0, len);
                    }
                    fullResult = bout.toByteArray();
                }
                ByteArrayInputStream bin = new ByteArrayInputStream(fullResult);
                DataInputStream din = new DataInputStream(bin);
                int size = din.readInt();
                byte[] vCard = new byte[size];
                din.read(vCard);
                // card is received
    
                op.close();
                parent.getListener().onReceiveBC(vCard);
    
                return ResponseCodes.OBEX_HTTP_OK;
            } catch (Exception e) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
        }
  7. Create methods for handling sending.

        public void startSending(int oper) throws Exception {
            parent.setState(new InquiryState(parent, oper));
        }
    
        public void cancelSending() {
            // Internal error, but not fatal
        }
    }

Now that you have implemented the idle state, implement the inquiry state.