From 3ef36904c9d4de0d57d714c2ddd7d60e0d13f0b7 Mon Sep 17 00:00:00 2001 From: Kaoru Shoji <0x0badc0de@gmail.com> Date: Fri, 12 Apr 2024 22:16:03 +0900 Subject: [PATCH] Separate initialization of Unity plugin --- .../kshoji/unity/midi/BleMidiUnityPlugin.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/BLE-MIDI-library/src/main/java/jp/kshoji/unity/midi/BleMidiUnityPlugin.java b/BLE-MIDI-library/src/main/java/jp/kshoji/unity/midi/BleMidiUnityPlugin.java index c237881..c8baaf6 100644 --- a/BLE-MIDI-library/src/main/java/jp/kshoji/unity/midi/BleMidiUnityPlugin.java +++ b/BLE-MIDI-library/src/main/java/jp/kshoji/unity/midi/BleMidiUnityPlugin.java @@ -479,6 +479,18 @@ public void initialize(Context context, OnBleMidiDeviceConnectionListener onMidi } public void initialize(Context context) { + try { + initializeCentralProvider(context); + } catch (Exception ignored) { + } + + try { + initializePeripheralProvider(context); + } catch (Exception ignored) { + } + } + + private void initializeCentralProvider(Context context) { bleMidiCentralProvider = new BleMidiCentralProvider(context); bleMidiCentralProvider.setAutoStartInputDevice(true); if (UnityPlayer.currentActivity instanceof BleMidiUnityPlayerActivity) { @@ -528,7 +540,9 @@ public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevic UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceDetached", midiOutputDevice.getDeviceAddress()); } }); + } + private void initializePeripheralProvider(Context context) { bleMidiPeripheralProvider = new BleMidiPeripheralProvider(context); bleMidiPeripheralProvider.setAutoStartDevice(true); if (UnityPlayer.currentActivity instanceof BleMidiUnityPlayerActivity) { @@ -584,14 +598,18 @@ public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevic * @param timeoutInMilliSeconds timeout in msec, 0 : no timeout */ public void startScanDevice(int timeoutInMilliSeconds) { - bleMidiCentralProvider.startScanDevice(timeoutInMilliSeconds); + if (bleMidiCentralProvider != null) { + bleMidiCentralProvider.startScanDevice(timeoutInMilliSeconds); + } } /** * Stops scan device */ public void stopScanDevice() { - bleMidiCentralProvider.stopScanDevice(); + if (bleMidiCentralProvider != null) { + bleMidiCentralProvider.stopScanDevice(); + } } /** @@ -599,7 +617,9 @@ public void stopScanDevice() { */ public void startAdvertising() { - bleMidiPeripheralProvider.startAdvertising(); + if (bleMidiPeripheralProvider != null) { + bleMidiPeripheralProvider.startAdvertising(); + } } /** @@ -607,7 +627,9 @@ public void startAdvertising() */ public void stopAdvertising() { - bleMidiPeripheralProvider.stopAdvertising(); + if (bleMidiPeripheralProvider != null) { + bleMidiPeripheralProvider.stopAdvertising(); + } } /**