> 13.1.1 | Class ATCommand

> The ATCommand class supports the execution of AT commands in much the same way as they would be executed over a serial interface. It provides a simple way to send strings directly to the device’s AT parsers.

> 13.1.1.1 | Instantiation with or without CSD Support

> There can be only exactly as many ATCommand instances as there are parsers on the device. If there are no more parsers available, the ATCommand constructor will throw ATCommand FailedException. All AT parser instances support CSD. However from a Java application point of view it may make sense to have one dedicated instance for CSD call handling. Therefore, and also for historical reasons, only one parser with CSD support may be requested through the constructor. If more then one parser with CSD support is requested, the constructor will throw ATCommandFailedException.

> try { ATCommand atc = new ATCommand(false); /* An instance of ATCommand is created. CSD is not explicitly * requested. */ } catch (ATCommandFailedException e) { System.out.println(e); }

> The csdSupported() method returns the CSD capability of the connected instance of the device's AT parser. The method checks as well, if the current mode of the module supports CSD. Please notice that this check has not been done when opening the ATCommand instance.

> boolean csd_support = atc.csdSupported();

> release() releases the resources held by the instance of the ATCommand class. After calling this function the class instance cannot be used any more but the resources are free to be used by a new instance

> 13.1.1.2 | Sending an AT Command to the Device, the send() Method

> An AT command is sent to the device by using the send() method. The AT command is sent as a string which must include the finalizing line feed "\r" or the corresponding line end character.

> String response = atc.send(“at+cpin?\r”); /* method returns when the module returns a response */ System.out.println(response); #Possible response printed to System.out: +CPIN: READY OK

> This send function is a blocking call, which means that the calling thread will be interrupted until the module returns a response. The function returns the response, the result code of the AT command, as a string.
> Occasionally it may be infeasible to wait for an AT command that requires some time to be processed, such as AT+COPS. There is a second, non-blocking, send function which takes a second parameter in addition to the AT command. This second parameter is a callback instance, ATCommandResponseListener. Any response to the AT command is delivered to the callback instance when it becomes available. The method itself returns immediately. The ATComman dResponseListener and the non-blocking send method are described in Section 13.1.2.
> Note: Using the send methods with strings with incorrect AT command syntax will cause errors.

> 13.1.1.3 | Data Connections

> If a data connection is created with the ATCommand class, for instance with ‘atd’, an input stream is opened to receive the data from the connection. Similarly, an output stream can be opened to send data on the connection.

> /* Please note that this example would not work unless the module had * been initialized and logged into a network. */ System.out.println("Dialing: ATD" + CALLED_NO); response = atc.send("ATD" + CALLED_NO + "\r"); System.out.println("received: " + response); if (response.indexOf("CONNECT") >= 0) { try { // We have a data connection, now we do some streaming... // IOException will be thrown if any of the Stream methods fail OutputStream dataOut = ATCmd.getDataOutputStream(); InputStream dataIn = ATCmd.getDataInputStream(); // out streaming... dataOut.write(new String("\n\rHello world\n\r").getBytes()); dataOut.write(new String("\n\rThis data was sent by a Java " + "MIDlet!\n\r").getBytes()); dataOut.write(new String("Press 'Q' to close the " + "connection\n\r").getBytes()); // ...and in streaming System.out.println("Waiting for incoming data, currently " + dataIn.available() + " bytes in buffer."); rcv = 0; while(((char)rcv != 'q') && ((char)rcv != 'Q') && (rcv != -1)){ rcv = dataIn.read(); if (rcv >= 0) { System.out.print((char)rcv); } } /* The example continues after the next block of text */

> These streams behave slightly differently than regular data streams. The streams are not closed by using the close() method. A stream remains open until the release() method is called. A module can be switched from the data mode to the AT command mode by calling the break Connection() method.

> /* continue example */ if (rcv != -1) { // Now break the data connection System.out.println("\n\n\rBreaking connection"); try { strRcv = ATCmd.breakConnection(); } catch(Exception e) { System.out.println(e); } System.out.println("received: " + strRcv); } else { // Received EOF, somebody else broke the connection System.out.println("\n\n\rSomebody else switched to " + "command mode!"); } System.out.println("Hanging up"); strRcv = ATCmd.send("ATH\r"); System.out.println("received: " + strRcv); } catch(IOException e) { System.out.println(e); } } else { System.out.println("No data connection established,"); }

> An IOException is thrown if any function of the I/O streams are called when the module is in AT command mode, except for read functions. The read function case is explained in more detail in Section 13.1.3. The ATCommand class does not report the result codes returned after data connection release.
> Data Connections are not only used for data transfer over the air but also to access external hardware. Here is a list of at commands which open a data connection:

> • | atd, for data calls

> • | AT^SSPI, for access to I2C/SPI

> • | AT^SIS commands for Internet services

> For data connection signaling see also Section 13.1.3.

> 13.1.1.4 | Synchronization

> For performance reasons no synchronization is done in the ATCommand class. If an instance of this class has to be accessed from different threads ensure that the send() functions, the release() function, the cancelCommand() function and the breakConnection() function are synchronized in the user implementation.