Class BlueRS
- java.lang.Object
-
- org.deepjava.runtime.ppc32.Task
-
- org.deepjava.runtime.mpc555.driver.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 likeconnect(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 likedisconnect()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 methodgetResult()can be called to check, if the command has been sent successfully. If the result code isRESULT_UNDEFINED, the command has not got the result from the Bluetooth module yet.Starting the Driver
Thestart()method must been called first to start theBlueRSdriver!- Author:
- schlaepfer
-
-
Field Summary
Fields Modifier and Type Field Description static intMODE_ATMode code forgetMode().
The module can receive AT commands in this mode.static intMODE_CONFIGMode code forgetMode().
The module can receive configuration commands in this mode.static intMODE_CONNECTEDMode code forgetMode().
If a connection between this module and another device is established, the module is in this mode.static intMODE_CONNECTED_ATMode code forgetMode().
The module is still connected to another device, but can receive AT-commands.static intRECEIVE_MTUThe maximum number of bytes which can be sent in one packet.static intRECEIVE_TASK_PERIODThe receiver task period.static intRESULT_ERRORResult code forgetResult().static intRESULT_OKResult code forgetResult().static intRESULT_UNDEFINEDResult code forgetResult().-
Fields inherited from class org.deepjava.runtime.ppc32.Task
done, firstErr, maxNofTasks, nofActivations, period, safe, time
-
Fields inherited from interface org.deepjava.runtime.ppc32.Ippc32
CTR, DAR, DEC, DSISR, FPR0, FPR1, FPR10, FPR11, FPR12, FPR13, FPR14, FPR15, FPR16, FPR17, FPR18, FPR19, FPR2, FPR20, FPR21, FPR22, FPR23, FPR24, FPR25, FPR26, FPR27, FPR28, FPR29, FPR3, FPR30, FPR31, FPR4, FPR5, FPR6, FPR7, FPR8, FPR9, FPSCR, LR, MSR, R0, R1, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, R2, R20, R21, R22, R23, R24, R25, R26, R27, R28, R29, R3, R30, R31, R4, R5, R6, R7, R8, R9, SPR1, SPR18, SPR19, SPR22, SPR26, SPR268, SPR269, SPR27, SPR272, SPR273, SPR274, SPR275, SPR287, SPR8, SPR9, SPRG0, SPRG1, SPRG2, SPRG3, SRR0, SRR1, TBLread, TBUread, XER
-
-
Constructor Summary
Constructors Constructor Description BlueRS()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaction()Receive Task
Do not call this method!static intavailableToReceive()Checks the number of bytes which are available to receive.
If the device is not inMODE_CONNECTED, the result will be -1.static voidconnect(byte[] connectTo)Connects to a device.
Seeconnect(String).static voidconnect(String connectTo)Connects to a device.
To use this command, the device must be inMODE_AT.static voidconnectionAccept()Accepts a initiated connection.
UseisConnectionInitiated()to check if a connection has been initiated by a remote device.static voiddisconnect()Disconnects the connection.
To use this command, the device must be inMODE_CONNECTED_AT.static voidenableErrorMessages()Enables error messages from the Bluetooth module.
In addition to the AT commands, the Bluetooth module will display error codes after each command.static intgetMode()Returns the current mode.
The values areMODE_AT, MODE_CONNECTED, MODE_CONNECTED_ATandMODE_CONFIGstatic intgetResult()Returns the result of the last operation.
The values areRESULT_UNDEFINED, RESULT_OK or RESULT_ERROR.static voidinquiry(boolean getServices)Starts an inquiry.
To use this command, the device must be inMODE_AT.static booleanisConnectionInitiated()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.static byteread()static intread(byte[] b)static voidreset()Sends a reset command to the module.
To use this command, the device must be inMODE_AT.static voidreturnFromATMode()Returns from AT-mode to connection-mode.
To use this command, the device must be inMODE_CONNECTED_AT.static voidreturnFromConfigMode()static voidsendCommand(byte[] cmd)Sends a command to the Bluetooth module.
SeesendCommand(String).static voidsendCommand(byte[] cmd, int off, int len)Sends a command to the Bluetooth module.
Writes, if possible,lenbytes starting atoffsetto this output stream.static voidsendCommand(String cmd)Sends a command to the Bluetooth module.
To use this command, the device must be inMODE_AT.static voidstart()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.static voidstop()Stops the BlueRS driver.
If the driver is not used in a time critical application, there is no need to call this method.static voidswitchToConfigMode()static voidswitchToConnectedATMode()Switches from connection-mode to AT-modeMODE_CONNECTED_AT.
To use this command, the device must be inMODE_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 beMODE_CONNECTED_ATand notMODE_AT.static booleanwrite(byte[] b, int len)
-
-
-
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
-
RESULT_UNDEFINED
public static final int RESULT_UNDEFINED
Result code forgetResult().- See Also:
- Constant Field Values
-
RESULT_OK
public static final int RESULT_OK
Result code forgetResult().- See Also:
- Constant Field Values
-
RESULT_ERROR
public static final int RESULT_ERROR
Result code forgetResult().- See Also:
- Constant Field Values
-
MODE_AT
public static final int MODE_AT
Mode code forgetMode().
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 forgetMode().
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 forgetMode().
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 forgetMode().
The module is still connected to another device, but can receive AT-commands.- See Also:
- Constant Field Values
-
-
Method Detail
-
sendCommand
public static void sendCommand(String cmd)
Sends a command to the Bluetooth module.
To use this command, the device must be inMODE_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,lenbytes starting atoffsetto this output stream.- Parameters:
cmd- the byte arrayoff- 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.
SeesendCommand(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 ifconnectionAccept()is called.- Returns:
- true if a connection has been initiated
-
connectionAccept
public static void connectionAccept()
Accepts a initiated connection.
UseisConnectionInitiated()to check if a connection has been initiated by a remote device.
-
inquiry
public static void inquiry(boolean getServices)
- Parameters:
getServices-trueif 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 inMODE_AT.
If the device is connectedgetMode()returnsMODE_CONNECTED. The result code will beRESULT_OK. If the target device does not respond, the result code will beRESULT_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.
Seeconnect(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 inMODE_CONNECTED_AT.
-
switchToConnectedATMode
public static void switchToConnectedATMode()
Switches from connection-mode to AT-modeMODE_CONNECTED_AT.
To use this command, the device must be inMODE_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 beMODE_CONNECTED_ATand notMODE_AT. Make shure the device is in that mode before using any other commands (e.g.disconnect().
-
switchToConfigMode
public static void switchToConfigMode()
-
returnFromATMode
public static void returnFromATMode()
Returns from AT-mode to connection-mode.
To use this command, the device must be inMODE_CONNECTED_AT.
-
returnFromConfigMode
public static void returnFromConfigMode()
-
reset
public static void reset()
Sends a reset command to the module.
To use this command, the device must be inMODE_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 inMODE_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)- Parameters:
b- data to write to the devicelen- 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)
- 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()
- 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 areRESULT_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 ofgetResult()will returnRESULT_OKorRESULT_ERROR.- Returns:
- the result of the last operation.
-
getMode
public static int getMode()
Returns the current mode.
The values areMODE_AT, MODE_CONNECTED, MODE_CONNECTED_ATandMODE_CONFIG- Returns:
- the current mode.
-
action
public void action()
Receive Task
Do not call this method!- Specified by:
actionin interfaceActionable- Overrides:
actionin classTask
-
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. Afterstop(),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 setdbgtrue.
-
-