블루투스 기기 uuid 받기
안드로이드의 BluetoothDevice 클래스의 메소드 getUuids()로 uuid를 받아와 화면에 출력하려고 합니다.
아래 코드의 device 는 BluetoothDevice 객체입니다.
다른 디바이스 정보들은 아무 문제없이 출력됐지만
이상하게 getUuids() 메소드로 받아온 ParcelUuid 객체에 접근만 하려고 하면 앱이 튕기더군요
코드 줄 777 : ParcelUuid [] pU = device.getUuids()
그래서 해결책을 찾았습니다
https://stackoverflow.com/questions/4032391/android-bluetooth-where-can-i-get-uuid
Android Bluetooth Where can I get UUID?
I want to connect 3 devices via Bluetooth. As for example I use BluetoothChat. So How I understand I should use different UUID for this devices. I have been trying to connect via such UUID=766c82f...
stackoverflow.com
getUuids() 메소드를 invoke 하는 방법입니다. 아마 device를 읽는 작업이 다 완료되지 않은 상태에서 getUuids() 메소드를 호출하여 null 이 반환되고 앱이 튕기지 않았나 추측됩니다.
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
try {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);
ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);
if(uuids != null) {
for (ParcelUuid uuid : uuids) {
Log.d(TAG, "UUID: " + uuid.getUuid().toString());
}
}else{
Log.d(TAG, "Uuids not found, be sure to enable Bluetooth!");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
|
cs |
퍼미션 추가도 잊지 마세요
mainfests 디렉토리 하위의 AndroidManifest.xml 에 추가해주면 됩니다.
1
|
<uses-permission android:name="android.permission.BLUETOOTH"/>
|
cs |
저는 코드를 살짝 수정하여 AlertDialog에 uuid가 출력되도록 하였습니다.