|
19 | 19 | from adafruit_midi.pitch_bend import PitchBend |
20 | 20 |
|
21 | 21 | import adafruit_ble_midi |
| 22 | +from adafruit_ble_midi import MIDIService |
22 | 23 |
|
23 | 24 | # Use default HID descriptor |
24 | 25 | midi_service = adafruit_ble_midi.MIDIService() |
|
35 | 36 | ble.start_advertising(advertisement) |
36 | 37 |
|
37 | 38 | while True: |
38 | | - print("Waiting for connection") |
39 | | - while not ble.connected: |
40 | | - pass |
41 | | - print("Connected") |
42 | | - while ble.connected: |
43 | | - midi_in = midi.receive() |
44 | | - while midi_in: |
45 | | - if not isinstance(midi_in, MIDIUnknownEvent): |
46 | | - print(time.monotonic(), midi_in) |
47 | | - midi_in = midi.receive() |
48 | | - print("Disconnected") |
49 | | - print() |
50 | | - ble.start_advertising(advertisement) |
| 39 | + print("Waiting for connection to a MIDI device") |
| 40 | + for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=60): |
| 41 | + if MIDIService not in advertisement.services: |
| 42 | + continue |
| 43 | + ble.connect(advertisement) |
| 44 | + break |
| 45 | + |
| 46 | + if ble.connections: |
| 47 | + for connection in ble.connections: |
| 48 | + if connection.connected and not connection.paired: |
| 49 | + print("Connected; Pairing with the MIDI device") |
| 50 | + connection.pair() |
| 51 | + |
| 52 | + if connection.connected and connection.paired: |
| 53 | + print("Paired") |
| 54 | + midi_service = connection[MIDIService] |
| 55 | + midi = adafruit_midi.MIDI(midi_out=midi_service, midi_in=midi_service) |
| 56 | + |
| 57 | + while ble.connected: |
| 58 | + midi_in = midi.receive() |
| 59 | + while midi_in: |
| 60 | + if not isinstance(midi_in, MIDIUnknownEvent): |
| 61 | + print(time.monotonic(), midi_in) |
| 62 | + midi_in = midi.receive() |
| 63 | + print("Disconnected") |
| 64 | + print() |
0 commit comments