3

TL;DR: Can you be connected to devices and scan for more devices at the same time?

I'm working on an app that talks to devices using BLE. A colleague who just finished a BLE project warned me that you can't be connected to devices and scan at the same time or bad things will happen. I've gone forward with this assumption. So currently I connect and disconnect on each screen.

Connection times can take ~3+ seconds sometimes so I'd like to speed this up by not disconnecting if I'm deep in the app (which is also where you can scan for more devices). I've played with it a little and everything seems fine (doing both at the same time).

I've read in the Android docs that you can't scan for Bluetooth and BLE at the same time but I haven't seen anything about being connected to BLE devices and scanning for BLE devices.

Some of the symptoms my colleague described were:

  • Unable to turn off system Bluetooth (the switch would turn back on)
  • Bluetooth would crash (not sure if it was Bluetooth Share or something else)
  • Other apps doing Bluetooth stuff would crash
Jeff Engebretsen
  • 666
  • 2
  • 8
  • 21
  • Theoretically I believe it should be possible but I have seen all the bugs your colleague reports over time and now wait for the scan to stop before trying to open devices. I also only try and have outstanding requests on a single device at a time which helps with stability a bit but is very bad for fast start-up and code structure. I only release my BLE code on a beta basis which is stupid 2 years after Google finally added support. – Ifor Sep 23 '15 at 22:26

1 Answers1

1

You can be connected and scan at the same time... though those symptoms do exist on some phones/versions of Android. (Personal experience with some Samsung devices)

Bluetooth can manage multiple connections, so it would make sense for you to be able to scan and be connected at the same time.(Multiple bluetooth connection).

In the older APIs (18-20), you would use the startLeScan in the BluetoothAdapter to search for BLE devices. In my experience, this did occasionally cause the issues you described.

However, if you are using APIs 21+, using BluetoothLeScanner.startScan is fairly pain-free and doesn't require you to handle a timer to restart the scan. You just give it a filter of Bluetooth devices you are looking for and a callback, and it will notify your app whenever it finds devices that match your criteria.

Community
  • 1
  • 1
afathman
  • 5,993
  • 2
  • 20
  • 28