Skip to content

Commit c28d5aa

Browse files
committed
Merge branch 'release/v0.0.14'
2 parents d5090a3 + afaec10 commit c28d5aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+889
-194
lines changed

.github/workflows/android.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v3
16-
- name: set up JDK 11
16+
- name: set up JDK 17
1717
uses: actions/setup-java@v3
1818
with:
19-
java-version: '11'
19+
java-version: '17'
2020
distribution: 'temurin'
2121
cache: gradle
2222

BLE-MIDI-library/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ android {
2828
withSourcesJar()
2929
}
3030
}
31+
namespace 'jp.kshoji.blemidi'
3132
}
3233

3334
repositories {
@@ -49,7 +50,7 @@ publishing {
4950
release(MavenPublication) {
5051
group = 'jp.kshoji'
5152
artifactId = 'ble-midi'
52-
version = '0.0.13'
53+
version = '0.0.14'
5354

5455
afterEvaluate {
5556
from components.release

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="jp.kshoji.blemidi">
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
32

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

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

+261-110
Large diffs are not rendered by default.

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

+8
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public void run() throws SecurityException {
184184

185185
private volatile boolean isScanning = false;
186186

187+
/**
188+
* Sets MidiInputDevice to start automatically at being connected
189+
* @param enable true to enable, default: true
190+
*/
191+
public void setAutoStartInputDevice(boolean enable) {
192+
midiCallback.setAutoStartDevice(enable);
193+
}
194+
187195
/**
188196
* Set if the Bluetooth LE device need `Pairing` <br />
189197
* Pairing feature can be used on Android KitKat (API Level 19) or later.

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/device/MidiInputDevice.java

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ public abstract class MidiInputDevice {
1919
*/
2020
public abstract void setOnMidiInputEventListener(@Nullable OnMidiInputEventListener midiInputEventListener);
2121

22+
/**
23+
* Starts using the device
24+
*/
25+
public abstract void start();
26+
27+
/**
28+
* Stops using the device
29+
*/
30+
public abstract void stop();
31+
32+
/**
33+
* Terminates the device instance
34+
*/
35+
public abstract void terminate();
36+
2237
/**
2338
* Obtains the device name
2439
*
@@ -27,6 +42,22 @@ public abstract class MidiInputDevice {
2742
@NonNull
2843
public abstract String getDeviceName();
2944

45+
/**
46+
* Obtains the manufacturer name
47+
*
48+
* @return manufacturer name
49+
*/
50+
@NonNull
51+
public abstract String getManufacturer();
52+
53+
/**
54+
* Obtains the model name
55+
*
56+
* @return model name
57+
*/
58+
@NonNull
59+
public abstract String getModel();
60+
3061
/**
3162
* Obtains the device address
3263
*

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/device/MidiOutputDevice.java

+76-11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ public abstract class MidiOutputDevice {
3131
@NonNull
3232
public abstract String getDeviceName();
3333

34+
/**
35+
* Obtains the manufacturer name
36+
*
37+
* @return manufacturer name
38+
*/
39+
@NonNull
40+
public abstract String getManufacturer();
41+
42+
/**
43+
* Obtains the model name
44+
*
45+
* @return model name
46+
*/
47+
@NonNull
48+
public abstract String getModel();
49+
3450
/**
3551
* Obtains the device address
3652
*
@@ -52,23 +68,44 @@ public final String toString() {
5268
}
5369

5470
volatile boolean transferDataThreadAlive;
71+
volatile boolean isRunning;
5572
final Thread transferDataThread = new Thread(new Runnable() {
5673
@Override
5774
public void run() {
5875
transferDataThreadAlive = true;
5976

60-
while (transferDataThreadAlive) {
61-
synchronized (transferDataStream) {
62-
if (writtenDataCount > 0) {
63-
transferData(transferDataStream.toByteArray());
64-
transferDataStream.reset();
65-
writtenDataCount = 0;
77+
while (true) {
78+
// running
79+
while (!transferDataThreadAlive && isRunning) {
80+
synchronized (transferDataStream) {
81+
if (writtenDataCount > 0) {
82+
transferData(transferDataStream.toByteArray());
83+
transferDataStream.reset();
84+
writtenDataCount = 0;
85+
}
86+
}
87+
88+
try {
89+
Thread.sleep(10);
90+
} catch (InterruptedException ignored) {
6691
}
6792
}
6893

69-
try {
70-
Thread.sleep(10);
71-
} catch (InterruptedException ignored) {
94+
if (!transferDataThreadAlive) {
95+
break;
96+
}
97+
98+
// stopping
99+
while (!transferDataThreadAlive && !isRunning) {
100+
// sleep until interrupt
101+
try {
102+
Thread.sleep(10);
103+
} catch (InterruptedException ignored) {
104+
}
105+
}
106+
107+
if (!transferDataThreadAlive) {
108+
break;
72109
}
73110
}
74111
}
@@ -79,14 +116,42 @@ protected MidiOutputDevice() {
79116
}
80117

81118
/**
82-
* Stops transfer thread
119+
* Starts using the device
120+
*/
121+
public final void start() {
122+
if (!transferDataThreadAlive) {
123+
return;
124+
}
125+
isRunning = true;
126+
transferDataThread.interrupt();
127+
}
128+
129+
/**
130+
* Stops using the device
83131
*/
84-
public void stop() {
132+
public final void stop() {
133+
if (!transferDataThreadAlive) {
134+
return;
135+
}
136+
isRunning = false;
137+
transferDataThread.interrupt();
138+
}
139+
140+
/**
141+
* Terminates the device instance
142+
*/
143+
public final void terminate() {
85144
transferDataThreadAlive = false;
145+
isRunning = false;
146+
transferDataThread.interrupt();
86147
}
87148

88149
transient int writtenDataCount;
89150
private void storeTransferData(byte[] data) {
151+
if (!transferDataThreadAlive || !isRunning) {
152+
return;
153+
}
154+
90155
synchronized (transferDataStream) {
91156
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
92157
if (writtenDataCount == 0) {

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

+63-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.os.ParcelUuid;
2222
import android.support.annotation.NonNull;
2323
import android.support.annotation.Nullable;
24-
import android.text.TextUtils;
2524
import android.util.Log;
2625

2726
import java.nio.charset.StandardCharsets;
@@ -87,6 +86,7 @@ public final class BleMidiPeripheralProvider {
8786

8887
private OnMidiDeviceAttachedListener midiDeviceAttachedListener;
8988
private OnMidiDeviceDetachedListener midiDeviceDetachedListener;
89+
private boolean autoStartDevice = true;
9090

9191
private String manufacturer = "kshoji.jp";
9292
private String deviceName = "BLE MIDI";
@@ -359,7 +359,7 @@ public void onConnectionStateChange(BluetoothDevice device, int status, int newS
359359
if (midiInputDevice != null) {
360360
midiInputDevicesMap.remove(deviceAddress);
361361

362-
((InternalMidiInputDevice) midiInputDevice).stop();
362+
midiInputDevice.terminate();
363363
midiInputDevice.setOnMidiInputEventListener(null);
364364
if (midiDeviceDetachedListener != null) {
365365
midiDeviceDetachedListener.onMidiInputDeviceDetached(midiInputDevice);
@@ -397,10 +397,12 @@ public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, i
397397
} else {
398398
switch (BleUuidUtils.toShortValue(characteristicUuid)) {
399399
case MODEL_NUMBER:
400-
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, deviceName.getBytes(StandardCharsets.UTF_8));
400+
// the running device's MODEL
401+
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, Build.MODEL.substring(0, Math.min(Build.MODEL.length(), 20)).getBytes(StandardCharsets.UTF_8));
401402
break;
402403
case MANUFACTURER_NAME:
403-
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, manufacturer.getBytes(StandardCharsets.UTF_8));
404+
// the running device's MANUFACTURER
405+
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, Build.MANUFACTURER.substring(0, Math.min(Build.MANUFACTURER.length(), 20)).getBytes(StandardCharsets.UTF_8));
404406
break;
405407
default:
406408
// send empty
@@ -487,6 +489,11 @@ private void connectMidiDevice(@NonNull BluetoothDevice device) {
487489
midiDeviceAttachedListener.onMidiInputDeviceAttached(midiInputDevice);
488490
midiDeviceAttachedListener.onMidiOutputDeviceAttached(midiOutputDevice);
489491
}
492+
493+
if (autoStartDevice) {
494+
midiInputDevice.start();
495+
midiOutputDevice.start();
496+
}
490497
}
491498

492499
/**
@@ -565,6 +572,14 @@ public void setDeviceName(@NonNull String deviceName) {
565572
}
566573
}
567574

575+
/**
576+
* Sets MidiInputDevice to start automatically at being connected
577+
* @param enable true to enable, default: true
578+
*/
579+
public void setAutoStartDevice(boolean enable) {
580+
autoStartDevice = enable;
581+
}
582+
568583
/**
569584
* {@link jp.kshoji.blemidi.device.MidiInputDevice} for Peripheral
570585
*
@@ -586,13 +601,30 @@ public InternalMidiInputDevice(@NonNull BluetoothDevice bluetoothDevice) {
586601
this.bluetoothDevice = bluetoothDevice;
587602
}
588603

604+
/**
605+
* Starts parser's thread
606+
*/
607+
@Override
608+
public void start() {
609+
midiParser.start();
610+
}
611+
589612
/**
590613
* Stops parser's thread
591614
*/
592-
void stop() {
615+
@Override
616+
public void stop() {
593617
midiParser.stop();
594618
}
595619

620+
/**
621+
* Terminates parser's thread
622+
*/
623+
@Override
624+
public void terminate() {
625+
midiParser.terminate();
626+
}
627+
596628
@Override
597629
public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventListener) {
598630
midiParser.setMidiInputEventListener(midiInputEventListener);
@@ -601,10 +633,19 @@ public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventL
601633
@NonNull
602634
@Override
603635
public String getDeviceName() throws SecurityException {
604-
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
605-
return bluetoothDevice.getAddress();
606-
}
607-
return bluetoothDevice.getName();
636+
return "";
637+
}
638+
639+
@NonNull
640+
@Override
641+
public String getManufacturer() {
642+
return "";
643+
}
644+
645+
@NonNull
646+
@Override
647+
public String getModel() {
648+
return "";
608649
}
609650

610651
private void incomingData(@NonNull byte[] data) {
@@ -650,10 +691,19 @@ public InternalMidiOutputDevice(@NonNull final BluetoothDevice bluetoothDevice,
650691
@NonNull
651692
@Override
652693
public String getDeviceName() throws SecurityException {
653-
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
654-
return bluetoothDevice.getAddress();
655-
}
656-
return bluetoothDevice.getName();
694+
return "";
695+
}
696+
697+
@NonNull
698+
@Override
699+
public String getManufacturer() {
700+
return "";
701+
}
702+
703+
@NonNull
704+
@Override
705+
public String getModel() {
706+
return "";
657707
}
658708

659709
@Override

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/service/BleMidiCentralService.java

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public IBinder onBind(Intent intent) {
4141
@Override
4242
protected void onStart() {
4343
midiProvider = new BleMidiCentralProvider(this);
44+
midiProvider.setAutoStartInputDevice(true);
4445
midiProvider.setOnMidiDeviceAttachedListener(serviceMidiDeviceAttachedListener);
4546
midiProvider.setOnMidiDeviceDetachedListener(serviceMidiDeviceDetachedListener);
4647
midiProvider.startScanDevice(0);

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/service/BleMidiPeripheralService.java

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public IBinder onBind(Intent intent) {
4444
@Override
4545
protected void onStart() {
4646
midiProvider = new BleMidiPeripheralProvider(this);
47+
midiProvider.setAutoStartDevice(true);
4748
midiProvider.setOnMidiDeviceAttachedListener(serviceMidiDeviceAttachedListener);
4849
midiProvider.setOnMidiDeviceDetachedListener(serviceMidiDeviceDetachedListener);
4950
}

0 commit comments

Comments
 (0)