The 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 to have Location Services enabled.
A BluetoothAdapter object is required to start scanning/advertising operations:
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
The startScan (ScanCallback callback)
method of the BluetoothLeScanner class is the most basic way to start a scanning operation. A ScanCallback
object is required to receive 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());
}
});