Binding VestelService to Applications (Android) | VESTEL B2B Support Portal

Binding VestelService to Applications (Android)

Binding VestelService to Applications (Android)

This document explains how to use the VestelService interface in your applications. Briefly, it consists of 3 steps: Creating Folder Structure for aidl, Connecting to VestelService and Using VestelService Methods.

IVestelService.aidl

First, you need the aidl file to make calls to VestelService. This file should be placed into the following directory in your project:

// Android Studio

> src
   > main
      > aidl
         > android
            > os
               > IVestelService.aidl

// Eclipse
> src
   > android
      > os
         > IVestelService.aidl

The reason we have to put it there is that package of the aidl file you use in your project should match the original aidl’s package in the framework. You most likely will miss some of these directories in your project. If so, please create the folder structure like above.

Java
package android.os;
/**
* {@hide}
*/
interface IVestelService {
    String sendCustomUartCommand(String command);
}

Connect to VestelService

The following example shows how the connection can be established. Please notice that bindService returns a boolean based on whether it has found the service or not. If it returns true, it doesn’t necessarily mean that connection has been made. There is a delay between bindService returning true and receiving the onServiceConnected callback. During this period, mService might still be null, especially if it’s used immediately after bindService. Because of this, it’s a good idea to check whether mService is null or not before using it.

Java
// Following imports are needed
import android.os.IVestelService;
import android.os.IBinder;
import android.content.ServiceConnection;
import android.content.Intent;
import android.content.ComponentName;
 
// Declare following variables in your class for VestelService connection
private IVestelService mService;
private ServiceConnection mServiceConnection;
 
// Example function to establish VestelService connection.
private void connectToVestelService(){
    this.mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            mService = IVestelService.Stub.asInterface(binder);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };
    // Bind to VestelService
    bindService(new Intent("VestelServiceConnector").setPackage("android"),mServiceConnection,BIND_AUTO_CREATE);
}

Note: Please be extra careful if you’re trying to bind during the application’s lifecycle. Since onServiceConnected is executed in the same thread as the lifecycle, if you try to bind in for example onCreate method, your connection will not be made before onCreate ends no matter how much you wait. So trying to make calls during the lifecycle can be unpredictable. If you need to make some initializations after the connection is made, you can do it in onServiceConnected method instead.

To prevent leaks, you should unbind the service when you’re done with it. You can do this in onDestroy method:

Java
@Override
public void onDestroy() {
    super.onDestroy();
    if (mServiceConnection != null) {
        unbindService(mServiceConnection);
    }
}

Use VestelService Methods

If connection is successful, you can call functions defined in aidl to communicate with VestelService using IVestelService instance returned from onServiceConnected. There’s a limit to how long sendCustomUartCommand waits for a response after a command is sent. If it times out, it returns null so you should check the returned string before using it.

Java
if (mService != null){
    mService.sendCustomUartCommand("SETRTCDATE 10:10:2010 10:10");
    mService.sendCustomUartCommand("GTZ"); // Get timezone
     
    // Read volume level
    String volStr = mService.sendCustomUartCommand("GETVOLUME");
    if (volStr != null){
        Log.i("VolumeResponse",volStr);
    }
}

Sample Application

You can access the sample application prepared by Vestel via the link below. Application screenshot is shown in Figure 1.

VestelService Application

Figure 1: VestelService App Sample View

Additionally, you can access all RS232 commands from the below link.

Advanced LAN/RS232 Commands