Keeping metawear board paired on restart?

Hello, a small issue I'm having is that if I restart my phone the metawear Bluetooth device needs to be manually paired again. Is there any way to keep a bluetooth connection persistent on restart?

Thanks

Comments

  • Does it have to be paired? MetaWear sensors do not require to be paired and Apps should be able to reconnect in the background.
    What you are describing seems to be the way Bluetooth behaved years ago, are you using an old OS or system?

  • edited October 2019

    I'm using a Google pixel on android 10. The app compiles and targets sdk28.

    public void retrieveBoard(){
            // Get the MetaWear device via MAC address
            final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
            final BluetoothDevice remoteDevice = bluetoothManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);
            // Create a MetaWear board object for the Bluetooth Device
            board = serviceBinder.getMetaWearBoard(remoteDevice);
    }
    
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        serviceBinder = (BtleService.LocalBinder) service;
        retrieveBoard();
    board.connectAsync(do stuff)
    

    If I unbond the MetaWear device I get this exception:

    D/BluetoothGatt: refresh() - device:
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=5
    E/MetaWearSensorManager: java.util.concurrent.TimeoutException: Failed to connect and discover services within 10000ms
    D/BluetoothGatt: connect() - device: , auto: false
    registerApp()
    D/BluetoothGatt: registerApp() - UUID=0c65e8d3....
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=5

    I'm fairly new to this, is there something obvious I'm missing?

  • Don't bond, don't pair. Only connect. We don't support bonding or pairing.

  • edited November 2019

    If the device isn't paired on the Bluetooth settings I just get this error:
    java.util.concurrent.TimeoutException: Failed to connect and discover services within 10000ms

    I have the mac address saved and this is how I'm supposed to get the board according to the docs?

    final BluetoothDevice remoteDevice = bluetoothManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);
          // Create a MetaWear board object for the Bluetooth Device
          board = serviceBinder.getMetaWearBoard(remoteDevice);
    

    I'm not running any scan or anything, just connecting with the mac address.

    This gives me a valid Bluetooth device, I don't see why this wouldn't work:

    board = serviceBinder.getMetaWearBoard(remoteDevice);
    board.connectAsync()...

    The task always faults if I don't go into the phone and pair the MetaWear board with:
    Failed to connect and discover services within 10000ms

  • edited November 2019

    Can you try the same code on another Android device, maybe a year or two old (older hardware and OS)?

  • Now that you mention it, it seems to work just fine on a galaxy s7 running android 8 but not on a Google pixel on android 10.

    The older galaxy s7 also lets me programmatically bond the device if I want while the pixel doesn't.

  • I am afraid you might need to look into this. Some OSs and phones require pairing...it's obnoxious....see if you can find this info on the Pixel docs.

  • That might be the case, but is there a way to at least have the phone remember the metawear board so there is only one time pairing?

  • Just store the MAC address and use this for future connection.

  • edited November 2019

    I know that much, right now it's even hardcoded to my specific mac address.

    getRemoteDevice() gives me a valid bluetooth device but if I try to get data with connectAsync()...
    I get this error
    Failed to connect and discover services within 10000ms

    This only happens on my Google Pixel, everything works fine regardless of pairing on my galaxy s7

    Here is some of my bluetooth gatt logs produced by connectAsync():

        9999-9999 D/BluetoothGatt: connect() - device: <MAC address>, auto: false
        9999-9999 D/BluetoothGatt: registerApp()
        9999-9999 D/BluetoothGatt: registerApp() - UUID=<my uuid>
        9999-10016 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
        9999-10050 W/io.lumme.guava: Accessing hidden method Landroid/bluetooth/BluetoothGatt;->refresh()Z (greylist, reflection, allowed)
        9999-10050 D/BluetoothGatt: refresh() - device: <MAC address>
        9999-10050 D/BluetoothGatt: close()
        9999-10050 D/BluetoothGatt: unregisterApp() - mClientIf=6
        9999-10069 D/BluetoothGatt: connect() - device: <MAC address>, auto: false
        9999-10069 D/BluetoothGatt: registerApp()
        9999-10069 D/BluetoothGatt: registerApp() - UUID=<my uuid>
        9999-10043 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
        9999-10050 D/BluetoothGatt: refresh() - device: <MAC address>
        9999-10050 D/BluetoothGatt: close()
        9999-10050 D/BluetoothGatt: unregisterApp() - mClientIf=6
        9999-10070 D/BluetoothGatt: connect() - device: <MAC address>, auto: false
        9999-10070 D/BluetoothGatt: registerApp()
        9999-10070 D/BluetoothGatt: registerApp() - UUID=<my uuid>
        9999-10043 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
    

    Everything seems to work perfectly fine though if I manually pair the metawear device through the settings, but upon restarting the phone it's no longer paired, and createbond() doesn't seem to do anything.

    If things are working fine these are my gatt logs:

    10792-10792 D/BluetoothGatt: connect() - device: <MAC address>, auto: false
    10792-10792 D/BluetoothGatt: registerApp()
    10792-10792 D/BluetoothGatt: registerApp() - UUID=<my uuid>
    10792-10810 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
    10792-10810 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=<MAC address>
    10792-10810 D/BluetoothGatt: onConnectionUpdated() - Device=<MAC address> interval=6 latency=0 timeout=500 status=0
    10792-10819 D/BluetoothGatt: onConnectionUpdated() - Device=<MAC address> interval=36 latency=0 timeout=500 status=0
    10792-10843 D/BluetoothGatt: discoverServices() - device: <MAC address>
    10792-10819 D/BluetoothGatt: onSearchComplete() = Device=<MAC address> Status=0
    10792-10819 D/BluetoothGatt: setCharacteristicNotification() - uuid: <my uuid> enable: true
    
  • Actually, I found out how I can pair the device programmatically and I run into the issue of the board being extremely finicky. Even in the phone settings for the google pixel it will often take dozens of tries before working. I might just have to chalk it up to the phone at this point?

  • You may want to try to play with the Bluetooth connection settings.

  • edited November 2019

    @Laura said:
    You may want to try to play with the Bluetooth connection settings.

    You mean on the phone or the metawear board? Phone is just for testing.
    I was hoping connectAsync() would work well on most phones.

  • I mean conn parameters on the metawear side: libmetawear.mbl_mw_settings_set_connection_parameters(self.device.board, 7.5, 7.5, 0, 6000)

  • edited November 2019

    After playing around with nRF Connect it seems like the MMR unit just refuses to stay bonded on restarts? I successfully bond the device through either settings, my app, or nRF Connect. The app tells me that the device is indeed bonded. But as soon as I restart the device it's no longer bonded? This happens on all my devices and I don't understand why the bond info would be deleted unless it's on the end of the MMR board?

    I noticed that in this version of the settings there is a keepBond() method:
    https://mbientlab.com/documents/metawear/android/2/com/mbientlab/metawear/module/Settings.html

    but it was removed in this one:
    https://mbientlab.com/documents/metawear/android/3/com/mbientlab/metawear/module/Settings.html

    Does that mean I cannot maintain a bond with the device?(Our app is supposed to stay connected all the time with secure data transfer)

  • @danielj pairing and bonding are not supported by our devices.

    Apps are able to connect and re-connect in the background, and are identifiable by ID.

  • @Matt So the only option for persistent connection is connect when the phone turns on and receive unencrypted sensor data?

    It's odd that I can bond the device at all then.

  • @danielj That's correct.
    We have some code from side projects and from Nordic's bonding/pairing in our OS/firmware but we don't officially support bonding and pairing through our MetaWear firmware and APIs.

Sign In or Register to comment.