반응형

arduino 101의 기본 예제 중 하나인 LED에서 제공하는 Service와  Characteristic에 대한 128-bit UUID를 이용하여  안드로이드에서 통신을 연결하여 LED를 제어하고 현재 LED값을 가져오는 것을 구현하는데 성공었습니다.



[Arduino/Android 101 ( Genuino 101 )] - Android Phone으로 arduino 101에 연결된 LED 제어 및 상태값 읽어오기 ( Bluetooth BLE 프로그래밍 )




아두이노 코드에 적혀있는 UUID를 복사하여..

1
2
3
4
5
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
 
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
 
cs




안드로이드 코드에 그대로 적어주고 Characteristic에서 값을 읽어오거나 Characteristic에 값을 기록하는 것을 구현했었습니다..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "value=",  Toast.LENGTH_LONG);
 
        if (v.getId() == R.id.read_state) {
 
            BluetoothGattService disService = mConnGatt.getService(UUID.fromString("19B10000-E8F2-537E-4F6C-D104768A1214"));
            if (disService == null) {
                Log.d("""Dis service not found!");
                return;
            }
 
 
            BluetoothGattCharacteristic characteristic = disService.getCharacteristic(UUID.fromString("19B10001-E8F2-537E-4F6C-D104768A1214"));
 
            if (characteristic == null) {
                Log.d(""" charateristic not found!");
                return;
            }
 
            boolean result = mConnGatt.readCharacteristic(characteristic);
            if (result == false) {
                Log.d("""reading is failed!");
            }
 
 
 
        } else if (v.getId() == R.id.turn_off_led) {
            BluetoothGattService disService = mConnGatt.getService(UUID.fromString("19B10000-E8F2-537E-4F6C-D104768A1214"));
            if (disService == null) {
                Log.d("""Dis service not found!");
                return;
            }
 
 
            BluetoothGattCharacteristic characteristic = disService.getCharacteristic(UUID.fromString("19B10001-E8F2-537E-4F6C-D104768A1214"));
 
            if (characteristic == null) {
                Log.d("""firmware revison charateristic not found!");
                return;
            }
 
 
            characteristic.setValue(new byte[] { (byte0x00 });
                if (mConnGatt.writeCharacteristic(characteristic)) {
 
                }
 
 
        } else if (v.getId() == R.id.turn_on_led) {
 
            BluetoothGattService disService = mConnGatt.getService(UUID.fromString("19B10000-E8F2-537E-4F6C-D104768A1214"));
            if (disService == null) {
                Log.d("""Dis service not found!");
                return;
            }
 
 
            BluetoothGattCharacteristic characteristic = disService.getCharacteristic(UUID.fromString("19B10001-E8F2-537E-4F6C-D104768A1214"));
 
            if (characteristic == null) {
                Log.d("""charateristic not found!");
                return;
            }
 
 
 
            characteristic.setValue(new byte[] { (byte0x01 });
                if (mConnGatt.writeCharacteristic(characteristic)) {
 
                }
 
        }
    }
cs




이후 다른 예제인 BatteryMonitor를  보니 16진수로 4바이트.. 즉 16-bit UUID만 적혀있습니다..


1
2
3
4
5
6
BLEService batteryService("180F"); // BLE Battery Service
 
// BLE Battery Level Characteristic"
BLEUnsignedCharCharacteristic batteryLevelChar("2A19",  // standard 16-bit characteristic UUID
    BLERead | BLENotify);     // remote clients will be able to
// get notifications if this characteristic changes
cs



이 값만 복사하여 안드로이드에서 해보면 잘못된 UUID라고 나오는 거봐서는 128비트 UUID로 바꾸어 주어야 하는 듯 싶어 자료를 검색해보고 방법을 찾았습니다.


https://farwestab.wordpress.com/2011/02/05/some-tips-on-android-and-bluetooth/



The standard UUID for the Serial Port Profile is 00001101-0000-1000-8000-00805F9B34FB. When you specify the 16-bit UUID for SPP, then you specify 1101. Notice how that sequence appears in the first part of the full 128-bit UUID? Well, if you put your own 4 hex digits in place of 1101, it appears that a device that only supports 16-bit UUID’s will find the service (it looks like Android up-converts the 16-bit UUID to a 128-bit UUID and assumes the rest is part of the standard Bluetooth UUID form). So, if you wanted to use 9999 as your UUID on the 16-bit only device, then you would use 00009999-0000-1000-8000-00805F9B34FB on the Android side.



위 아두이노 코드에서 16비트 UUID는 180F이므로.. 이 값을  128비트 비트로 바꾸어 주면  0000180F-0000-1000-8000-00805F9B34FB이 됩니다.. 안드로이드 코드에 적용해서 해보니 값이 읽혀집니다..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        if (v.getId() == R.id.read_state) {
 
            BluetoothGattService disService = mConnGatt.getService(UUID.fromString("0000180F-0000-1000-8000-00805F9B34FB"));
            if (disService == null) {
                Log.d("""Dis service not found!");
                return;
            }
 
 
            BluetoothGattCharacteristic characteristic = disService.getCharacteristic(UUID.fromString("00002A19-0000-1000-8000-00805F9B34FB"));
 
            if (characteristic == null) {
                Log.d(""" charateristic not found!");
                return;
            }
 
            boolean result = mConnGatt.readCharacteristic(characteristic);
            if (result == false) {
                Log.d("""reading is failed!");
            }
 
 
 
        } 
cs




Playstore에 있는 nRF Master Control앱처럼 자동으로 UUID를 바꾸어 주는것을 알아봤는데.. 라이브러리를 이용하면 가능하던군요... 그래도 당분간은 수작업으로 적어주려합니다.. 



이제 LED예제만 해보는게 아니라 다른 센서나 모터 같은 것들도 제어해봐야 겠습니다..



반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts