This documentation is for WSO2 IoT Server 3.2.0. View the documentation for the latest release.
Listening to Events Not Broadcasted by the Android OS - IoT Server 3.2.0 - WSO2 Documentation
Skip to end of metadata
Go to start of metadata

This section describes how WSO2 IoT Server is configured to check the status of the events that are not broadcasted by the OS. This is done by continuously polling the relevant APIs. 

Follow the steps given below to check the status of the events via the Android API when the alarm is triggered.

  1. Configure the startListening() method of the AlertEventListener interface, for the API to poll continuously, and check if any events have triggered the alarm.

    The startListening() method needs to be configured in the new class you created. This class is in the package.

    public void startListening() {
     RuntimeInfo runtimeInfo = new RuntimeInfo(EventRegistry.context, new String[] {
     Application application = runtimeInfo.getHighestCPU();
     if (application.getCpu() > CPU_THRESHOLD) {
      try {
       String appState = CommonUtils.toJSON(application);
       publishEvent(appState, Constants.EventListners.RUNTIME_STATE);
       if (Constants.DEBUG_MODE_ENABLED) {
        Log.d(TAG, appState);
      } catch (AndroidAgentException e) {
       Log.i(TAG, "Could not convert to JSON");
  2. When an event is received, the onReceive method that is in the package is notified. At that point you need to extract the data from the intent and populate the bean with the data.

    The Bean classes are located in the package.

  3. Convert the data populated by the bean to a JSON string using the CommonUtils.toJSON(beanInstance).

    ApplicationStatus applicationState = new ApplicationStatus();
    String appState = CommonUtils.toJSON(applicationState);
  4. Publish the data to WSO2 IoT Server by calling the publishEvent method. For this you need to configure the publish method so that it calls the HttpDataPublisher.

    public void publishEvent(String payload, String type) {
     EventPayload eventPayload = new EventPayload();
     HttpDataPublisher httpDataPublisher = new HttpDataPublisher();
  5. If you wish to periodically poll the APIs, you need to schedule the AlarmManager. This can be done via two options, depending on your requirement.

    For more information on the AlarmManager concept, see AlarmManager.

    • Option 1: Setting up the default alarm
      Use this option, if you wish to use the existing DEFAULT_START_TIME and DEFAULT_INTERVAL values. 

      1. In this method, you don't have to do any changes to the default EventRegistry.startAlarm method that is in the package.

        • DEFAULT_START_TIME: The time the alarm should first go off, in milliseconds. The default value is 1000.
        • DEFAULT_INTERVAL: The interval between subsequent repeats of the alarm, in milliseconds. The default value is 30000.
        • For more information on the default configuration, see file.
      2. Register the new listener by adding an or condition to the existing if condition.


      if (Constants.EventListners.RUNTIME_STATE_LISTENER || Constants.EventListners.NEWLY_ADDED_LISTENER) {
          // If the event is running on a scheduled polling, it is only necessary to schedule
          // the alarm manager. If the same DEFAULT_START_TIME and DEFAULT_INTERVAL
          // can be used for any new event, there is no need to create a new
          // scheduled alarm here.
    • Option 2: Setting up a new alarm

      Use this method, if you wish to change the DEFAULT_START_TIME and DEFAULT_INTERVAL values. 

      You will be using the same EventAlarmReceiver class, which is in the package, in both option 1 and option 2.




        • DEFAULT_LISTENER_CODE: The code used by the EventAlarmReceiver to identify the event listeners. Make sure to change this field to a preferred value.

        • DEFAULT_START_TIME: Define the time the alarm should first go off, in milliseconds.
        • DEFAULT_INTERVAL: Define the interval between subsequent repeats of the alarm, in milliseconds.
        • For more information on the default configuration, see file.
      2. Configure the EventAlarmReceiver class that is in the package.
        1. Configure the Constants.EventListners.DEFAULT_LISTENER_CODE  property by updating it with the new code you defined in step a.

          if (requestCode == Constants.EventListners.DEFAULT_LISTENER_CODE)


          if (requestCode == Constants.EventListners.NEWLY_ADDED_LISTENER_CODE)
        2. Write a logic inside the startListening() to periodically poll the API.

          if (requestCode == Constants.EventListners.DEFAULT_LISTENER_CODE) {
              RuntimeStateListener runtimeStateListener = new RuntimeStateListener();
  • No labels