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

BLE.autoConnect is not installed #614

Closed
Paul811 opened this issue Jun 27, 2018 · 24 comments
Closed

BLE.autoConnect is not installed #614

Paul811 opened this issue Jun 27, 2018 · 24 comments

Comments

@Paul811
Copy link

Paul811 commented Jun 27, 2018

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 ?

@Domvel
Copy link
Contributor

Domvel commented Jun 28, 2018

Because this plugin is for cordova. Ionic just wraps it to be conform with angular and typescript.
see https://github.com/ionic-team/ionic-native (you should post this issue here)

I already sent a pull-request to the ionic-native project and added the autoConnect method. It is also merged. But the ionic team has not yet released it.
Until they do you can workaround this. You can access this plugin directly from the window object (global scope) or write your own typed file to extend the plugin.

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.

@don
Copy link
Owner

don commented Jun 29, 2018

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.

See danielsogl/awesome-cordova-plugins#2573

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

Thank you for the answers !

Is it possible to have an example ?
Because I have an error...

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

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.

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

I have the following error : "TypeError: undefined is not a functiob (near'...window.ble.autoConnect...').
Do I have to initialize the varialbe "window" ?

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

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 autoConnect.

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.
Also look to your package json. What's the version of cordova-plugin-ble-central?

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

1.1.2

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

But I am on an Iphone, so I don't think that the problem comes from Android

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

Android is just an example. You should execute cordova clean or reinstall the ios platform. It should work with all platform. Did you do this?

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

Yes, but I have the following error when I do cordova clean :
capture

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

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 jcenter. Go to the file: \platforms\android\build.gradle and move maven.google.com to the top in buildscript and allprojects repositories Like

repositories {
  maven {
    url "https://maven.google.com"
  }
  jcenter()
}

and try it again.
If this doesn't help, remove the whole platform folder and try again.
Still not works? Maybe git clone you project again for a fresh new enviroment.

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

The cordova clean works well, but your code doesn't... Same error after the clean

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

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 ble.autoConnect is undefined message, you probably have not the latest version of this plugin. Note: Clean the platform folder (e.g. remove it) when you update the plugin.

package.json

"cordova-plugin-ble-central": "^1.2.2"

Could you read the file \plugins\cordova-plugin-ble-central\www\ble.js. Is there the autoConnect method? At line 112 Maybe the same file in platform folder has not this method. If so, it's a cache problem as described above in my previous posts.

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

No, I have the following error : TypeError: undefined is not a function
I think the problem comes from the (<any>window)
Why it doesn't works with this.ble.autoConnect ?

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

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 (<any>window) you access this BLE Plugin directly like a native javascript app with cordova will do.
Here are my changes to ionic-native.
You could write to the ionic team and ask for a release. And upgrade the ionic-native package like npm install --save @ionic-native/ble. The version should be greater than 4.7.0.

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

So I don't hoave to import { BLE } from '@ionic-native/ble'; ? How do I have to import the plugin ?

@Domvel
Copy link
Contributor

Domvel commented Jul 2, 2018

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 window.ble access.
We only use the window object because the wrapper has not yet wrapped all methods of this plugin. You only need this for the few methods.

Does the "normal" connect method work for you? Not the autoConnect method. Please try it to make sure that your usuage is correct.
If connect works, I have not idea what's wrong in your case. You could post your source code or link to your repository. But that is no guarantee that we can solve the problem. You also could ask stackoverflow or other forums like ionic forum. Because this does not look like an issue of this plugin.

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

Yes, the normal method works properly.
I do a scan in a page, when I click on the device I push another page and I send the device to the page, and I call the autoConnect function

@Paul811
Copy link
Author

Paul811 commented Jul 2, 2018

Why the autoConnect function is referenced in the Ionic Native Doc if we can't use it ? https://ionicframework.com/docs/native/ble/

@don
Copy link
Owner

don commented Jul 2, 2018

Should it works on an Iphone through Ionic DevApp ?

Not yet. ble.autoConnect is a new function. The Ionic DevApp is likely using an older version of the plugin.

Why the autoConnect function is referenced in the Ionic Native Doc if we can't use it?

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)

@Paul811
Copy link
Author

Paul811 commented Jul 3, 2018

Thank you !

@Paul811
Copy link
Author

Paul811 commented Jul 9, 2018

I have no error when I deploy the app on an Android device.
In my code, I scan the devices and when I click on a device, I call another page (DevicePage) and I send the device to this page. In the DevicePage, I call the autoConnect function and when the disconnect callback is called, I don't disconnect the ble device in order to be automatically reconnect when the device is in range. Each time I'm connected to a device, I created a page which is visible in the menu.

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 ?

@don
Copy link
Owner

don commented Jul 11, 2018

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.

@Paul811
Copy link
Author

Paul811 commented Jul 13, 2018

Thank you, it works well !

But I have to be in a "paring" mode. It means that I have to run the command sudo hciconfig hci0 leadv 0 and this, each time someone connected to the device. Do you know how can I active this mode forever ? Or how can the autoConnect function can work withtout this mode ?

Thank you

@don don closed this as completed Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants