-
Notifications
You must be signed in to change notification settings - Fork 607
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
BLE.autoConnect is not installed #614
Comments
Because this plugin is for cordova. Ionic just wraps it to be conform with angular and typescript. I already sent a pull-request to the ionic-native project and added the For example use it like: (<any>window).ble.autoConnect(device_id, data => {
console.log('Connected', data);
}, error => {
console.log('Cannot connect / disconnected', error);
}); It's a bit dirty but it works. And in the future you can replace it with the official ionic style. If they finally release it. Note you access directly the javascript function. There is no Observable or Promise returned. You have to pass two functions to the autoConnect method. This plugin has nothing to do with ionic. Ionic native is just a wrapper for this. And the wrapper is just outdated. Because these function are recently added to this project. |
For autoConnect to work properly, you need to use the callbacks, even in Ionic. When a device is autoconnecting and disconnecting the success and failure functions are called multiple times. Unfortunately with an Observable disconnect can only be called 1x, the the Observable is dead and you can't reconnect. |
Thank you for the answers ! Is it possible to have an example ? |
As wrote. Instead of: // In a class method, ble defined in contructor.
this.ble.autoConnect(deviceId).subscribe(data => {
console.log('Connected Data: ', JSON.stringify(data));
}, error => {
console.log('Cannot connect or peripheral disconnected.', JSON.stringify(error));
}); do // Same place.
(<any>window).ble.autoConnect(deviceId, data => {
console.log('Connected Data: ', JSON.stringify(data));
}, error => {
console.log('Cannot connect or peripheral disconnected.', JSON.stringify(error));
}); This is one working solution. You directly access the corodva plugin by the window object like in javascript. In typescript you have to cast to any first, otherwise ble is not found. You also could declare this type. But this is a easy and fast solution. A bit ugly / dirty. But this is a temporary solution anyway. Until the ionic-native wrapper is released. |
I have the following error : "TypeError: undefined is not a functiob (near'...window.ble.autoConnect...'). |
Maybe you have not the latest version of this plugin? OR you should reinstall the android platform ("platform" folder). Perhaps it's cached a old version without ionic cordova platform rm android
ionic cordova platform add android You can try a clean first. cordova clean
ionic-app-scripts clean Or check out your project from git again. (git clone). This should have not platform and no plugin folder. |
1.1.2 |
But I am on an Iphone, so I don't think that the problem comes from Android |
Android is just an example. You should execute |
You wrote that you used iOS.? This is an android problem. I hope you do not follow my android example and installed the android platform without using it. But maybe this is a problem with
and try it again. |
The |
I just tested it. It works for me. (<any>window).ble.autoConnect(deviceId, device => {
console.log('Connected', device);
}, error => {
console.log('Disconnected', error);
}); If you get a package.json "cordova-plugin-ble-central": "^1.2.2" Could you read the file |
No, I have the following error : |
Because - as wrote in my first post - Ionic Native has not yet released the wrapper with this methods. They already merged my pull-request with these methods. But not yet released. With |
So I don't hoave to |
That's not the problem. You can import the BLE and use the existing methods from this wrapper. It's not a problem to use both. The ionic-native module import AND Does the "normal" |
Yes, the normal method works properly. |
Why the autoConnect function is referenced in the Ionic Native Doc if we can't use it ? https://ionicframework.com/docs/native/ble/ |
Not yet. ble.autoConnect is a new function. The Ionic DevApp is likely using an older version of the plugin.
The documents are generated from the source code. I'm not sure if that version of the wrapper has been released or not. The 2 project are a bit out of sync. The function exists in the Cordova plugin, just not in the wrapper so you can use it, just not through the Ionic wrapper yet. See @Domvel's solution #614 (comment) |
Thank you ! |
I have no error when I deploy the app on an Android device. But the problem is the following : when the device is in range, the app doesn't connect automatically to the device : I have to do the same steps like the first connection. How do I have to call the autoConnect and where in order to have an automatic re-connection ? |
You should not need to call autoConnect after disconnect is called. Cordova is going to reconnect automatically and notify you when it happens. For this to work, you will need the new function signature from danielsogl/awesome-cordova-plugins#2573 or call Cordova directly with (window).ble.autoConnect(onConnected, onDisconnected). The autoConnect function can live for a long time and may be better in a service instead of on a page. |
Thank you, it works well ! But I have to be in a "paring" mode. It means that I have to run the command Thank you |
I'm currently coding an Ionic app.
I am on an Iphone 7, and I test the app through Ionic DevApp.
I'm trying to test the autoConnect function in order to be connected each time my RPI is near the phone in order to connect without scanning.
However, when the function is called, it is said that the BLE.autoConnect is not installed.
I did : ionic cordova plugin add cordova-plugin-ble-central
and it is said that the plugin is updated.
Can you help me ?
The text was updated successfully, but these errors were encountered: