How to access a native cordova plugin?

How to access a native cordova plugin? My case:

I added a method to a plugin in the plugins folder. I already tested it on native cordova project. It works great. But the ionic has a wrapper for this plugin. Now I just want to bypass the ionic wrapper and access the cordova js API to call a method.

Ok, to ignore the typescript errors you can just declare an any variable like let myPlugin: any = ionicPlugin; and you be able to call whatever you want. It’s an ugly way, necessary for special reasons.

Or how to extend the ionic wrapper?

Depends of the plugin but you could write your own definition’s file

For example, I access the calendar without ionic-native. I have a cordova.d.ts file I wrote in my project where I’ve got

 interface Window {
    plugins: WindowPlugins;
}

interface WindowPlugins {
   calendar: ICalendarService;
}

interface ICalendarService {
    createEvent(title: string, eventLocation: string, notes: string, startDate: Date, endDate: Date, success: any, error: any);
}

which could then be use in my app code like

window.plugins.calendar.createEvent(...

but if the ionic-native wrapper exists, I would rather use it. More users support/helps to maintain these

Thanks for fast response.
I’m using the Ionic BLE Plugin.

I tried to call

console.log(Object.keys((<any>window).plugins));

result: Some plugin names but not ble.

Now I found out, that the ble variable es accessible in global scope (window).

console.log(Object.keys((<any>window).ble));

result: All original methods, but not my custom added. In a native cordova project it worked. Maybe the ionic has to re-build it. But how? The changes are not yet built on installed app. (using ionic cordova run android -lc)
I’ll test to reinstall the android platform.

Ok, it worked. On ionic you have to reinstall the platform. (or maybe there is another solution?)
Now you can access the plugin e.g. by:

(<any>window).ble.yourMethod();

This is a quick’n’dirty solution. Of course typed-files are useful.

There is a wrapper actually, you could follow the doc https://ionicframework.com/docs/native/ble/

Right, I already using it. But the wrapper is not actual up to date. Because I’ve just updated the cordova plugin. To test the new method, I easiely bypass the ionic wrapper. In up coming releases (if my pull-request gonna be merged), the ionic wrapper should be updated too.
In this case I just updated the source files in the ionic project /plugins/ble/ folder (www for the JS API and src/android test it.

Otherwise I have to wait until the the ionic wrapper is updated. Or I have to fork it, publish to npm and update my packages. A long way. Actually I have no idea how the workflow is until ionic updated the wrapper to the latest plugin version.

1 Like

Simply awesome :thumbsup: