IT recording...
[Android] BluetoothDevice.ACTION_FOUND / 블루투스 디바이스 찾기 해도 BroadcastReceiver에 들어가지 않는 오류 본문
Android
[Android] BluetoothDevice.ACTION_FOUND / 블루투스 디바이스 찾기 해도 BroadcastReceiver에 들어가지 않는 오류
I-one 2020. 9. 9. 12:11mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadCastReceiver,discoverDevicesIntent);
를 통해 인텐트필터를 등록하고, 브로드캐스트에 해당 action을 받아도 broadcastlistener가 작동하지 않는 일이 발생했다. mBluetoothAdapter.ACTION_SCAN_MODE_CHANGED 이런거는 잘 되는데 왜 ACTION_FOUND만 안되는지 한참 고민하고 오타가 있는건가 찾았는데 답은 user에게 물어보는 별도의 권한을 줘야하는 것이었다.
1. manifest에 권한 추가
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
2.아래와 같은 함수를 작성한 후, startDiscovery 하기 전에 checkBTPermissions()를 호출하면 된다.
@RequiresApi(api = Build.VERSION_CODES.M)
private void checkBTPermissions(){
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
if(permissionCheck!=0){
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},1001);
}
else{
Log.d("checkPermission", "No need to check permissions. SDK version < LoLLIPOP");
}
}
}
** checkBTPermission()을 호출하면 생기는 빨간줄은 버전때문에 생기는 것인데 무시해도 된다고 한다.
---전체 코드
//onCreate 안에
btnFindDiscover.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
bluetoothDiscover();
}
});
//bluetoothDiscover()
public void bluetoothDiscover(){
if(mBluetoothAdapter.isDiscovering()){ //already discovering > cancel > discover
mBluetoothAdapter.cancelDiscovery();
Toast.makeText(getApplicationContext(), "Canceling discovery", Toast.LENGTH_SHORT).show();
checkBTPermissions(); //check for permissions
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadCastReceiver,discoverDevicesIntent);
}
else if(!mBluetoothAdapter.isDiscovering()){
checkBTPermissions();
Toast.makeText(getApplicationContext(), "Starting discovery", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadCastReceiver,discoverDevicesIntent);
}
}
//checkBTPermissions()
@RequiresApi(api = Build.VERSION_CODES.M)
private void checkBTPermissions(){
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
if(permissionCheck!=0){
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},1001);
}
else{
Log.d("checkPermission", "No need to check permissions. SDK version < LoLLIPOP");
}
}
}
//BroadcastReceiver
private final BroadcastReceiver mBroadCastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)){
//get devices
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBTdevices.add(device);
//attach device to adapter & set list
mDeviceListAdapter = new DeviceListAdapter(context,R.layout.device_adapter_view, mBTdevices);
newDevicesList.setAdapter(mDeviceListAdapter);
}//end if
}//end onReceive
};
**참고
stackoverrun.com/ko/q/10525893
stackoverrun.com/ko/q/10614409
'Android' 카테고리의 다른 글
[Android] 안드로이드 블루투스 (2) - Discoverable / Find device (0) | 2020.09.09 |
---|---|
[Android] 안드로이드 블루투스(1) - Bluetooth on/off (0) | 2020.09.09 |
[Android] BLE (블루투스 통신) (0) | 2020.09.08 |
[안드로이드] AndroidStudio & Git연동 (git 터미널 사용법, gitHub 연동, gitLab연동, SSH) (0) | 2020.09.07 |
[Android] missing constraints in constraintlayout 에러, 오류 (0) | 2020.08.16 |
Comments