Skip to content

Commit

Permalink
Merge branch 'release/v0.0.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
kshoji committed Aug 17, 2022
2 parents 503b28e + 94f3001 commit ddfa381
Show file tree
Hide file tree
Showing 29 changed files with 630 additions and 199 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Android CI

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]

jobs:
build:

runs-on: ubuntu-latest

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

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
5 changes: 3 additions & 2 deletions BLE-MIDI-library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 29
compileSdkVersion 32

defaultConfig {
minSdkVersion 18
targetSdkVersion 32
}

compileOptions {
Expand Down Expand Up @@ -39,7 +40,7 @@ group = 'jp.kshoji'
uploadArchives {
repositories.mavenDeployer {
repository url: 'file://' + file('../library/repository').absolutePath
pom.version = '0.0.11'
pom.version = '0.0.12'
pom.artifactId = 'ble-midi'
}
}
Expand Down
9 changes: 7 additions & 2 deletions BLE-MIDI-library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="jp.kshoji.blemidi">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ boolean isConnected(@NonNull BluetoothDevice device) {

private volatile static Object gattDiscoverServicesLock = null;
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) throws SecurityException {
super.onConnectionStateChange(gatt, status, newState);
// In this method, the `status` parameter shall be ignored.
// so, look `newState` parameter only.
Expand Down Expand Up @@ -296,7 +296,7 @@ void disconnectDevice(@NonNull MidiOutputDevice midiOutputDevice) {
*
* @param deviceAddress the device address from {@link android.bluetooth.BluetoothGatt}
*/
private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
private void disconnectByDeviceAddress(@NonNull String deviceAddress) throws SecurityException {
synchronized (deviceAddressGattMap) {
List<BluetoothGatt> bluetoothGatts = deviceAddressGattMap.get(deviceAddress);

Expand Down Expand Up @@ -347,7 +347,7 @@ private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
/**
* Terminates callback
*/
public void terminate() {
public void terminate() throws SecurityException {
synchronized (deviceAddressGattMap) {
for (List<BluetoothGatt> bluetoothGatts : deviceAddressGattMap.values()) {
if (bluetoothGatts != null) {
Expand Down Expand Up @@ -514,7 +514,7 @@ private static final class InternalMidiInputDevice extends MidiInputDevice {
* @param bluetoothGatt the gatt of device
* @throws IllegalArgumentException if specified gatt doesn't contain BLE MIDI service
*/
public InternalMidiInputDevice(@NonNull final Context context, @NonNull final BluetoothGatt bluetoothGatt) throws IllegalArgumentException {
public InternalMidiInputDevice(@NonNull final Context context, @NonNull final BluetoothGatt bluetoothGatt) throws IllegalArgumentException, SecurityException {
super();
this.bluetoothGatt = bluetoothGatt;

Expand Down Expand Up @@ -543,7 +543,7 @@ void stop() {
/**
* Configure the device as BLE Central
*/
public void configureAsCentralDevice() {
public void configureAsCentralDevice() throws SecurityException {
bluetoothGatt.setCharacteristicNotification(midiInputCharacteristic, true);

List<BluetoothGattDescriptor> descriptors = midiInputCharacteristic.getDescriptors();
Expand All @@ -564,7 +564,7 @@ public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventL

@NonNull
@Override
public String getDeviceName() {
public String getDeviceName() throws SecurityException {
return bluetoothGatt.getDevice().getName();
}

Expand Down Expand Up @@ -604,7 +604,7 @@ private static final class InternalMidiOutputDevice extends MidiOutputDevice {
* @param bluetoothGatt the gatt of device
* @throws IllegalArgumentException if specified gatt doesn't contain BLE MIDI service
*/
public InternalMidiOutputDevice(@NonNull final Context context, @NonNull final BluetoothGatt bluetoothGatt) throws IllegalArgumentException {
public InternalMidiOutputDevice(@NonNull final Context context, @NonNull final BluetoothGatt bluetoothGatt) throws IllegalArgumentException, SecurityException {
super();
this.bluetoothGatt = bluetoothGatt;

Expand All @@ -631,7 +631,7 @@ public void configureAsCentralDevice() {
}

@Override
public void transferData(@NonNull byte[] writeBuffer) {
public void transferData(@NonNull byte[] writeBuffer) throws SecurityException {
midiOutputCharacteristic.setValue(writeBuffer);

try {
Expand All @@ -644,7 +644,7 @@ public void transferData(@NonNull byte[] writeBuffer) {

@NonNull
@Override
public String getDeviceName() {
public String getDeviceName() throws SecurityException {
return bluetoothGatt.getDevice().getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class BleMidiCentralProvider {
*/
private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) throws SecurityException {

if (bluetoothDevice.getType() != BluetoothDevice.DEVICE_TYPE_LE && bluetoothDevice.getType() != BluetoothDevice.DEVICE_TYPE_DUAL) {
return;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void run() {
* @param context the context
*/
@SuppressLint("NewApi")
public BleMidiCentralProvider(@NonNull final Context context) throws UnsupportedOperationException {
public BleMidiCentralProvider(@NonNull final Context context) throws UnsupportedOperationException, SecurityException {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) == false) {
throw new UnsupportedOperationException("Bluetooth LE not supported on this device.");
}
Expand All @@ -107,7 +107,7 @@ public BleMidiCentralProvider(@NonNull final Context context) throws Unsupported
scanCallback = new ScanCallback() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onScanResult(int callbackType, ScanResult result) {
public void onScanResult(int callbackType, ScanResult result) throws SecurityException {
super.onScanResult(callbackType, result);

if (callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
Expand All @@ -121,7 +121,7 @@ public void onScanResult(int callbackType, ScanResult result) {
if (context instanceof Activity) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
public void run() throws SecurityException {
bluetoothDevice.connectGatt(BleMidiCentralProvider.this.context, true, midiCallback);
}
});
Expand All @@ -131,7 +131,7 @@ public void run() {
} else {
handler.post(new Runnable() {
@Override
public void run() {
public void run() throws SecurityException {
bluetoothDevice.connectGatt(BleMidiCentralProvider.this.context, true, midiCallback);
}
});
Expand Down Expand Up @@ -171,7 +171,7 @@ public void setRequestPairing(boolean needsPairing) {
* @param timeoutInMilliSeconds 0 or negative value : no timeout
*/
@SuppressLint({ "Deprecation", "NewApi" })
public void startScanDevice(int timeoutInMilliSeconds) {
public void startScanDevice(int timeoutInMilliSeconds) throws SecurityException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
List<ScanFilter> scanFilters = BleMidiDeviceUtils.getBleMidiScanFilters(context);
Expand Down Expand Up @@ -210,7 +210,7 @@ public void run() {
* Stops to scan devices
*/
@SuppressLint({ "Deprecation", "NewApi" })
public void stopScanDevice() {
public void stopScanDevice() throws SecurityException {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public BleMidiPeripheralProvider(final Context context) throws UnsupportedOperat
/**
* Starts advertising
*/
public void startAdvertising() {
public void startAdvertising() throws SecurityException {
// register Gatt service to Gatt server
if (gattServer == null) {
gattServer = bluetoothManager.openGattServer(context, gattServerCallback);
Expand Down Expand Up @@ -212,7 +212,7 @@ public void startAdvertising() {
/**
* Stops advertising
*/
public void stopAdvertising() {
public void stopAdvertising() throws SecurityException {
try {
bluetoothLeAdvertiser.stopAdvertising(advertiseCallback);
} catch (IllegalStateException ignored) {
Expand Down Expand Up @@ -257,7 +257,7 @@ public void disconnectDevice(@NonNull MidiOutputDevice midiOutputDevice) {
*/
private final BluetoothGattCallback disconnectCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) throws SecurityException {
super.onConnectionStateChange(gatt, status, newState);
Log.d(Constants.TAG, "onConnectionStateChange status: " + status + ", newState: " + newState);
// disconnect the device
Expand All @@ -272,7 +272,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
*
* @param deviceAddress the device address from {@link android.bluetooth.BluetoothGatt}
*/
private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
private void disconnectByDeviceAddress(@NonNull String deviceAddress) throws SecurityException {
synchronized (bluetoothDevicesMap) {
BluetoothDevice bluetoothDevice = bluetoothDevicesMap.get(deviceAddress);
if (bluetoothDevice != null) {
Expand All @@ -285,7 +285,7 @@ private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
/**
* Terminates provider
*/
public void terminate() {
public void terminate() throws SecurityException {
stopAdvertising();

synchronized (bluetoothDevicesMap) {
Expand Down Expand Up @@ -373,7 +373,7 @@ public void onConnectionStateChange(BluetoothDevice device, int status, int newS
}

@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) throws SecurityException {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);

UUID characteristicUuid = characteristic.getUuid();
Expand All @@ -398,7 +398,7 @@ public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, i
}

@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) throws SecurityException {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);

if (BleUuidUtils.matches(characteristic.getUuid(), CHARACTERISTIC_BLE_MIDI)) {
Expand All @@ -416,7 +416,7 @@ public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
}

@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) throws SecurityException {
super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);

byte[] descriptorValue = descriptor.getValue();
Expand All @@ -430,7 +430,7 @@ public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, Blue
}

@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) throws SecurityException {
super.onDescriptorReadRequest(device, requestId, offset, descriptor);

if (offset == 0) {
Expand Down Expand Up @@ -587,7 +587,7 @@ public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventL

@NonNull
@Override
public String getDeviceName() {
public String getDeviceName() throws SecurityException {
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
return bluetoothDevice.getAddress();
}
Expand Down Expand Up @@ -635,15 +635,15 @@ public InternalMidiOutputDevice(@NonNull final BluetoothDevice bluetoothDevice,

@NonNull
@Override
public String getDeviceName() {
public String getDeviceName() throws SecurityException {
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
return bluetoothDevice.getAddress();
}
return bluetoothDevice.getName();
}

@Override
public void transferData(@NonNull byte[] writeBuffer) {
public void transferData(@NonNull byte[] writeBuffer) throws SecurityException {
midiOutputCharacteristic.setValue(writeBuffer);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,38 @@ private long calculateEventFireTime(final int timestamp) {
// checks timestamp value is always zero
if (isTimestampAlwaysZero != null) {
if (isTimestampAlwaysZero) {
return currentTimeMillis;
if (timestamp != 0) {
// timestamp comes with non-zero. prevent misdetection
isTimestampAlwaysZero = false;
zeroTimestampCount = 0;
lastTimestampRecorded = 0;
} else {
// event fires immediately
return currentTimeMillis;
}
} else {
if (timestamp == 0) {
// recheck timestamp value on next time
isTimestampAlwaysZero = null;
zeroTimestampCount = 0;
// event fires immediately
return currentTimeMillis;
}
}
} else {
if (timestamp == 0) {
if (zeroTimestampCount >= 3) {
// decides timestamp is always zero: event fires immediately
// decides timestamp is always zero
isTimestampAlwaysZero = true;
return currentTimeMillis;
} else {
zeroTimestampCount++;
}
// event fires immediately
return currentTimeMillis;
} else {
isTimestampAlwaysZero = false;
zeroTimestampCount = 0;
lastTimestampRecorded = 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ public static boolean isBleSupported(@NonNull final Context context) {

final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

final BluetoothAdapter bluetoothAdapter;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
bluetoothAdapter = bluetoothManager.getAdapter();
} else {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter != null) {
return true;
}
Expand Down Expand Up @@ -81,13 +75,7 @@ public static boolean isBluetoothEnabled(@NonNull final Context context) {
return false;
}

final BluetoothAdapter bluetoothAdapter;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
bluetoothAdapter = bluetoothManager.getAdapter();
} else {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
return false;
}
Expand Down
Loading

0 comments on commit ddfa381

Please sign in to comment.