Skip to content

Commit ddfa381

Browse files
committed
Merge branch 'release/v0.0.12'
2 parents 503b28e + 94f3001 commit ddfa381

29 files changed

+630
-199
lines changed

.github/workflows/android.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Android CI
2+
3+
on:
4+
push:
5+
branches: [ "develop" ]
6+
pull_request:
7+
branches: [ "develop" ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: set up JDK 11
17+
uses: actions/setup-java@v3
18+
with:
19+
java-version: '11'
20+
distribution: 'temurin'
21+
cache: gradle
22+
23+
- name: Grant execute permission for gradlew
24+
run: chmod +x gradlew
25+
- name: Build with Gradle
26+
run: ./gradlew build

BLE-MIDI-library/build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 29
4+
compileSdkVersion 32
55

66
defaultConfig {
77
minSdkVersion 18
8+
targetSdkVersion 32
89
}
910

1011
compileOptions {
@@ -39,7 +40,7 @@ group = 'jp.kshoji'
3940
uploadArchives {
4041
repositories.mavenDeployer {
4142
repository url: 'file://' + file('../library/repository').absolutePath
42-
pom.version = '0.0.11'
43+
pom.version = '0.0.12'
4344
pom.artifactId = 'ble-midi'
4445
}
4546
}

BLE-MIDI-library/src/main/AndroidManifest.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
23
package="jp.kshoji.blemidi">
34

4-
<uses-permission android:name="android.permission.BLUETOOTH" />
5-
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5+
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
6+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
7+
8+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
9+
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
10+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
611

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

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/central/BleMidiCallback.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ boolean isConnected(@NonNull BluetoothDevice device) {
7979

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

@@ -347,7 +347,7 @@ private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
347347
/**
348348
* Terminates callback
349349
*/
350-
public void terminate() {
350+
public void terminate() throws SecurityException {
351351
synchronized (deviceAddressGattMap) {
352352
for (List<BluetoothGatt> bluetoothGatts : deviceAddressGattMap.values()) {
353353
if (bluetoothGatts != null) {
@@ -514,7 +514,7 @@ private static final class InternalMidiInputDevice extends MidiInputDevice {
514514
* @param bluetoothGatt the gatt of device
515515
* @throws IllegalArgumentException if specified gatt doesn't contain BLE MIDI service
516516
*/
517-
public InternalMidiInputDevice(@NonNull final Context context, @NonNull final BluetoothGatt bluetoothGatt) throws IllegalArgumentException {
517+
public InternalMidiInputDevice(@NonNull final Context context, @NonNull final BluetoothGatt bluetoothGatt) throws IllegalArgumentException, SecurityException {
518518
super();
519519
this.bluetoothGatt = bluetoothGatt;
520520

@@ -543,7 +543,7 @@ void stop() {
543543
/**
544544
* Configure the device as BLE Central
545545
*/
546-
public void configureAsCentralDevice() {
546+
public void configureAsCentralDevice() throws SecurityException {
547547
bluetoothGatt.setCharacteristicNotification(midiInputCharacteristic, true);
548548

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

565565
@NonNull
566566
@Override
567-
public String getDeviceName() {
567+
public String getDeviceName() throws SecurityException {
568568
return bluetoothGatt.getDevice().getName();
569569
}
570570

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

@@ -631,7 +631,7 @@ public void configureAsCentralDevice() {
631631
}
632632

633633
@Override
634-
public void transferData(@NonNull byte[] writeBuffer) {
634+
public void transferData(@NonNull byte[] writeBuffer) throws SecurityException {
635635
midiOutputCharacteristic.setValue(writeBuffer);
636636

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

645645
@NonNull
646646
@Override
647-
public String getDeviceName() {
647+
public String getDeviceName() throws SecurityException {
648648
return bluetoothGatt.getDevice().getName();
649649
}
650650

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/central/BleMidiCentralProvider.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class BleMidiCentralProvider {
4646
*/
4747
private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
4848
@Override
49-
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {
49+
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) throws SecurityException {
5050

5151
if (bluetoothDevice.getType() != BluetoothDevice.DEVICE_TYPE_LE && bluetoothDevice.getType() != BluetoothDevice.DEVICE_TYPE_DUAL) {
5252
return;
@@ -86,7 +86,7 @@ public void run() {
8686
* @param context the context
8787
*/
8888
@SuppressLint("NewApi")
89-
public BleMidiCentralProvider(@NonNull final Context context) throws UnsupportedOperationException {
89+
public BleMidiCentralProvider(@NonNull final Context context) throws UnsupportedOperationException, SecurityException {
9090
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) == false) {
9191
throw new UnsupportedOperationException("Bluetooth LE not supported on this device.");
9292
}
@@ -107,7 +107,7 @@ public BleMidiCentralProvider(@NonNull final Context context) throws Unsupported
107107
scanCallback = new ScanCallback() {
108108
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
109109
@Override
110-
public void onScanResult(int callbackType, ScanResult result) {
110+
public void onScanResult(int callbackType, ScanResult result) throws SecurityException {
111111
super.onScanResult(callbackType, result);
112112

113113
if (callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
@@ -121,7 +121,7 @@ public void onScanResult(int callbackType, ScanResult result) {
121121
if (context instanceof Activity) {
122122
((Activity) context).runOnUiThread(new Runnable() {
123123
@Override
124-
public void run() {
124+
public void run() throws SecurityException {
125125
bluetoothDevice.connectGatt(BleMidiCentralProvider.this.context, true, midiCallback);
126126
}
127127
});
@@ -131,7 +131,7 @@ public void run() {
131131
} else {
132132
handler.post(new Runnable() {
133133
@Override
134-
public void run() {
134+
public void run() throws SecurityException {
135135
bluetoothDevice.connectGatt(BleMidiCentralProvider.this.context, true, midiCallback);
136136
}
137137
});
@@ -171,7 +171,7 @@ public void setRequestPairing(boolean needsPairing) {
171171
* @param timeoutInMilliSeconds 0 or negative value : no timeout
172172
*/
173173
@SuppressLint({ "Deprecation", "NewApi" })
174-
public void startScanDevice(int timeoutInMilliSeconds) {
174+
public void startScanDevice(int timeoutInMilliSeconds) throws SecurityException {
175175
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
176176
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
177177
List<ScanFilter> scanFilters = BleMidiDeviceUtils.getBleMidiScanFilters(context);
@@ -210,7 +210,7 @@ public void run() {
210210
* Stops to scan devices
211211
*/
212212
@SuppressLint({ "Deprecation", "NewApi" })
213-
public void stopScanDevice() {
213+
public void stopScanDevice() throws SecurityException {
214214
try {
215215
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
216216
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/peripheral/BleMidiPeripheralProvider.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public BleMidiPeripheralProvider(final Context context) throws UnsupportedOperat
141141
/**
142142
* Starts advertising
143143
*/
144-
public void startAdvertising() {
144+
public void startAdvertising() throws SecurityException {
145145
// register Gatt service to Gatt server
146146
if (gattServer == null) {
147147
gattServer = bluetoothManager.openGattServer(context, gattServerCallback);
@@ -212,7 +212,7 @@ public void startAdvertising() {
212212
/**
213213
* Stops advertising
214214
*/
215-
public void stopAdvertising() {
215+
public void stopAdvertising() throws SecurityException {
216216
try {
217217
bluetoothLeAdvertiser.stopAdvertising(advertiseCallback);
218218
} catch (IllegalStateException ignored) {
@@ -257,7 +257,7 @@ public void disconnectDevice(@NonNull MidiOutputDevice midiOutputDevice) {
257257
*/
258258
private final BluetoothGattCallback disconnectCallback = new BluetoothGattCallback() {
259259
@Override
260-
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
260+
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) throws SecurityException {
261261
super.onConnectionStateChange(gatt, status, newState);
262262
Log.d(Constants.TAG, "onConnectionStateChange status: " + status + ", newState: " + newState);
263263
// disconnect the device
@@ -272,7 +272,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
272272
*
273273
* @param deviceAddress the device address from {@link android.bluetooth.BluetoothGatt}
274274
*/
275-
private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
275+
private void disconnectByDeviceAddress(@NonNull String deviceAddress) throws SecurityException {
276276
synchronized (bluetoothDevicesMap) {
277277
BluetoothDevice bluetoothDevice = bluetoothDevicesMap.get(deviceAddress);
278278
if (bluetoothDevice != null) {
@@ -285,7 +285,7 @@ private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
285285
/**
286286
* Terminates provider
287287
*/
288-
public void terminate() {
288+
public void terminate() throws SecurityException {
289289
stopAdvertising();
290290

291291
synchronized (bluetoothDevicesMap) {
@@ -373,7 +373,7 @@ public void onConnectionStateChange(BluetoothDevice device, int status, int newS
373373
}
374374

375375
@Override
376-
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
376+
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) throws SecurityException {
377377
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
378378

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

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

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

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

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

432432
@Override
433-
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
433+
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) throws SecurityException {
434434
super.onDescriptorReadRequest(device, requestId, offset, descriptor);
435435

436436
if (offset == 0) {
@@ -587,7 +587,7 @@ public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventL
587587

588588
@NonNull
589589
@Override
590-
public String getDeviceName() {
590+
public String getDeviceName() throws SecurityException {
591591
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
592592
return bluetoothDevice.getAddress();
593593
}
@@ -635,15 +635,15 @@ public InternalMidiOutputDevice(@NonNull final BluetoothDevice bluetoothDevice,
635635

636636
@NonNull
637637
@Override
638-
public String getDeviceName() {
638+
public String getDeviceName() throws SecurityException {
639639
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
640640
return bluetoothDevice.getAddress();
641641
}
642642
return bluetoothDevice.getName();
643643
}
644644

645645
@Override
646-
public void transferData(@NonNull byte[] writeBuffer) {
646+
public void transferData(@NonNull byte[] writeBuffer) throws SecurityException {
647647
midiOutputCharacteristic.setValue(writeBuffer);
648648

649649
try {

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/util/BleMidiParser.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,38 @@ private long calculateEventFireTime(final int timestamp) {
134134
// checks timestamp value is always zero
135135
if (isTimestampAlwaysZero != null) {
136136
if (isTimestampAlwaysZero) {
137-
return currentTimeMillis;
137+
if (timestamp != 0) {
138+
// timestamp comes with non-zero. prevent misdetection
139+
isTimestampAlwaysZero = false;
140+
zeroTimestampCount = 0;
141+
lastTimestampRecorded = 0;
142+
} else {
143+
// event fires immediately
144+
return currentTimeMillis;
145+
}
146+
} else {
147+
if (timestamp == 0) {
148+
// recheck timestamp value on next time
149+
isTimestampAlwaysZero = null;
150+
zeroTimestampCount = 0;
151+
// event fires immediately
152+
return currentTimeMillis;
153+
}
138154
}
139155
} else {
140156
if (timestamp == 0) {
141157
if (zeroTimestampCount >= 3) {
142-
// decides timestamp is always zero: event fires immediately
158+
// decides timestamp is always zero
143159
isTimestampAlwaysZero = true;
144-
return currentTimeMillis;
145160
} else {
146161
zeroTimestampCount++;
147162
}
163+
// event fires immediately
164+
return currentTimeMillis;
148165
} else {
149166
isTimestampAlwaysZero = false;
167+
zeroTimestampCount = 0;
168+
lastTimestampRecorded = 0;
150169
}
151170
}
152171

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/util/BleUtils.java

+2-14
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ public static boolean isBleSupported(@NonNull final Context context) {
3131

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

34-
final BluetoothAdapter bluetoothAdapter;
35-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
36-
bluetoothAdapter = bluetoothManager.getAdapter();
37-
} else {
38-
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
39-
}
40-
34+
final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
4135
if (bluetoothAdapter != null) {
4236
return true;
4337
}
@@ -81,13 +75,7 @@ public static boolean isBluetoothEnabled(@NonNull final Context context) {
8175
return false;
8276
}
8377

84-
final BluetoothAdapter bluetoothAdapter;
85-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
86-
bluetoothAdapter = bluetoothManager.getAdapter();
87-
} else {
88-
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
89-
}
90-
78+
final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
9179
if (bluetoothAdapter == null) {
9280
return false;
9381
}

0 commit comments

Comments
 (0)