Finding BLE Devices
suggest changeThe following permissions are required to use the Bluetooth APIs:
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
If you’re targeting devices with Android 6.0 (API Level 23) or higher and want to perform scanning/advertising operations you will require a Location permission:
android.permission.ACCESS_FINE_LOCATION
or
android.permission.ACCESS_COARSE_LOCATION
Note: Devices with Android 6.0 (API level 23) or higher also need Location Services enabled. On those versions the user grants the location permission at runtime.
A BluetoothAdapter object is required to start scanning/advertising operations:
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
BluetoothLeScanner.startScan(ScanCallback) is the basic way to start a scan. The callback receives the results:
bluetoothAdapter.getBluetoothLeScanner().startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
Log.i(TAG, "Remote device name: " + result.getDevice().getName());
}
});
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents