Skip to content

Commit

Permalink
Separate initialization of Unity plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kshoji committed Apr 12, 2024
1 parent 102c252 commit 3ef3690
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -584,30 +598,38 @@ 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();
}
}

/**
* Starts advertising
*/
public void startAdvertising()
{
bleMidiPeripheralProvider.startAdvertising();
if (bleMidiPeripheralProvider != null) {
bleMidiPeripheralProvider.startAdvertising();
}
}

/**
* Stops advertising
*/
public void stopAdvertising()
{
bleMidiPeripheralProvider.stopAdvertising();
if (bleMidiPeripheralProvider != null) {
bleMidiPeripheralProvider.stopAdvertising();
}
}

/**
Expand Down

0 comments on commit 3ef3690

Please sign in to comment.