How to make BLE-midi bidirectional?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

I copied the code from the midi_server example over to a new MTW project and I can send Midi notes to iOS apps (Garageband, MidiWrench). However the server does not seem to receive any Midi events from the phone. I added LEGATTDB_CHAR_PROP_WRITE_NO_RESPONSE to the GATT db in the HANDLE_HSENS_SERVICE_CHAR_NOTIFY characteristic entry, and now the iOS apps do see that the device is capable of Input as well as Output.

I expected that when the client sends a Midi note or CC that the callback I registered (midi_server_gatts_callback) would get called but that does not seem to happen. Am I missing some obvious step?

Unfortunately, the MTW documentation is pretty light on tutorials and overviews 😞

Thanks, Norm

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.

Please try to change the permission of the midi character as below:

LEGATTDB_PERM_READABLE | LEGATTDB_PERM_WRITE_REQ | LEGATTDB_PERM_AUTH_READABLE | LEGATTDB_PERM_AUTH_WRITABLE

It can successfully bond per my test. I also attach the code for you.

View solution in original post

0 Likes
46 Replies
AnjanaM_61
Moderator
Moderator
Moderator
5 comments on KBA First comment on KBA 5 questions asked

Hello @ncampbell 

Can you please let us know which Bluetooth chip are you using?

Regards,

Anjana

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

The 20719 (on a CYW920719B2Q40EVB eval board). Thanks, Norm

 

0 Likes
AnjanaM_61
Moderator
Moderator
Moderator
5 comments on KBA First comment on KBA 5 questions asked

hI @ncampbell 

Can you please share the modified project & steps to reproduce the issue. 

Regards,

Anjana

0 Likes
lock attach
Attachments are accessible only for community members.
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Project is attached. If you build and run it as is, GarageBand can connect to the Midi device ("MIDI7P") and that more or less works. But it doesn't attempt to pair. If you remove | LEGATTDB_PERM_WRITE_CMD on line 192 of the C file then GarageBand attempts to pair. Sometimes the pairing succeeds, but mostly it times out (and the log reports an error 22). Thanks, Norm

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Any progress on this?

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Is this ticket still active? I've heard no response for 3 weeks.

 

0 Likes

Very sorry for the late reply.

I am not sure how to send MIDI note through the Garageband. I tried to write to the GATT with the CYSmart on the iphone and I can see the GATT log successfully. Please check the log below.

Owen_Zhang123_0-1658289363886.png

 

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Thanks, Owen. But this doesn't address my current problem. As I said above "GarageBand can connect to the Midi device and that more or less works." Then I went on to describe the current problem:

"it doesn't attempt to pair. If you remove | LEGATTDB_PERM_WRITE_CMD on line 192 of the C file then GarageBand attempts to pair. Sometimes the pairing succeeds, but mostly it times out (and the log reports an error 22)."

Can you help with this? Thanks, Norm

PS. if you want to get GarageBand to send a Midi note just tap on a key on the piano keyboard, while the BLE-Midi device is connected.

0 Likes

I tested the problem as you said for several times. Each time, the phone can successfully pair with the device. I can't reproduce the problem on my side.

I am testing with iphoneSE2 and iphone13.

The SDK is Modus 2.4 and btsdk 3.3. The test board is CYW920719B2Q40EVB-01.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

I'm using the same test board, and Modus 2.4. Not sure what version of btsdk I have - how do I tell? You're saying that GarageBand on the iPhone paired with my firmware without any mods required? Can you send me the log so maybe I can tell where it started to go wrong? Any idea why it pairs reliably for you but not for me? Thanks, Norm

0 Likes

Yes, I can pair with the firmware you shared without any modification. You can check the BTSDK version in the library manager. I will share the log with you later.

0 Likes
lock attach
Attachments are accessible only for community members.

Attach the log during the connection and pairing. I did the pair twice.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Thanks for the log. Yours is very different from my log. The first difference is:

yours: local keys read from NVRAM result: 0

mine: local keys read from NVRAM result: 40

But the documentation for wiced_hal_read_nvram gives no indication of what the status (0 or 40) even means. How are we supposed to develop applications without such basic info and without the source code?

I have verified that I am using BTSDK 3.3. So: we are using the same board, the same code, the same BTSDK, and nearly the same iPhone and iOS app, yet it works for you and not for me.

Now how do I get mine to work? It's not enough for you to simply say "it works for me".

Thanks, Norm

 

0 Likes
Owen_Zhang123
Moderator
Moderator
Moderator
5 questions asked 500 solutions authored 250 sign-ins

The meaning for the status is defined in WICED_RESULT_LIST. 0 means success and 40 (0x28) means error.

As I can't reproduce the problem on my side, could you catch the OTA logs during the connection process to check the problem?

0 Likes
lock attach
Attachments are accessible only for community members.
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Here's the serial port log - is that what you mean by "OTA log"? In this case GarageBand connected but did not attempt to pair. Other times I have tried this, GarageBand does prompt me to pair, but then the pairing times out (error code 22).

Thanks, Norm

0 Likes

The OTA log means over the air log. You need a equipment such as ellisys or frontline to catch it. The log will show the packets in the air during the bluetooth communication.

If you don't have the equipment, we can check the application log first. Before you initiate the connection, did you check the bluetooth device in the phone system page and ignore the device which is in the pairing list?

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

I don't have an OTA analyzer and they look pretty expensive. You have the application log from my previous response. Before I initiate the connection, the iPhone's Settings-Bluetooth shows no midi device in the list under "My devices". When I connect in Garageband then the device "MIDI" (not MIDI7P which is my device's name) shows up in the list. You said "pairing list" but do you mean the list under "My Devices"? (I ask because the midi device is only connected, not paired, yet it shows up in that list). Thanks, Norm

0 Likes
lock attach
Attachments are accessible only for community members.

The application log shows little information for the debug. Please route the log to btspy to get more information from the stack.

To enable the btspy log, you need enable the HCI trace in makefile and route the log to wiced uart in the application.

C_FLAGS += -DENABLE_HCI_TRACE

wiced_set_debug_uart( WICED_ROUTE_DEBUG_TO_WICED_UART );

You can refer to chapter 4.4.3 in the following guide: https://www.infineon.com/dgdl/Infineon-CYW920706WCDEVAL_Evaluation_Kit_Guide-UserManual-v01_00-EN.pd...

I also attached the code, makefile and the btspy log on my side for you.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Hi Owen - I added the C_FLAGS line to the makefile and rebuilt. The line wiced_set_debug_uart( WICED_ROUTE_DEBUG_TO_WICED_UART ); was already in the main C file (enabled by #ifdef NO_PUART_SUPPORT). I downloaded to my board, started ClientControl, set the baud rate to 3000000, started BTSpy, then opened COM6 (WICED_HCI_UART) in ClientControl. As soon as I opened the port, pressing reset (SW2) no longer displayed anything in the application log in Teraterm, and BTSpy displayed nothing other than the startup messages:

Spy instance 0, listening on port 9876

Opened \\.\COM6 at speed: 3000000 flow on

Startup

Set discoverable:1, connectable:1

Set Pairable:1

 

Is there any board configuration I need to do? Any idea why this does not work here?

Thanks, Norm

 

 

0 Likes

Did you try the code I sent you in last response?

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Yes I did. With your code downloaded, Teraterm displays no text at all, even after pressing the Reset button. And BTSpy shows only the lines I mentioned in my last message.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

HI Owen -

I got ClientControl/BTSpy to work finally, but there's a lot of things you didn't tell me:
- Teraterm no longer works to display the app log or to send keypresses to the server. I assume this is because WICED_ROUTE_DEBUG_TO_WICED_UART stops the app log from being displayed on the Peripheral UART?
- the Reset button on the board does nothing as far as I can tell. It worked OK before these latest changes to my program
- hitting Open Port in ClientControl does work (BTSpy shows lots of messages) but it took me a long time to realize this because it displays nothing until some BT event comes along, which can take a long time if I'm not actually trying to do stuff in GarageBand. Pressing Reset shows nothing.
- you removed the LEGATTDB_PERM_WRITE_CMD that I added in the GATT, with no explanation. I believe I need that in order that the client (eg. GarageBand) can send midi notes to the server. With that permission removed, I do get prompted in GB to pair. If I have to leave it removed, how can Midi notes get received and stored in the "database"?
- the BLEMidi example gets keys typed via Teraterm and turns those into Midi notes which it sends to GB. Without teraterm working, how do I test this now?
- should I restore my LEGATTDB_PERM_WRITE_CMD and send you the BTSpy log, since that's the case in which the pairing does not work?

Thanks, Norm

0 Likes
lock attach
Attachments are accessible only for community members.
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

In an attempt to simplify things I stopped using Garageband and switched to LightBlue (free from PunchThrough.com). I also removed the additions I made to the GATT DB (write properties and permissions). I've attached the current source file, it's slightly different from what you have. Pairing still fails. I've attached a BTSpy log. After log line 425 I hit Confirm on the pairing dialog in LightBlue. Public keys then get exchanged etc. Please advise what I need to do to get reliable pairing. Thx.

0 Likes
lock attach
Attachments are accessible only for community members.
Owen_Zhang123
Moderator
Moderator
Moderator
5 questions asked 500 solutions authored 250 sign-ins

@ncampbell wrote:

Project is attached. If you build and run it as is, GarageBand can connect to the Midi device ("MIDI7P") and that more or less works. But it doesn't attempt to pair. If you remove | LEGATTDB_PERM_WRITE_CMD on line 192 of the C file then GarageBand attempts to pair. Sometimes the pairing succeeds, but mostly it times out (and the log reports an error 22). Thanks, Norm


Could you try to initiate the pairing process on the CYW20719 side. I add the LEGATTDB_PERM_WRITE_CMD. Then initiate the pairing with wiced_bt_dev_sec_bond in the connection up event. Seems it can bond successfully. Please try it.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Still doesn't work. About 20 seconds after I hit the OK button in Garageband's Pairing dialog box I get an error in the application log and Garageband says my device is not connected. Here's the whole application log:

wiced_bt_start_advertisements (undir low): 0
midi_server_conn_up 4B A8 22 B3 04 72 id:1
:midi_server_management_cback: 0x17
Advertisement State Change: 0
Advert stopped
Stopping Advertisements0
NVRAM write 9 bytes
midi_server_management_cback: 0xA
Pairing IO Cap Request
midi_server_management_cback: 0x17
Advertisement State Change: 0
Advert stopped
midi_server_management_cback: 0x20
midi_server_gatts_req_cb: conn_id 1, type MTU
req_mtu: 517
midi_server_gatts_req_cb: conn_id 1, type READ
get_attr Handle: 0x16
get_attr Handle: 0x18
get_attr Handle: 0x2A
read_hndlr conn_id:1 hdl: 0x2A offset:0 len:5
midi_server_management_cback: 0x13
keys read from NVRAM 4B A8 22 B3 04 72 result: 40
midi_server_management_cback: 0x22
midi_server_gatts_req_cb: conn_id 1, type READ
get_attr Handle: 0x16
get_attr Handle: 0x18
get_attr Handle: 0x2A
get_attr Handle: 0x2B
get_attr Handle: 0x2D
get_attr Handle: 0x42
read_hndlr conn_id:1 hdl: 0x42 offset:0 len:13
midi_server_gatts_req_cb: conn_id 1, type READ
get_attr Handle: 0x16
get_attr Handle: 0x18
get_attr Handle: 0x2A
get_attr Handle: 0x2B
get_attr Handle: 0x2D
get_attr Handle: 0x42
get_attr Handle: 0x44
read_hndlr conn_id:1 hdl: 0x44 offset:0 len:8
midi_server_management_cback: 0x1F
midi_server_gatts_req_cb: conn_id 1, type READ
get_attr Handle: 0x16
read_hndlr conn_id:1 hdl: 0x16 offset:0 len:9
midi_server_management_cback: 0x12
keys save to NVRAM 4B A8 22 B3 04 72 result: 0
midi_server_management_cback: 0x12
keys save to NVRAM 4B A8 22 B3 04 72 result: 0
midi_server_management_cback: 0x12
keys save to NVRAM 4B A8 22 B3 04 72 result: 0
midi_server_management_cback: 0xC
Encryption Status Event: bd ( 4B A8 22 B3 04 72 ) res 0
encryp change BT dev addr 4B A8 22 B3 04 72 res: 0
midi_server_gatts_req_cb: conn_id 1, type READ
get_attr Handle: 0x16
read_hndlr conn_id:1 hdl: 0x16 offset:0 len:9
midi_server_management_cback: 0xB
Pairing Complete: 38 2
midi_server, bond result: 38

Sometimes the status at the end is 22, not 38.

Thx, Norm

 

 

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

I bought another eval kit. Loaded the same code on it. Got the same result. I hope you have some ideas. Thx, Norm

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Can you send me your built application executable? I don't know exactly what this is, as I just use the BLE Midi Program entry under Launches in the Quick Panel. It looks like my executable is build\CYW920719B2Q40EVB-01\Debug\EmptyWicedBluetooth.elf. If you send me yours and I just replace mine with it, will the launch work? Thx.

0 Likes
lock attach
Attachments are accessible only for community members.

Could you try the attached .hex file on your side. You may program your device with cypress programmer.

https://softwaretools.infineon.com/tools/com.ifx.tb.tool.cypressprogrammer

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Thanks, I got that file burned into one of my boards. The first time I tried to pair in Garageband it worked. Then I disconnected and tried again and it failed after about 20 seconds (Pairing Complete 22 2 again), and the device showed as "Not Connected" in GarageBand. Same for every subsequent attempt.

I have tried this on two evaluation boards, using two iPhones, with 2 applications (GarageBand and LightBlue) and now with your build and my build, and I cannot get reliable pairing. Are you sure you are getting reliable pairing, multiple times? Thx, Norm

0 Likes

The problem is because the device is already bonded. Then if you initiate the secure bond process again, the phone will not respond and it will disconnect.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Hi Owen - any progress on this?

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Hi Owen - we seem to not be communicating very well. Let me try to lay this out clearly:

(First off - I'm using the executable you sent me Aug 29, and GarageBand (GB) running on an iPhone).

I'm not super familiar with BLE, obviously, but here's my understanding of what should happen, please correct me if I'm wrong:

- first time, my device will show up in the list of BLE-Midi devices and if I click on it a popup will say "Pairing Request"
- if I click on Pair, GB will pair with the eval board and they will both store their keys
- after a disconnection or power cycle, GB will again show my device in the list of BLE-Midi devices but it will connect if I click it,  without a "Pair?" prompt. It will then show as "Connected" and be useable
- after clicking on the device in GB and hitting "Forget", behaviour will begin at the 1st step again

But here's what actually happens:

- on phone, turn on BT
- in GB click on MIDI7P, I see Bluetooth Pairing Request, hit Pair
- GB shows Connected
- in GB, disconnect, MIDI7P shows as Not connected
- hit MIDI7P, shows as Connected
- in GB, disconnect, MIDI7P shows as Not connected
- hit MIDI7P, shows as Connected
- 28 seconds later GB shows it as Offline then Not Connected
- a few lines up in the log is "Pairing Complete 22 2"
- hit MIDI7P, shows as Connected
- 28 seconds later GB shows it as Offline then Not Connected
- a few lines up in the log is "Pairing Complete 22 2"

Clearly the connection is being made without pairing, SOMETIMES, NOT ALWAYS. More than half the time the pairing times out after 28 seconds and that log message is shown, and GarageBand is Not Connected to the device. Why is this and how do I fix it?

Thanks

Norm

 

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Can I get some help please?

0 Likes
lock attach
Attachments are accessible only for community members.
Owen_Zhang123
Moderator
Moderator
Moderator
5 questions asked 500 solutions authored 250 sign-ins

Hi Norm,

Very sorry for the long delay about this problem.

Let's sync up the problem first. 

I tested the problem again. I comment the LEGATTDB_PERM_WRITE_CMD on your code. I also make some change about the bd addr for OTA capture.

1. Connect the device from GB and it can successfully connect and pair.

2. Hit the MIDI on GB, it will show disconnect. But on the bluetooth list, it is still in connection state.

3. Hit the MIDI on GB again, it will show connect. There will not be pair process because the phone system bluetooth doesn't change.

4. On the bluetooth setting page of the phone, click the "forget this device". The phone will disconnect with the MIDI.

5. On BG, find the MIDI and hit it again, the device will connect and pair as step 1.

 

I think this is what you expect. I also tested it at the beginning.

Now I share my code and logs with you agian, please try to do the same test and see what happened on your side.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

Thanks Owen. I've grabbed the code, will rebuild it and test here.

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

That seems to have fixed the problem! Pairing, bonding, connect and disconnect all work as I expected. One final (I hope) question - you added a fair amount of code here, and it's not obvious to me what the extra API calls are doing and why they are needed - is there some documentation or example application that I could have gotten all this from? Thanks, Norm

0 Likes

You can find some simple description in the definition of the API. You can also find the information in the API description document below:

https://infineon.github.io/btsdk-docs/BT-SDK/20719-B2_Bluetooth/API/group__wicedbt.html

0 Likes
ncampbell
Level 3
Level 3
First like given 25 sign-ins 25 replies posted

We're not out of the woods yet. Remember the original problem in this issue (BLE-Midi needs to be bidirectional)? That still doesn't work. and here's why: You removed the LEGATTDB_PERM_WRITE_CMD that I added to the main Midi characteristic in the midi_server_gatt_database, and with that removed pairing and bonding are reliable. But with that WRITE_CMD removed I cannot send Midi notes to the server, which makes sense (the Midi server is required to support Write Without Response, and without that permission it cannot do that). But if I add that permission back in, pairing is no longer reliable. Specifically: on the first connection my phone prompts me to Pair and that works OK. But then if I disconnect, nothing happens on the board (the log shows no activity). And if I hit Forget on my phone and then try to connect again, I am NOT prompted to pair - instead the device just shows as Connected.

This behaviour is wrong, correct? And I need LEGATTDB_PERM_WRITE_CMD, correct?

FYI I am using the MidiWrench iOS app as it's easier to see notes coming in and going out with.

0 Likes
lock attach
Attachments are accessible only for community members.

Please try to change the permission of the midi character as below:

LEGATTDB_PERM_READABLE | LEGATTDB_PERM_WRITE_REQ | LEGATTDB_PERM_AUTH_READABLE | LEGATTDB_PERM_AUTH_WRITABLE

It can successfully bond per my test. I also attach the code for you.

0 Likes