How to automatically reconnect with Easyble.js

Ask, and ye shall receive.
biczak
Posts: 6
Joined: 15:43, 09 Sep 2015

How to automatically reconnect with Easyble.js

Postby biczak » 20:33, 04 Oct 2015

I am using a device that every time it is used, it very briefly shuts off BLE before turning it back on. This process takes about 1 second, but in that time it disconnects from my app. However, I can not seem to find a working method that has my app automatically reconnect to the device. So I'm wondering how
1) Do I let my app know that the device is no longer connected, or is that automatic?
and
2) How do I get the app to start scanning only when the device disconnects?

I'm new to this whole thing so if you could be as detailed as possible I would very much appreciate it!

User avatar
micke
Posts: 256
Joined: 20:49, 18 Nov 2013

Re: How to automatically reconnect with Easyble.js

Postby micke » 11:51, 05 Oct 2015

Hi, here is a way to do what you are asking, some "pseudo-code" that scans and connects to a device with known name or address, then connects again when disconnected.

Code: Select all

function startScanning()
{
    // Stop any ongoing scan.
    evothings.easyble.stopScan()
   
    // Start scanning.
    evothings.easyble.startScan(deviceFound, scanError)
}
       
function deviceFound(device)
{
    // Log device name and address to the Workbench Tools window.
    hyper.log('Found devices: ' + device.name + ' ' + device.address)

    // Connect to the device with a given name or address.
    // TODO: Use one of these methods to identify the device, insert
    // actual name or address printed to log. (First run the app to
    // determine value of name/address for your device.)
    if ('myname' == devices.name)
    //if ('myaddress' == devices.address)
    {
        evothings.easyble.stopScan()
       
        // TODO: Connect to device.   
    }
}

function scanError(error)
{
    if (evothings.easyble.error.DISCONNECTED == error)
    {
        // Scan and connect again (wait a second to be safe, might not be needed).
        setTimeout(startScanning, 1000)
    }
}


The method used is based on connecting to a device with a known name or address. Use whatever works for identifying your device.

On iOS the situation is a bit tricky. The device name is somehow cached by iOS so if the name is changed it may not reflect in the value reported during scan. Further more, the device address is a "semi-persistent" id unique to each phone (won't work to share the value with other phones, so publishing an app using the address value as identifier will not work). On approach is to use data delivered in the advertisement data record. Let me know if you want help with this. What ever method will word depends on what is actually advertised by your BLE device.

Best regards, Mikael

biczak
Posts: 6
Joined: 15:43, 09 Sep 2015

Re: How to automatically reconnect with Easyble.js

Postby biczak » 18:56, 05 Oct 2015

Mikael,

Thank you very much for your response! I understand what are you saying and why it should work. However, I still am having difficulty in my app. I have no problem scanning for, finding, and connecting to my device. But when my device disconnects from my app, the app does not start to scan again.


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 6 guests