Android Bluetooth device scan/discovery for CLassic and Low Energy Devices sequentially -
i developing android app searches classic , low energy bluetooth devices such when press "search" button show me bluetooth devices (low energy , classic) in range. since classic bt discovery , le scanning different things, have implement them separately , combine them in 1 function such that
searchfirstlowenergythenclassic() or searchfirstclassicthenlowenergy()
in order implement this, have know when discovery/scanning ends start scan/discovery other technology.
here implementation:
- started classic bt discovery
- received bluetoothadapter.action_discovery_finished
- started ble scaning -> onreceive action equals(action_discovery_finished)
- stop search when ble scan ended
this looks ok there problem when extend behavior. when want search, start searching first le scan or classic discovery based on last connected technology. example if last time device connected classic bt device, searchfirstclassicthenlowenergy() run. otherwise, searchfirstlowenergythenclassic().
so might guess, gets more complicated. example, when classic bt discovery ends, app should know whether search ended or should proceed le scan.
there issue. when user stops search during scan/discovery of first technology, recieve bluetoothadapter.action_discovery_finished shouldn't start le scan since search terminated user.
i implemented using flags (not working properly, though) code looks dirty.
else if (bluetoothadapter.action_discovery_finished.equals(action)) { // classic bluetooth discovery ended lastopenedtype = getlastopenedtype(); if (lastopenedtype == bt_classic && !issearchstoppedbyuser()) { // search should continue low energy scan startbtlescanning(); } else if (lastopenedtype != bt_classic && !issearchstoppedbyuser()){ // search ended searchprogresslayout.setvisibility(view.invisible); } else { // search ended user searchprogresslayout.setvisibility(view.invisible); } }
in short, asking if has more brilliant , simple solution on this?
ps. solution without broadcast intent appreciated if possible.
bluetoothadapter's startdiscovery() method searches both classic , ble devices. once result scan, can separate them based on type of device. example:
int devicetype = device.gettype(); if(devicetype == bluetoothdevice.device_type_classic) { } else if(devicetype == bluetoothdevice.device_type_le) { } else if(devicetype == bluetoothdevice.device_type_dual) { }
so, it's not needed search separately.
Comments
Post a Comment