Retrieve and display a value from a native Cordona plugin

I’m trying to retrieve an identification number from my native SDK plugin I have built in Cordova but I can’t see to get it no matter what I try. I have a native Android application that gets the number from the device but the code doesn’t translate to Cordova directly.

Working Android Code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Device = SdkForAndroid.getInstance(this);

    // First, we test if the device was already initialized.
    if (!Device.isInitialized()) {

        Log.i(TAG, "mobile device not initialized yet");

        initializeSDK();

    } else {

        // if we already initialized, show the Secured Objects we got

        //Log.i(TAG, "device is initialized. deviceId: " + mobileDevice.getDeviceId());
        TextView textView = (TextView) findViewById(R.id.txtDeviceId);
        textView.setText("" + Device.getDeviceId());
    }
}

Private void initializeSDK() {
    new Thread() {

        @Override
        public void run() {
            try {
                String deviceId = Device.initialize();//hidden sdk code

                Log.i(TAG, "initialized SDK, deviceId = " + deviceId);
            } catch (Exception e) {
                Log.e(TAG, "failed to initialize SDK", e);
            }
        }
    }.start();
}

Cordova code:

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;
    if (action.equals("initializeSDK")) {
        cordova.getActivity().runOnUiThread(
            new Runnable() {
                public void run() {
                        isInitialized();
                    }
                });
            return true;
        }
protected void isInitialized(){
                  Device = SdkForAndroid.getInstance(cordova.getActivity());
                  try{
                        if (!Device.isInitialized()) {
                            Log.i(TAG, "mobile device not initialized yet");
                            initSDK();
                        } else {
                            Log.i(TAG, "mobile device is initialized. deviceId: " + Device.getDeviceId());
                        }
                        this.callbackContext.success(mobileDevice.getDeviceId());
                  } catch (Exception e){
                        this.callbackContext.error("Error");
                  }

}

protected void initSDK(){
    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
                    try{
                        String deviceId = mobileDevice.initialize()
                            //hidden sdk code
                        Log.i(TAG, "initialized SDK, deviceId = " + deviceId);
                                callbackContext.success(deviceId);
                            } catch (Exception e){
                                Log.e(TAG, "failed to initialize SDK", e);
                                callbackContext.error("Error");
                            }
                }
            });
}

Javascript code:

var exec = require('cordova/exec');

module.exports = {
    initializeSDK: function (success, fail) {
        cordova.exec(success, fail, 'BlueIDplugin', 'initializeSDK', []);
    }
};

Ionic appcomponent.ts

initiateSDK(){

    this.BlueIdPlugin.initializeSDK(
      function(success){
        console.log(deviceId);
      },
      function(fail){
        console.log("Error getting device id")
      }
    )

    this.BlueIdPlugin.initializeSDK();
    console.log(this.BlueIdPlugin.getDeviceId());
  }

I think I’m linking the code correctly as the SDK is initialised with my console log. The problem is that I cannot get the deviceId to display. I’m really unsure about how to interact with the problem in ionic itself, I believe the problem actual lives there as I’m not calling the success callback correctly. Please help! :(

Seems you are calling the success callback twice, even if device is not initialised:

this.callbackContext.success(mobileDevice.getDeviceId());

This results in callbackContext being invoked twice, which Cordova does not allow (unless you request it explicitly). So try this:

Cordova code:

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;
    if (action.equals("initializeSDK")) {
        cordova.getActivity().runOnUiThread(
            new Runnable() {
                public void run() {
                        isInitialized();
                    }
                });
            return true;
        }
protected void isInitialized(){
    Device = SdkForAndroid.getInstance(cordova.getActivity());
    try{
        if (!Device.isInitialized()) {
            Log.i(TAG, "mobile device not initialized yet");
            initSDK();
        } else {
            Log.i(TAG, "mobile device is initialized. deviceId: " + Device.getDeviceId());
            this.callbackContext.success(Device.getDeviceId());
        }
    } catch (Exception e){
        this.callbackContext.error("Error");
    }

}

protected void initSDK(){
    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
                try{
                    String deviceId = mobileDevice.initialize()
                        //hidden sdk code
                    Log.i(TAG, "initialized SDK, deviceId = " + deviceId);
                    callbackContext.success(deviceId);
                } catch (Exception e){
                    Log.e(TAG, "failed to initialize SDK", e);
                    callbackContext.error("Error");
                }
        }
    });
}

appcomponent.ts:

initiateSDK(){
    this.BlueIdPlugin.initializeSDK(
      function(success){
        console.log(deviceId);
      },
      function(fail){
        console.log("Error getting device id")
      }
    )
}
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Retrieve and display a value from a native Cordona plugin

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录