chakokuのブログ(rev4)

テック・コミック・ごくまれにチャリ

nimBLEを使って BLE MIDIを実装中

ESP-IDFには2系統のBTライブラリがあるようで、BLEだけなら、NimBLEを使うべしとあった

For BLE-only usecases, using NimBLE is recommended. It is less demanding in terms of code footprint and runtime memory, making it suitable for such scenarios.

確かに、サンプルソースを見ても、NimBLEの方が圧倒的に記述量が少ない。だから、NimBLEでBLE MIDIを試作することにした。
ソース抜粋(Advertisingのテスト中)

BLEサンプル(Heart-rate)をベースに、Serive UUIDとCharacteristic UUIDだけ変えてみる

static const ble_uuid128_t gatt_svr_svc_sec_midi_uuid =
BLE_UUID128_INIT(0x00,0xC7,0xC4,0x4E,0xE3,0x6C,0x51,0xA7,
		 0x33,0x4B,0xE8,0xED,0x5A,0x0E,0xB8,0x03);

static const ble_uuid128_t gatt_svr_chr_midi_uuid = 
BLE_UUID128_INIT(0xF3,0x6B,0x10,0x9D,0x66,0xF2,0xA9,0xA1,
		 0x12,0x41,0x68,0x38,0xDB,0xE5,0x72,0x77);


*略

static const struct ble_gatt_svc_def gatt_svr_svcs[] = {

    {
        /* Service: BLE MIDI */
        .type = BLE_GATT_SVC_TYPE_PRIMARY,
        .uuid = &gatt_svr_svc_sec_midi_uuid.u,
        .characteristics = (struct ble_gatt_chr_def[])
        {
	  {
	    /* Characteristic: BLE MIDI NOTE */
	    .uuid = &gatt_svr_chr_midi_uuid.u,
	    .access_cb = gatt_svr_chr_access_heart_rate,
	    .val_handle = &hrs_hrm_handle,
	    .flags = BLE_GATT_CHR_F_NOTIFY,
	  },
	  {
	    0, /* No more characteristics in this service */
	  },
        }
    },
*略*

ESP32のログ出力

I (831) NimBLE_BLE_MIDI: BLE Host Task Started
Device Address: 30:ae:a4:cc:4d:xx
GAP procedure initiated: advertise; disc_mode=2 adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
BLE_GAP_EVENT_CONNECT
connection established; status=0
BLE_GAP_EVENT_MTU
mtu update event; conn_handle=0 mtu=256
BLE_GAP_EVENT_SUBSCRIBE
subscribe event; cur_notify=0
 value handle; val_handle=12
I (80861) BLE_GAP_SUBSCRIBE_EVENT: conn_handle from subscribe=0
BLE_GAP_EVENT_ENC_CHANGE

ラスパイのBLEツールによるスキャン
BLE MIDIのUUIDである、「03b80e5a-ede8-4b33-a751-6ce34ec4c700」が返却されているようである。。

# bluetoothctl

[bluetooth]# scan on
Discovery started
......
[bluetooth]# connect 30:AE:A4:CC:4D:XX
[BLE MIDI 0.00]# pair 30:AE:A4:CC:4D:XX
Attempting to pair with 30:AE:A4:CC:4D:XX
[CHG] Device 30:AE:A4:CC:4D:XX Paired: yes
Pairing successful
[BLE MIDI 0.00]# info 30:AE:A4:CC:4D:XX
Device 30:AE:A4:CC:4D:XX (public)
        Name: BLE MIDI 0.00
        Alias: BLE MIDI 0.00
        Paired: yes
        Trusted: no
        Blocked: no
        Connected: yes
        LegacyPairing: no
        UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
        UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
        UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
        UUID: Vendor specific           (03b80e5a-ede8-4b33-a751-6ce34ec4c700)

Windows10からESP32のBLE MIDIを接続してみる。以下の通り、BLE_GAP_EVENT_SUBSCRIBEを2回やって、最後はDISCONNECTで終わっている。なぜだろうか。

GAP procedure initiated: advertise; disc_mode=2 adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
BLE_GAP_EVENT_CONNECT
connection established; status=0
BLE_GAP_EVENT_MTU
mtu update event; conn_handle=0 mtu=256
BLE_GAP_EVENT_ENC_CHANGE
BLE_GAP_EVENT_SUBSCRIBE
subscribe event; cur_notify=0
 value handle; val_handle=12
I (1333791) BLE_GAP_SUBSCRIBE_EVENT: conn_handle from subscribe=0
BLE_GAP_EVENT_CONN_UPDATE
BLE_GAP_EVENT_CONN_UPDATE
BLE_GAP_EVENT_SUBSCRIBE
subscribe event; cur_notify=0
 value handle; val_handle=12
I (1338831) BLE_GAP_SUBSCRIBE_EVENT: conn_handle from subscribe=0
BLE_GAP_EVENT_DISCONNECT
disconnect; reason=531

WindowsのBLE管理画面上では接続されていることになっているが、、実際にはdisconnect状態である。

詳細な動作確認はまだできていないが、Advertisingの時に、ServiceUUIDが出せていないように思える。
struct ble_hs_adv_fields に BLE MIDIのServiceUUIDを入れないといけない気がする。

■参考URL
BLE Peripheral Project — Apache Mynewt latest documentation
NimBLE-based host APIs - ESP32 - — ESP-IDF Programming Guide latest documentation
https://sites.google.com/a/gclue.jp/ble-docs/