Finding BLE Devices

Bluetooth API를 사용하려면 다음 권한이 필요합니다.

1
2
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN

Android 6.0 (API 레벨 23) 이상의 장치를 대상으로하고 스캔 / advertising 작업을 수행하려는 경우 위치 권한이 필요합니다.

1
2
3
4
5
android.permission.ACCESS_FINE_LOCATION

or

android.permission.ACCESS_COARSE_LOCATION

참고 : Android 6.0 (API 레벨 23) 이상의 장치도 위치 서비스를 활성화해야합니다.

스캔 / advertising 작업을 시작하려면 BluetoothAdapter 객체가 필요합니다.

1
2
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();

BluetoothLeScanner 클래스의 startScan(ScanCallback 콜백) 방법은 스캔 작업을 시작하는 가장 기본적인 방법입니다.

결과를 받으려면 ScanCallback 객체가 필요합니다.

1
2
3
4
5
6
7
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());
}
});
공유하기