user1622343
user1622343

Reputation: 969

How do I use AIDL or talk to Android Services from codenameone?

I am building an Android POS application using codenameone. I want to use the CMSoft BT-Printer SDK from here http://www.cm-soft.com/AndroidPrinterSDK.htm. This uses an AIDL interface. How would I access it from Codenameone project?

Upvotes: 1

Views: 703

Answers (1)

Chen
Chen

Reputation: 3760

1)Create in your project a regular interface that extends NativeInterface to communicate with the printer service.

2) interface PrinterInterface extends NativeInterface{

public void bindService(); public void startScan(); public void stopScan(); }

3)right click on the interface and select "Generate Native Access" - this will create implementation files under the native directory in the project.

4)under the native/android dir you will get a PrinterInterfaceImpl class make sure the isSupported() method returns true and now simply implement your android code in this class.

use AndroidNativeUtil.getActivity() to gain access to your Activity. for example:

AndroidNativeUtil.getActivity().registerReceiver(mReceiver, new IntentFilter(RECEIVER));

AndroidNativeUtil.getActivity().unregisterReceiver(mReceiver);

5)in the impl class you can bind your receiver:

final class ScannerReceiver extends BroadcastReceiver {
    @Override

    public void onReceive(Context context, Intent intent) {            
        String data = null;

        if (intent.getAction().equals(RECEIVER)) {
            data = intent.getStringExtra(DATA);
        }

        if (data != null) {
            String msg;

            if (data.startsWith("S:")) {                 
                msg = data.substring(data.indexOf(':', 2) + 1);                    
            }                

            if (data.startsWith("D:")) {
                msg = data.substring(data.indexOf(':', 2) + 1);                    
            }
        }
    }        
}

private final ScannerReceiver mReceiver = new ScannerReceiver();
private final Intent mService = new Intent(SERVICE);

Upvotes: 2

Related Questions