Connection Question

I've observed some (possibly) strange behavior in android with respect to reliably connecting to Metawear devices.

I setup the connection target like this (I know the Metawear's MAC ahead of time):

final BluetoothDevice remoteDevice_base =
btManager.getAdapter().getRemoteDevice(BuildConfig.BASE_MAC_ADDRESS);
mwBoard_base = serviceBinder.getMetaWearBoard(remoteDevice_base);

The behavior I observe is that if I don't first do an explicit Bluetooth scan before trying to connect, the connection will always fail. This could be using something like:
mBluetoothAdapter.startLeScan()

...or it could be performed by disabling and re-enabling Bluetooth so the OS performs a scan.

If you know the address of the device you would like to connect to, is there a preferred efficient method to connect to the target Metawear without performing an explicit scan? I'd like to make this process as efficient as possible if I have a cached set of addresses I want to connect to.

Comments

  • Does the connection eventually succeed if you try again?

    I haven't had issues with getRemoteDevice on our Nexus and older Samsung devices. Maybe your Android OS / device requires the device to have been detected during a scan before it can successfully connect. Not all Bluetooth stacks are equal on Android.

  • That was my suspicion. After a bunch of debugging, I found that the connection attempt NEVER succeeds until I do an explicit scan, then always succeeds on the first attempt once the device list is populated by an explicit scan.

    My eventual application has devices dynamically moving in and out of range from the phone and connecting + streaming data, so I think I'll have to run a background scan periodically to populate the device list before trying to connect. Since I don't know what particular phones the end users will have, I suppose I will need to play it safe and perform some explicit scans.

    For what it's worth, I'm using an LG Stylo 3 -- a relatively recent, but low-end phone.

This discussion has been closed.