Class BlueRS

  • All Implemented Interfaces:
    Ippc32, Actionable

    public class BlueRS
    extends Task
    Driver for the Stollmann BlueRS+I Bluetooth Module.
    The module supports one emulated serial connection using the Bluetooth Serial Port Profile (SPP).
    The device is connected to the second serial port of the mpc555. To test and configure the module, the Windows Hyperterminal can be used at a baud rate of 9600 kbps, no flow control. Stollmann provides the complete documentation at www.stollmann.de.

    Connection Modes

    There are 3 different modes in which the Bluetooth module can operate.
    First there's the connection mode (MODE_CONNECTED). In this mode, data is sent (write(byte[], int)) and received (read(byte[])).
    Secondly there's the AT command mode (MODE_AT). In this mode, the device can receive AT commands like connect(String) which are mainly used for connection handling. After power up, the module is in this mode. When in connection mode, the device can temporarily switch to AT mode and back again (MODE_CONNECTED_AT). The connection remains open in the background. To send commands like disconnect() to the module, the AT mode has to be entered. In normal connection mode, the device would not recognize the command and send it to its connection partner as normal text.
    The last mode is for configuring the device. This mode is not used in normal operation and should only be used by experienced users.

    Checking for Results

    After sending a command to the Bluetooth module (e.g. connect(String)), the method getResult() can be called to check, if the command has been sent successfully. If the result code is RESULT_UNDEFINED, the command has not got the result from the Bluetooth module yet.

    Starting the Driver

    The start() method must been called first to start the BlueRS driver!
    Author:
    schlaepfer
    • Field Detail

      • RECEIVE_TASK_PERIOD

        public static final int RECEIVE_TASK_PERIOD
        The receiver task period.
        See Also:
        Constant Field Values
      • RECEIVE_MTU

        public static final int RECEIVE_MTU
        The maximum number of bytes which can be sent in one packet.
        See Also:
        Constant Field Values
      • MODE_AT

        public static final int MODE_AT
        Mode code for getMode().
        The module can receive AT commands in this mode. After power-up or reset the module is in this mode.
        See Also:
        Constant Field Values
      • MODE_CONFIG

        public static final int MODE_CONFIG
        Mode code for getMode().
        The module can receive configuration commands in this mode. In normal operation, this mode is not used.
        See Also:
        Constant Field Values
      • MODE_CONNECTED

        public static final int MODE_CONNECTED
        Mode code for getMode().
        If a connection between this module and another device is established, the module is in this mode.
        See Also:
        Constant Field Values
      • MODE_CONNECTED_AT

        public static final int MODE_CONNECTED_AT
        Mode code for getMode().
        The module is still connected to another device, but can receive AT-commands.
        See Also:
        Constant Field Values
    • Constructor Detail

      • BlueRS

        public BlueRS()
    • Method Detail

      • sendCommand

        public static void sendCommand​(String cmd)
        Sends a command to the Bluetooth module.
        To use this command, the device must be in MODE_AT.
        After using this method, getResult() can be used to get the result of this operation.
        Parameters:
        cmd - the command to send
      • sendCommand

        public static void sendCommand​(byte[] cmd,
                                       int off,
                                       int len)
        Sends a command to the Bluetooth module.
        Writes, if possible, len bytes starting at offset to this output stream.
        Parameters:
        cmd - the byte array
        off - the start offset in the data.
        len - the number of bytes to write
      • sendCommand

        public static void sendCommand​(byte[] cmd)
        Sends a command to the Bluetooth module.
        See sendCommand(String).
        Parameters:
        cmd - the command to send
      • isConnectionInitiated

        public static boolean isConnectionInitiated()
        Returns true if a connection has been initiated by a remote device.
        When a remote device wants to connect to this module, the module sends a "RING" command. Depending on the configuration of the module, the connection is accepted immediately or only if connectionAccept() is called.
        Returns:
        true if a connection has been initiated
      • connectionAccept

        public static void connectionAccept()
        Accepts a initiated connection.
        Use isConnectionInitiated() to check if a connection has been initiated by a remote device.
      • inquiry

        public static void inquiry​(boolean getServices)
        Starts an inquiry.
        To use this command, the device must be in MODE_AT.
        Parameters:
        getServices - true if a list of the services of the found devices should be retrieved, else false
      • connect

        public static void connect​(String connectTo)
        Connects to a device.
        To use this command, the device must be in MODE_AT.
        If the device is connected getMode() returns MODE_CONNECTED. The result code will be RESULT_OK. If the target device does not respond, the result code will be RESULT_ERROR.
        Parameters:
        connectTo - This string represents a Bluetooth address (e.g. "0002ee36b272") or a previously inquired device (e.g. "d1").
      • connect

        public static void connect​(byte[] connectTo)
        Connects to a device.
        See connect(String).
        Parameters:
        connectTo - This byte array represents a Bluetooth address (e.g. "0002ee36b272") or a previously inquired device (e.g. "d1").
      • disconnect

        public static void disconnect()
        Disconnects the connection.
        To use this command, the device must be in MODE_CONNECTED_AT.
      • switchToConnectedATMode

        public static void switchToConnectedATMode()
        Switches from connection-mode to AT-mode MODE_CONNECTED_AT.
        To use this command, the device must be in MODE_CONNECTED.
        It is required to wait 1 second before using this command (no data transfer).
        Because the device is still connected but in AT-mode, the resulting mode will be MODE_CONNECTED_AT and not MODE_AT. Make shure the device is in that mode before using any other commands (e.g. disconnect().
      • switchToConfigMode

        public static void switchToConfigMode()
        Switches from MODE_AT to MODE_CONFIG.
        To use this command, the device must be in MODE_AT.
      • returnFromATMode

        public static void returnFromATMode()
        Returns from AT-mode to connection-mode.
        To use this command, the device must be in MODE_CONNECTED_AT.
      • returnFromConfigMode

        public static void returnFromConfigMode()
        Returns from MODE_CONFIG to MODE_AT.
        To use this command, the device must be in MODE_CONFIG.
      • reset

        public static void reset()
        Sends a reset command to the module.
        To use this command, the device must be in MODE_AT. Configuration mode may be entered after using this command.
      • availableToReceive

        public static int availableToReceive()
        Checks the number of bytes which are available to receive.
        If the device is not in MODE_CONNECTED, the result will be -1.
        Returns:
        number of bytes which are available to receive or -1 if not in MODE_CONNECTED.
      • write

        public static boolean write​(byte[] b,
                                    int len)
        Writes data to the device.
        This command must only be used in MODE_CONNECTED (see getMode()).
        Parameters:
        b - data to write to the device
        len - length of the data to write to the device
        Returns:
        true if the data has been sent, else false
      • read

        public static int read​(byte[] b)
        Reads data from the device.
        This command must only be used in MODE_CONNECTED (see getMode()).
        Parameters:
        b - the byte buffer to which the data will be written
        Returns:
        number of bytes read, -1 if the device is not connected or 0 if no data is available.
        If the number of bytes available will exceed the buffer size, only the buffer size is returned.
      • read

        public static byte read()
        Reads one byte from the device.
        This command must only be used in MODE_CONNECTED (see getMode()).
        Returns:
        one byte from the device, -1 if the device is not connected or 0 if no data is available.
        If the number of bytes available will exceed the buffer size, only the buffer size is returned.
      • getResult

        public static int getResult()
        Returns the result of the last operation.
        The values are RESULT_UNDEFINED, RESULT_OK or RESULT_ERROR. After calling a method which writes a command to the module (e.g. connect(String), the result will be RESULT_UNDEFINED. As soon as the module returns its status, the return value of getResult() will return RESULT_OK or RESULT_ERROR.
        Returns:
        the result of the last operation.
      • getMode

        public static int getMode()
        Returns the current mode.
        The values are MODE_AT, MODE_CONNECTED, MODE_CONNECTED_AT and MODE_CONFIG
        Returns:
        the current mode.
      • action

        public void action()
        Receive Task
        Do not call this method!
        Specified by:
        action in interface Actionable
        Overrides:
        action in class Task
      • start

        public static void start()
        Starts the BlueRS driver.
        This method must been called first to start the background task which is needed to receive and process characters from the Bluetooth device.
      • stop

        public static void stop()
        Stops the BlueRS driver.
        If the driver is not used in a time critical application, there is no need to call this method. It will remove the background task to receive and process characters from the Bluetooth device. After stop(), start() must be called to reactivate the driver.
      • enableErrorMessages

        public static void enableErrorMessages()
        Enables error messages from the Bluetooth module.
        In addition to the AT commands, the Bluetooth module will display error codes after each command. A detailed description can be found in the Stollmann BlueRS+E/I manual (section 5.2). To display this error codes on the Target Log set dbg true.