Implementing the inquiry state

To implement the inquiry state:

  1. Create the InquiryState.java class file.

  2. Import the required classes.

    import java.io.IOException;
    import java.util.Vector;
    
    import javax.bluetooth.DeviceClass;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.DiscoveryListener;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.RemoteDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.obex.Operation;
    import javax.obex.ResponseCodes;
    
  3. Set InquiryState to extend ExchangerState and implement DiscoveryListener . MIDlets use the DiscoveryListener interface to receive device and service discovery events.

    public class InquiryState extends ExchangerState
            implements DiscoveryListener {
  4. Create the required variables and the InquiryState class constructor.

    Use the startInquiry method to start an inquiry for devices. Devices that respond to the inquiry are returned to the MIDlet through the deviceDiscovered method of the DiscoveryListener interface. Call the cancelInquiry method to stop the inquiry.

        private DiscoveryAgent agent;
        private Vector remoteDevices; // vector of found devices
        private int operation = ServiceDiscoveryState.GET;
    
        /**
         * Constructor
         *
         * @param _parent -
         *          the class which is nesting the current state of the
         *          state machine
         */
        public InquiryState(ExchangerStateParent _parent, int oper)
                throws IOException {
            super(_parent);
            operation = oper;
            remoteDevices = new Vector();
            // initiate Bluetooth
            LocalDevice local = LocalDevice.getLocalDevice();
            agent = local.getDiscoveryAgent();
            // start Bluetooth inquiry
            agent.startInquiry(DiscoveryAgent.GIAC, this);
        }
  5. Create methods for handling sending.

        /*
         * Inquiry state does not allow to start any other business card
         * exchange process.
         *
         * @see bcexchanger.comm.ExchangerState#startSending()
         */
        public void startSending(int op) throws IOException {
            throw new IOException(
                    "Inquiry is in progress. Inquiry has to be cancelled before starting new sending process");
        }
    
        /*
         * InquiryState allows to cancel inquiry process.
         *
         * @see bcexchanger.comm.ExchangerState#cancelSending()
         */
        public void cancelSending() {
            agent.cancelInquiry(this);
        }
  6. The MIDlet calls the deviceDiscovered method when a device is found during an inquiry, and the inquiryCompleted method when the inquiry is completed. After that, the MIDlet converts the inquiry completion codes into application completion codes. For more information on inquiry completion, see the DiscoveryListener reference.

        public void deviceDiscovered(RemoteDevice dev, DeviceClass devClass) {
            remoteDevices.addElement(dev);
        }
    
        public void inquiryCompleted(int code) {
            try {
                int completionCode = ExchangerComm.ERROR;
    
                // convert the inquiry completion code to application completion
                // code
                switch (code) {
                    case DiscoveryListener.INQUIRY_COMPLETED:
                        completionCode = ExchangerComm.DONE;
                        break;
                    case DiscoveryListener.INQUIRY_TERMINATED:
                        completionCode = ExchangerComm.CANCELED;
                        break;
                    case DiscoveryListener.INQUIRY_ERROR:
                        completionCode = ExchangerComm.ERROR;
                        break;
                }
                parent.getListener().onInquiryComplete(completionCode); // signal
                // that
                // inquiry
                // is
                // done
    
                if (code == DiscoveryListener.INQUIRY_COMPLETED) { // no errors
                    // or
                    // cancellations
                    parent.setState(new ServiceDiscoveryState(parent,
                            remoteDevices, operation));
    
                } else {
                    parent.setState(new IdleState(parent));
                }
    
            } catch (Exception e) {
                parent.setState(new IdleState(parent));
            }
        }
    
        /*
         * Service discovery callbacks are not handled and not supposed to
         * occur, since service discovery process is not started
         *
         * @see javax.bluetooth.DiscoveryListener#servicesDiscovered(int,
         *      javax.bluetooth.ServiceRecord[])
         */
        public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
            throw new RuntimeException(
                    "Internal error #4: InquiryState.servicesDiscovered() should not be called");
        }
    
        /*
         * Service discovery callbacks are not handled and not supposed to
         * occur, since service discovery process is not started
         *
         * @see javax.bluetooth.DiscoveryListener#serviceSearchCompleted(int,
         *      int)
         */
        public void serviceSearchCompleted(int arg0, int arg1) {
            throw new RuntimeException(
                    "Internal error #5: InquiryState.serviceSearchCompleted() should not be called");
        }
  7. Create empty methods for sending and receiving business cards.

        /*
         * Serving OBEX GET operation is supported only in IdleState
         *
         * @see bcexchanger.comm.ExchangerState#onGet(javax.obex.Operation)
         */
        public int onGet(Operation op) {
    
            return ResponseCodes.OBEX_HTTP_CONFLICT;
        }
    
        /*
         * Serving OBEX GET operation is supported only in
         * IdleState
         *
         * @see bcexchanger.comm.ExchangerState#onPut(javax.obex.Operation)
         */
        public int onPut(Operation op) {
            // onPut is supported only in IdleState
            return ResponseCodes.OBEX_HTTP_CONFLICT;
        }
    }

Now that you have implemented the inquiry state, implement the service discovery state.