Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Bluetooth 사용을 위한 location permission #12

Open
im-yeobi opened this issue Sep 6, 2019 · 1 comment
Open

[Android] Bluetooth 사용을 위한 location permission #12

im-yeobi opened this issue Sep 6, 2019 · 1 comment
Labels
Android Android 관련 내용 작성 완료 완료된 이슈

Comments

@im-yeobi
Copy link
Owner

im-yeobi commented Sep 6, 2019

Android Bluetooth는 왜 ?

  • 회사에서 'iOS는 블루투스 사용 시 위치 권한이 필요하지 않은데, AOS에서는 왜 필요한가?'를 주제로 한창 얘기가 많이 나왔다.
  • '구글 정책이 그렇다'라는 개발자스럽지 않은 말을 하고 싶지 않아 공식문서를 찾아보고 직접 안드로이드 블루투스 기능을 구현해보았다. (사실 공식 문서에 구현방법이 다 나온다..)

정리

  • 공식 문서 참고
    bluetooth location permissionn

  • 위 내용을 보면 안드로이드에서 블루투스를 사용하기 위해서는 두 가지 권한이 필요하다고 나온다.

첫 째, 위치 권한. 위치 권한에는 두 종류가 있다. coarse(대략적인 위치), fine(정확한 위치)

  • 그렇다면 왜 위치 권한을 필요한가?
  • 블루투스 스캔을 사용하여 사용자의 위치 정보를 수집할 수 있다. 사용자의 기기에서 외부에 위치 정보를 제공하거나, 블루투스 비콘(근거리 무선 통신 기술)의 위치 정보를 사용자의 기기로 얻어오기 위해서 위칙 권한이 필요하다.
  • 쉽게 생각하면, 블루투스 통신을 통해서 사용자 혹은 외부의 위치 정보를 얻어오는 경우가 있기 때문에 위치 권한이 필요하다는 것이다.

둘 째, 블루투스 권한.

  • BLUETOOTH, BLUETOOTH_ADMIN 권한이 필요하다. 블루투스를 사용하기 위해 당연히 블루투스 권한이 필요하겠지?
@im-yeobi im-yeobi added Android Android 관련 내용 작성중 진행중인 이슈 labels Sep 6, 2019
@im-yeobi im-yeobi changed the title [Android] Bluetooth permission [Android] Bluetooth 사용을 위한 locationpermission Sep 7, 2019
@im-yeobi im-yeobi changed the title [Android] Bluetooth 사용을 위한 locationpermission [Android] Bluetooth 사용을 위한 location permission Sep 7, 2019
@im-yeobi
Copy link
Owner Author

im-yeobi commented Sep 7, 2019

구현

  • 스캔
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    bluetoothAdapter.stopLeScan(leScanCallback);
                }
            }, SCAN_PERIOD);   // SCAN_PERIOD 시간 만큼 딜레이

            Log.d("Bluetooth scan", "Scanning");
            mScanning = true;
            bluetoothAdapter.startLeScan(leScanCallback);
        } else {
            mScanning = false;
            bluetoothAdapter.stopLeScan(leScanCallback);
        }
    }
  • 스캔 완료후 콜백
    private BluetoothAdapter.LeScanCallback leScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 스캔 된 블루투스 기기 처리 
                        }
                    });
                }
            };
  • 앱 이미지

app image

  • 간단하다. BLE scan 버튼을 클릭하면 블루투스 스캔이 일어난다. 나는 여기에서 위치 권한을 AndroidManifest 에서 넣은 경우와 뺀 경우 두 가지를 체크해봤다.
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <!--<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />-->
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
  • '위치 권한을' 뺀 경우에는 다음과 같은 에러가 발생했다.

error image

  • 위 과정을 통해 확실한 결과를 얻었다. 안드로이드에서 블루투스를 사용하기 위해서는 위치 권한이 필수로 필요하다.

References

@im-yeobi im-yeobi added 작성 완료 완료된 이슈 and removed 작성중 진행중인 이슈 labels Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Android 관련 내용 작성 완료 완료된 이슈
Projects
None yet
Development

No branches or pull requests

1 participant