diff --git a/examples/MidiTest/Builds/Android/app/src/main/java/com/yourcompany/miditest/MidiTest.java b/examples/MidiTest/Builds/Android/app/src/main/java/com/yourcompany/miditest/MidiTest.java index 8d239045e6..54c10b9ca7 100644 --- a/examples/MidiTest/Builds/Android/app/src/main/java/com/yourcompany/miditest/MidiTest.java +++ b/examples/MidiTest/Builds/Android/app/src/main/java/com/yourcompany/miditest/MidiTest.java @@ -491,12 +491,20 @@ public class MidiTest extends Activity //============================================================================== private class DummyBluetoothGattCallback extends BluetoothGattCallback { - public DummyBluetoothGattCallback() + public DummyBluetoothGattCallback (MidiDeviceManager mm) { super(); + owner = mm; } - public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {} + public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) + { + if (newState == BluetoothProfile.STATE_CONNECTED) + { + gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); + owner.pairBluetoothDeviceStepTwo (gatt.getDevice()); + } + } public void onServicesDiscovered(BluetoothGatt gatt, int status) {} public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {} public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {} @@ -506,6 +514,8 @@ public class MidiTest extends Activity public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {} public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {} public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {} + + private MidiDeviceManager owner; } //============================================================================== @@ -770,17 +780,28 @@ public class MidiTest extends Activity if (getBluetoothDeviceStatus (btAddress) != 0) return false; - BluetoothGatt gatt = btDevice.connectGatt (getApplicationContext(), true, new DummyBluetoothGattCallback()); - if (gatt != null) - gatt.requestConnectionPriority (BluetoothGatt.CONNECTION_PRIORITY_HIGH); - btDevicesPairing.put (btDevice.getAddress(), gatt); - manager.openBluetoothDevice(btDevice, this, null); + btDevicesPairing.put (btDevice.getAddress(), null); + BluetoothGatt gatt = btDevice.connectGatt (getApplicationContext(), true, new DummyBluetoothGattCallback (this)); + + if (gatt != null) + { + btDevicesPairing.put (btDevice.getAddress(), gatt); + } + else + { + pairBluetoothDeviceStepTwo (btDevice); + } } return true; } + public void pairBluetoothDeviceStepTwo (BluetoothDevice btDevice) + { + manager.openBluetoothDevice(btDevice, this, null); + } + public void unpairBluetoothDevice (String address) { if (address.isEmpty()) @@ -936,11 +957,13 @@ public class MidiTest extends Activity MidiDeviceInfo info = theDevice.getInfo(); int deviceID = info.getId(); BluetoothGatt gatt = null; + boolean isBluetooth = false; if (! openTasks.containsKey (deviceID)) { if (info.getType() == MidiDeviceInfo.TYPE_BLUETOOTH) { + isBluetooth = true; BluetoothDevice btDevice = (BluetoothDevice) info.getProperties().get (info.PROPERTY_BLUETOOTH_DEVICE); if (btDevice != null) { @@ -980,7 +1003,7 @@ public class MidiTest extends Activity MidiDeviceOpenTask openTask = new MidiDeviceOpenTask (this, theDevice, gatt); openTasks.put (deviceID, openTask); - new java.util.Timer().schedule (openTask, 3000); + new java.util.Timer().schedule (openTask, (isBluetooth ? 2000 : 100)); } } } diff --git a/modules/juce_core/native/java/AndroidMidi.java b/modules/juce_core/native/java/AndroidMidi.java index 30fe1f89b2..9e337be8f8 100644 --- a/modules/juce_core/native/java/AndroidMidi.java +++ b/modules/juce_core/native/java/AndroidMidi.java @@ -312,12 +312,20 @@ //============================================================================== private class DummyBluetoothGattCallback extends BluetoothGattCallback { - public DummyBluetoothGattCallback() + public DummyBluetoothGattCallback (MidiDeviceManager mm) { super(); + owner = mm; } - public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {} + public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) + { + if (newState == BluetoothProfile.STATE_CONNECTED) + { + gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); + owner.pairBluetoothDeviceStepTwo (gatt.getDevice()); + } + } public void onServicesDiscovered(BluetoothGatt gatt, int status) {} public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {} public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {} @@ -327,6 +335,8 @@ public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {} public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {} public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {} + + private MidiDeviceManager owner; } //============================================================================== @@ -591,17 +601,28 @@ if (getBluetoothDeviceStatus (btAddress) != 0) return false; - BluetoothGatt gatt = btDevice.connectGatt (getApplicationContext(), true, new DummyBluetoothGattCallback()); - if (gatt != null) - gatt.requestConnectionPriority (BluetoothGatt.CONNECTION_PRIORITY_HIGH); - btDevicesPairing.put (btDevice.getAddress(), gatt); - manager.openBluetoothDevice(btDevice, this, null); + btDevicesPairing.put (btDevice.getAddress(), null); + BluetoothGatt gatt = btDevice.connectGatt (getApplicationContext(), true, new DummyBluetoothGattCallback (this)); + + if (gatt != null) + { + btDevicesPairing.put (btDevice.getAddress(), gatt); + } + else + { + pairBluetoothDeviceStepTwo (btDevice); + } } return true; } + public void pairBluetoothDeviceStepTwo (BluetoothDevice btDevice) + { + manager.openBluetoothDevice(btDevice, this, null); + } + public void unpairBluetoothDevice (String address) { if (address.isEmpty()) @@ -757,11 +778,13 @@ MidiDeviceInfo info = theDevice.getInfo(); int deviceID = info.getId(); BluetoothGatt gatt = null; + boolean isBluetooth = false; if (! openTasks.containsKey (deviceID)) { if (info.getType() == MidiDeviceInfo.TYPE_BLUETOOTH) { + isBluetooth = true; BluetoothDevice btDevice = (BluetoothDevice) info.getProperties().get (info.PROPERTY_BLUETOOTH_DEVICE); if (btDevice != null) { @@ -801,7 +824,7 @@ MidiDeviceOpenTask openTask = new MidiDeviceOpenTask (this, theDevice, gatt); openTasks.put (deviceID, openTask); - new java.util.Timer().schedule (openTask, 3000); + new java.util.Timer().schedule (openTask, (isBluetooth ? 2000 : 100)); } } }