my goal is to find nearby Bluetooth devices(LE devices and "Classic"), in order to associate between current visible nearby devices to some functionality my app doing with it. (not a specific device/devices, but all of them!!!)
what I know:
startLeScan()
would callback only with BLE devicesBluetoothDevice
found via broadcasts. what I don't know for sure:
startScan()
will find always both discoverable BLE and classic devices. BluetoothDevice.connectGatt()
added with the new BLE API's, but should work also with classic bluetooth(return GATT Services...).what I would like to know:
startScan()
returns both types (Classic and BLE), what would be better to use in terms of battery consumption, performances, good practices and other aspects?my application will perform background scans periodically, so I would like to minimize the battery consumption impact as possible.
Call isEnabled() to check whether Bluetooth is currently enabled. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult() , passing in an ACTION_REQUEST_ENABLE intent action.
It allows remote devices to see this Bluetooth adapter when they perform a discovery. For privacy, Android is not discoverable by default. The sender of this Intent can optionally use extra field EXTRA_DISCOVERABLE_DURATION to request the duration of discoverability.
By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.
Set<BluetoothDevice> devices = BluetoothAdapter. getDefaultAdapter(). getBondedDevices(); BluetoothDevice glass = null; for (BluetoothDevice d : devices { ParcelUuid[] uuids = d. getUuids(); for (ParcelUuid p : uuids) { System.
You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetooth LE devices with startLeScan(). Caution: Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources.
Edit: On LG Nexus 4 with Android 4.4.2 startDiscovery() finds Bluetooth LE devices. On Samsung Galaxy S3 with Android 4.3 startDiscovery() doesn't find Bluetooth LE devices.
I have an off-market Chinese tablet that has BLE support, however, it always return a BLE equipped device with its name as "null" when I call startLeScan. The issue was resolved by calling startDiscovery. Remember that if your app targets 23 or above, you will need to have location permissions for startDiscovery to work correctly. Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With