Skip to content

Commit

Permalink
Merge pull request #31 from kshoji/feature/fix-deprecated-apis-20240412
Browse files Browse the repository at this point in the history
Change deprecated apis to latest apis
  • Loading branch information
kshoji authored Apr 12, 2024
2 parents 2d1983b + 0a75526 commit 102c252
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 46 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: set up JDK 17
uses: actions/setup-java@v3
- uses: actions/checkout@v4
- name: set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '17'
java-version: '21'
distribution: 'temurin'
cache: gradle
cache: 'gradle'

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
run: ./gradlew build
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,25 @@ public void run() {
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
public void onCharacteristicChanged(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Set<MidiInputDevice> midiInputDevices = midiInputDevicesMap.get(gatt.getDevice().getAddress());
if (midiInputDevices != null) {
for (MidiInputDevice midiInputDevice : midiInputDevices) {
((InternalMidiInputDevice) midiInputDevice).incomingData(value);
}
}
}
}

Set<MidiInputDevice> midiInputDevices = midiInputDevicesMap.get(gatt.getDevice().getAddress());
if (midiInputDevices != null) {
for (MidiInputDevice midiInputDevice : midiInputDevices) {
((InternalMidiInputDevice)midiInputDevice).incomingData(characteristic.getValue());
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
Set<MidiInputDevice> midiInputDevices = midiInputDevicesMap.get(gatt.getDevice().getAddress());
if (midiInputDevices != null) {
for (MidiInputDevice midiInputDevice : midiInputDevices) {
((InternalMidiInputDevice) midiInputDevice).incomingData(characteristic.getValue());
}
}
}
}
Expand Down Expand Up @@ -706,8 +718,12 @@ public void configureAsCentralDevice() throws SecurityException {
List<BluetoothGattDescriptor> descriptors = midiInputCharacteristic.getDescriptors();
for (BluetoothGattDescriptor descriptor : descriptors) {
if (BleUuidUtils.matches(BleUuidUtils.fromShortValue(0x2902), descriptor.getUuid())) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
bluetoothGatt.writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;

import jp.kshoji.blemidi.device.MidiInputDevice;
import jp.kshoji.blemidi.device.MidiOutputDevice;
Expand Down Expand Up @@ -109,21 +110,24 @@ public BleMidiCentralProvider(@NonNull final Context context) throws Unsupported
throw new UnsupportedOperationException("Bluetooth LE not supported on this device.");
}

try {
// Checks `android.software.companion_device_setup` feature specified at AndroidManifest.xml
FeatureInfo[] reqFeatures = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS).reqFeatures;
if (reqFeatures != null) {
for (FeatureInfo feature : reqFeatures) {
if (feature == null) {
continue;
}
if (PackageManager.FEATURE_COMPANION_DEVICE_SETUP.equals(feature.name)) {
useCompanionDeviceSetup = true;
break;
// if the context is not Activity, it can't use CompanionDeviceManager
if (context instanceof Activity) {
try {
// Checks `android.software.companion_device_setup` feature specified at AndroidManifest.xml
FeatureInfo[] reqFeatures = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS).reqFeatures;
if (reqFeatures != null) {
for (FeatureInfo feature : reqFeatures) {
if (feature == null) {
continue;
}
if (PackageManager.FEATURE_COMPANION_DEVICE_SETUP.equals(feature.name)) {
useCompanionDeviceSetup = true;
break;
}
}
}
} catch (PackageManager.NameNotFoundException ignored) {
}
} catch (PackageManager.NameNotFoundException ignored) {
}

bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
Expand Down Expand Up @@ -219,24 +223,49 @@ public void startScanDevice(int timeoutInMilliSeconds) throws SecurityException
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && useCompanionDeviceSetup) {
final CompanionDeviceManager deviceManager = context.getSystemService(CompanionDeviceManager.class);
final AssociationRequest associationRequest = BleMidiDeviceUtils.getBleMidiAssociationRequest(context);
// TODO: use another associate API when SDK_INT >= VERSION_CODES.TIRAMISU
try {
deviceManager.associate(associationRequest,
new CompanionDeviceManager.Callback() {
@Override
public void onDeviceFound(final IntentSender intentSender) {
try {
((Activity) context).startIntentSenderForResult(intentSender, SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
}
final CompanionDeviceManager.Callback associationCallback = new CompanionDeviceManager.Callback() {
@Override
public void onAssociationPending(@NonNull IntentSender intentSender) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
try {
((Activity) context).startIntentSenderForResult(intentSender, SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
} else {
// calls onDeviceFound
super.onAssociationPending(intentSender);
}
}

@Override
public void onFailure(final CharSequence error) {
Log.e(Constants.TAG, "onFailure error: " + error);
}
}, null);
@Override
public void onDeviceFound(final IntentSender intentSender) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
try {
((Activity) context).startIntentSenderForResult(intentSender, SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, e.getMessage(), e);
}
}
}

@Override
public void onFailure(final CharSequence error) {
Log.e(Constants.TAG, "onFailure error: " + error);
}
};

try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
deviceManager.associate(associationRequest, new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, associationCallback);
} else {
deviceManager.associate(associationRequest, associationCallback, null);
}
} catch (IllegalStateException ignored) {
Log.e(Constants.TAG, ignored.getMessage(), ignored);
// Must declare uses-feature android.software.companion_device_setup in manifest to use this API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static boolean isBluetoothEnabled(@NonNull final Context context) {
/**
* Request code for BLE MIDI device selection
*/
public static final int SELECT_DEVICE_REQUEST_CODE = 0x5e1ec7;
public static final int SELECT_DEVICE_REQUEST_CODE = 0x5e1e;

/**
* Enables bluetooth function.<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.app.Activity;
import android.bluetooth.le.ScanResult;
import android.companion.AssociationInfo;
import android.companion.CompanionDeviceManager;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.unity3d.player.UnityPlayerActivity;
Expand All @@ -22,7 +24,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == BleUtils.SELECT_DEVICE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
try {
AssociationInfo associationInfo = data.getParcelableExtra(CompanionDeviceManager.EXTRA_ASSOCIATION, AssociationInfo.class);
bleMidiCentralProvider.connectGatt(associationInfo.getAssociatedDevice().getBleDevice().getDevice());
} catch (Throwable t) {
Log.d(Constants.TAG, t.getMessage(), t);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
ScanResult scanResult = data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
bleMidiCentralProvider.connectGatt(scanResult.getDevice());
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.3.1'
classpath 'com.android.tools.build:gradle:8.3.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 102c252

Please sign in to comment.