> 13.1.3 | ATCommandListener Interface

> The ATCommandListener interface implements callback functions for:

> • | URCs

> • | Changes of the serial interface signals RING, DCD and DSR

> • | Opening and closing of data connections

> The user must create an implementation class for ATCommandListener to receive AT events. The ATEvent method of this class must contain the processing code for the different AT-Events (URCs). The RINGChanged, DCDChanged, DSRChanged and CONNChanged methods should contain the processing code for possible state changes. This code shall leave the listener context as soon as possible (i.e. running a new thread). While the callback method does not return it cannot be called again if changes occur.
> The CONNChanged method indicates the start and the end of data connections. During a data connection it is possible to transfer data with the I/O stream methods (see Section 13.1.1.3). Some data services (i.e. FTP) transfer the data so quickly, that the CONNChanged start and close events are received even parallel to the response of the AT command which originated the data connection. The user’s application must realize, that the data connection had taken place and read the data with the I/O stream read methods afterwards.

> 13.1.3.1 | ATEvents

> An ATEvent or a URC is a report message sent from the module to the application. An unsolicited result code is either delivered automatically when an event occurs or as a result of a query the module previously received. However, a URC is not issued as a direct response to an executed AT command. Some URCs must be activated with an AT command.
> Typical URCs may be information about incoming calls, a received SM, temperature changes, the status of the battery, etc. A summary of URCs is listed in [1].

> 13.1.3.2 | Implementation

> class ATListenerA implements ATCommandListener { public void ATEvent(String Event) { if (Event.indexOf("+CALA: Reminder 1") >= 0) { /* take desired action after receiving the reminder */ } else if (Event.indexOf("+CALA: Reminder 2") >= 0) { /* take desired action after receiving the reminder */ } else if (Event.indexOf("+CALA: Reminder 3") >= 0) { /* take desired action after receiving the reminder */ } /* No action taken for these events */ public void RINGChanged(boolean SignalState) {} public void DCDChanged(boolean SignalState) {} public void DSRChanged(boolean SignalState) {} } class ATListenerB implements ATCommandListener { public void ATEvent(String Event) { if (Event.indexOf("+SCKS: 0") >= 0) { System.out.println("SIM Card is not inserted."); /* perform other actions */ } else if (Event.indexOf("+SCKS: 1") >= 0) { System.out.println("SIM Card is inserted."); /* perform other actions */ } } public void RINGChanged(boolean SignalState) { /* take some action when the RING signal changes if you want to */ } public void DCDChanged(boolean SignalState) { /* take some action when the DCD signal changes if you want to */ } public void DSRChanged(boolean SignalState {} /* take some action when the DSR signal changes if you want to */ } public void CONNChanged(boolean SignalState {} /* take some action when the state of a connection changes if you want to */ } }

> 13.1.3.3 | Registering a Listener with an ATCommand Instance

> After creating an instance of the ATCommandListener class, it must be passed as a parameter to the ATCommand.addListener() method. The callback methods of the instance will be called by the runtime system each time the corresponding events (URCs or signal state changes) occur on the corresponding device AT parser.

> /* we have two ATCommands instances, atc1 and atc2 */ ATListenerA reminder_listener = new ATListenerA(); ATListenerB card_listener = new ATListenerB(); atc1.addListener(reminder_listener); atc2.addListener(card_listener);

> The ATCommand.removeListener() method removes a listener object that has been previously added to the internal list table of listener objects. After it has been removed from the list it will not be called when URCs occur. If it was not previously registered the list remains unchanged.
> The same ATCommandListener may be added to several ATCommand instances and several ATCommandListeners may be added to the same ATCommand.