Browse Source

Fixed a bug where the bluetooth LE scanner would continue to scan even if the bluetooth selector window was closed

tags/2021-05-28
hogliux 9 years ago
parent
commit
ebbba641a4
3 changed files with 89 additions and 33 deletions
  1. +18
    -2
      modules/juce_audio_utils/native/juce_android_BluetoothMidiDevicePairingDialogue.cpp
  2. +67
    -31
      modules/juce_core/native/java/AndroidMidi.java
  3. +4
    -0
      modules/juce_core/native/java/AndroidMidiFallback.java

+ 18
- 2
modules/juce_audio_utils/native/juce_android_BluetoothMidiDevicePairingDialogue.cpp View File

@@ -28,7 +28,8 @@
METHOD (pairBluetoothMidiDevice, "pairBluetoothMidiDevice", "(Ljava/lang/String;)Z") \
METHOD (unpairBluetoothMidiDevice, "unpairBluetoothMidiDevice", "(Ljava/lang/String;)V") \
METHOD (getHumanReadableStringForBluetoothAddress, "getHumanReadableStringForBluetoothAddress", "(Ljava/lang/String;)Ljava/lang/String;") \
METHOD (isBluetoothDevicePaired, "isBluetoothDevicePaired", "(Ljava/lang/String;)Z")
METHOD (isBluetoothDevicePaired, "isBluetoothDevicePaired", "(Ljava/lang/String;)Z") \
METHOD (startStopScan, "startStopScan", "(Z)V")
DECLARE_JNI_CLASS (AndroidBluetoothManager, JUCE_ANDROID_ACTIVITY_CLASSPATH "$BluetoothManager");
#undef JNI_CLASS_MEMBERS
@@ -36,6 +37,15 @@ DECLARE_JNI_CLASS (AndroidBluetoothManager, JUCE_ANDROID_ACTIVITY_CLASSPATH "$Bl
//==============================================================================
struct AndroidBluetoothMidiInterface
{
static void startStopScan (bool startScanning)
{
JNIEnv* env = getEnv();
LocalRef<jobject> btManager (android.activity.callObjectMethod (JuceAppActivity.getAndroidBluetoothManager));
if (btManager.get() != nullptr)
env->CallVoidMethod (btManager.get(), AndroidBluetoothManager.startStopScan, (jboolean) (startScanning ? 1 : 0));
}
static StringArray getBluetoothMidiDevicesNearby()
{
StringArray retval;
@@ -172,7 +182,6 @@ public:
setRowHeight (40);
setModel (this);
setOutlineThickness (1);
updateDeviceList();
startTimer (timerPeriodInMs);
}
@@ -365,6 +374,8 @@ public:
{
ScopedPointer<ModalComponentManager::Callback> exitCallback (exitCallbackToUse);
AndroidBluetoothMidiInterface::startStopScan (true);
setAlwaysOnTop (true);
setVisible (true);
addToDesktop (ComponentPeer::windowHasDropShadow);
@@ -375,6 +386,11 @@ public:
enterModalState (true, exitCallback.release(), true);
}
~BluetoothMidiSelectorOverlay()
{
AndroidBluetoothMidiInterface::startStopScan (false);
}
void paint (Graphics& g) override
{
g.fillAll (Colours::black.withAlpha (0.6f));


+ 67
- 31
modules/juce_core/native/java/AndroidMidi.java View File

@@ -3,14 +3,26 @@
{
BluetoothManager()
{
ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
scanFilterBuilder.setServiceUuid (ParcelUuid.fromString (bluetoothLEMidiServiceUUID));
}
ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
scanSettingsBuilder.setCallbackType (ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setScanMode (ScanSettings.SCAN_MODE_LOW_POWER)
.setScanMode (ScanSettings.MATCH_MODE_STICKY);
public String[] getMidiBluetoothAddresses()
{
return bluetoothMidiDevices.toArray (new String[bluetoothMidiDevices.size()]);
}
public String getHumanReadableStringForBluetoothAddress (String address)
{
BluetoothDevice btDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice (address);
return btDevice.getName();
}
public boolean isBluetoothDevicePaired (String address)
{
return getAndroidMidiDeviceManager().isBluetoothDevicePaired (address);
}
public void startStopScan (boolean shouldStart)
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null)
@@ -27,25 +39,24 @@
return;
}
bluetoothLeScanner.startScan (Arrays.asList (scanFilterBuilder.build()),
scanSettingsBuilder.build(),
this);
}
public String[] getMidiBluetoothAddresses()
{
return bluetoothMidiDevices.toArray (new String[bluetoothMidiDevices.size()]);
}
if (shouldStart)
{
ScanFilter.Builder scanFilterBuilder = new ScanFilter.Builder();
scanFilterBuilder.setServiceUuid (ParcelUuid.fromString (bluetoothLEMidiServiceUUID));
public String getHumanReadableStringForBluetoothAddress (String address)
{
BluetoothDevice btDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice (address);
return btDevice.getName();
}
ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
scanSettingsBuilder.setCallbackType (ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setScanMode (ScanSettings.SCAN_MODE_LOW_POWER)
.setScanMode (ScanSettings.MATCH_MODE_STICKY);
public boolean isBluetoothDevicePaired (String address)
{
return getAndroidMidiDeviceManager().isBluetoothDevicePaired (address);
bluetoothLeScanner.startScan (Arrays.asList (scanFilterBuilder.build()),
scanSettingsBuilder.build(),
this);
}
else
{
bluetoothLeScanner.stopScan (this);
}
}
public boolean pairBluetoothMidiDevice(String address)
@@ -406,27 +417,42 @@
return info.getPorts();
}
public String getHumanReadableNameForPort (MidiDeviceInfo.PortInfo port, int portIndexToUseInName)
public String getHumanReadableNameForPort (MidiDeviceInfo.PortInfo port, int portIndexToUseInName, boolean addPortNumberToName)
{
String portName = port.getName();
if (addPortNumberToName)
{
String portName = port.getName();
if (portName.equals (""))
portName = ((port.getType() == MidiDeviceInfo.PortInfo.TYPE_OUTPUT) ? "Out " : "In ")
+ Integer.toString (portIndexToUseInName);
if (portName.equals(""))
portName = ((port.getType() == MidiDeviceInfo.PortInfo.TYPE_OUTPUT) ? "Out " : "In ")
+ Integer.toString(portIndexToUseInName);
return getHumanReadableDeviceName() + " " + portName;
return getHumanReadableDeviceName() + " " + portName;
}
return getHumanReadableDeviceName();
}
public String getHumanReadableNameForPort (int portType, int androidPortID, int portIndexToUseInName)
{
MidiDeviceInfo.PortInfo[] ports = info.getPorts();
int numTotalPorts = 0;
for (MidiDeviceInfo.PortInfo port : ports)
{
if (port.getType() == portType)
{
numTotalPorts++;
}
}
for (MidiDeviceInfo.PortInfo port : ports)
{
if (port.getType() == portType)
{
if (port.getPortNumber() == androidPortID)
return getHumanReadableNameForPort (port, portIndexToUseInName);
return getHumanReadableNameForPort (port, portIndexToUseInName, (numTotalPorts > 1));
}
}
@@ -689,6 +715,15 @@
int portIdx = 0;
MidiDeviceInfo.PortInfo[] ports = physicalMidiDevice.getPorts();
int numberOfPorts = 0;
for (MidiDeviceInfo.PortInfo port : ports)
{
if (port.getType() == portType)
{
numberOfPorts++;
}
}
for (MidiDeviceInfo.PortInfo port : ports)
{
if (port.getType() == portType)
@@ -700,7 +735,8 @@
listOfReturnedDevices.add (path);
deviceNames.add (physicalMidiDevice.getHumanReadableNameForPort (port,
path.portIndexToUseInName));
path.portIndexToUseInName,
(numberOfPorts > 1)));
}
}
}


+ 4
- 0
modules/juce_core/native/java/AndroidMidiFallback.java View File

@@ -21,6 +21,10 @@
return false;
}
public void startStopScan (boolean shouldStart)
{
}
public boolean pairBluetoothMidiDevice(String address)
{
return false;


Loading…
Cancel
Save