Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot map Q.Promise objects needed for iBeacon plugin #241

Closed
Ritzlgrmft opened this issue Jun 24, 2016 · 5 comments
Closed

Cannot map Q.Promise objects needed for iBeacon plugin #241

Ritzlgrmft opened this issue Jun 24, 2016 · 5 comments

Comments

@Ritzlgrmft
Copy link
Contributor

I am trying to create the definition for the iBeacon plugin (see #223). But unfortunately, it doesn't work as expected. As far as I can see, the native method of the plugin gets called, returns also a result, but the promise in my calling code is never resolved.

My plugin definition looks like:

@Plugin({
    plugin: 'cordova-plugin-ibeacon',
    pluginRef: 'cordova.plugins.locationManager'
})
export class IBeacon {
    @Cordova()
    static isBluetoothEnabled(): Promise<boolean> { return; }
}

My call is:

IBeacon.isBluetoothEnabled()
    .then(enabled => { ... })
    .catch(e => { ... });

Neither catch nor then get called.

My suspicion is that the problem is that the iBeacon plugin maps the result in Q.Promise objects. And that there the mapping in ionic-native does not work. But unfortunately, I do not know how to fix it.

@ihadeed
Copy link
Collaborator

ihadeed commented Jun 26, 2016

Hey there,

Thanks for contributing to ionic-native.

Your implementation is invoking the isBluetoothEnabled() function this way:
cordova.plugins.locationManager.isBluetoothEnabled(function(res){ ... }, function(err){ ... });

Reason: that's what the @Cordova() decorator does (with the default options).


I just had a look at the docs for that function here.

The iBeacon plugin is relatively odd. They return a Q promise with each function call (not sure about all functions, but this is the case for the one you're working with now).

We can do one of the following:

  • Return that promise as-is (use @Cordova({ sync: true }); and it will do that for you, keep return type as Promise). I'm not sure if this will work since we're dealing with different promise libraries here.
  • Add a new option to the @Cordova decorator that will convert any promise to the promise type we're using. (ionic-native returns back a promise that is compatible with your environment, ES-6 promise or Angular1 promise).

I would give the first option a shot first, if that doesn't work well in all scenarios then we can look into modifying the @Cordova decorator to work with this plugin.

@Ritzlgrmft
Copy link
Contributor Author

I just tried @Cordova({ sync: true }). Generally, it works now, the promise gets now resolved. I think it's not too bad, that the promise has a different source, since at least then and catch seem to be quite similar.

However, in my test page, I display the results of the cordova calls. This does not work immediately, since change detection in Angular 2 gets not triggered. So I would really appreciate, if you could add a new option to the @Cordova decorator converting Q.Promise into Promise.

@Ritzlgrmft
Copy link
Contributor Author

I added a new option @Cordova({ otherPromise: true }). With this option, the plugin is called like with the sync option, and afterwards the promise is wrapped into the promise used by ionic-native.

Could you check the implementation at https://github.com/Ritzlgrmft/ionic-native/blob/master/src/plugins/plugin.ts#L99? If it is fine for you, I will merge it as soon as the plugin is working.

@Ritzlgrmft
Copy link
Contributor Author

And another question: for startMonitoring (see https://github.com/petermetz/cordova-plugin-ibeacon#start-monitoring-a-single-ibeacon), I need a parameter of type BeaconRegion. In vanilla JavaScript this looks like:

var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor);
cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion);

Using ionic-native, I would like to write

var beaconRegion = new IBeacon.BeaconRegion(identifier, uuid, major, minor);
IBeacon.startMonitoringForRegion(beaconRegion);

The second line is working, but the first line not. For this, I assume I have to define the BeaconRegion also in some way. But I do not know, how?

@ihadeed
Copy link
Collaborator

ihadeed commented Jul 11, 2016

Hey @Ritzlgrmft

Please see this: #270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants