Skip to content

Commit

Permalink
Merge branch 'release/v0.0.14'
Browse files Browse the repository at this point in the history
  • Loading branch information
kshoji committed Dec 4, 2023
2 parents d5090a3 + afaec10 commit c28d5aa
Show file tree
Hide file tree
Showing 48 changed files with 889 additions and 194 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:

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

Expand Down
3 changes: 2 additions & 1 deletion BLE-MIDI-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ android {
withSourcesJar()
}
}
namespace 'jp.kshoji.blemidi'
}

repositories {
Expand All @@ -49,7 +50,7 @@ publishing {
release(MavenPublication) {
group = 'jp.kshoji'
artifactId = 'ble-midi'
version = '0.0.13'
version = '0.0.14'

afterEvaluate {
from components.release
Expand Down
3 changes: 1 addition & 2 deletions BLE-MIDI-library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.kshoji.blemidi">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ public void run() throws SecurityException {

private volatile boolean isScanning = false;

/**
* Sets MidiInputDevice to start automatically at being connected
* @param enable true to enable, default: true
*/
public void setAutoStartInputDevice(boolean enable) {
midiCallback.setAutoStartDevice(enable);
}

/**
* Set if the Bluetooth LE device need `Pairing` <br />
* Pairing feature can be used on Android KitKat (API Level 19) or later.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public abstract class MidiInputDevice {
*/
public abstract void setOnMidiInputEventListener(@Nullable OnMidiInputEventListener midiInputEventListener);

/**
* Starts using the device
*/
public abstract void start();

/**
* Stops using the device
*/
public abstract void stop();

/**
* Terminates the device instance
*/
public abstract void terminate();

/**
* Obtains the device name
*
Expand All @@ -27,6 +42,22 @@ public abstract class MidiInputDevice {
@NonNull
public abstract String getDeviceName();

/**
* Obtains the manufacturer name
*
* @return manufacturer name
*/
@NonNull
public abstract String getManufacturer();

/**
* Obtains the model name
*
* @return model name
*/
@NonNull
public abstract String getModel();

/**
* Obtains the device address
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public abstract class MidiOutputDevice {
@NonNull
public abstract String getDeviceName();

/**
* Obtains the manufacturer name
*
* @return manufacturer name
*/
@NonNull
public abstract String getManufacturer();

/**
* Obtains the model name
*
* @return model name
*/
@NonNull
public abstract String getModel();

/**
* Obtains the device address
*
Expand All @@ -52,23 +68,44 @@ public final String toString() {
}

volatile boolean transferDataThreadAlive;
volatile boolean isRunning;
final Thread transferDataThread = new Thread(new Runnable() {
@Override
public void run() {
transferDataThreadAlive = true;

while (transferDataThreadAlive) {
synchronized (transferDataStream) {
if (writtenDataCount > 0) {
transferData(transferDataStream.toByteArray());
transferDataStream.reset();
writtenDataCount = 0;
while (true) {
// running
while (!transferDataThreadAlive && isRunning) {
synchronized (transferDataStream) {
if (writtenDataCount > 0) {
transferData(transferDataStream.toByteArray());
transferDataStream.reset();
writtenDataCount = 0;
}
}

try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
}

try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
if (!transferDataThreadAlive) {
break;
}

// stopping
while (!transferDataThreadAlive && !isRunning) {
// sleep until interrupt
try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
}

if (!transferDataThreadAlive) {
break;
}
}
}
Expand All @@ -79,14 +116,42 @@ protected MidiOutputDevice() {
}

/**
* Stops transfer thread
* Starts using the device
*/
public final void start() {
if (!transferDataThreadAlive) {
return;
}
isRunning = true;
transferDataThread.interrupt();
}

/**
* Stops using the device
*/
public void stop() {
public final void stop() {
if (!transferDataThreadAlive) {
return;
}
isRunning = false;
transferDataThread.interrupt();
}

/**
* Terminates the device instance
*/
public final void terminate() {
transferDataThreadAlive = false;
isRunning = false;
transferDataThread.interrupt();
}

transient int writtenDataCount;
private void storeTransferData(byte[] data) {
if (!transferDataThreadAlive || !isRunning) {
return;
}

synchronized (transferDataStream) {
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
if (writtenDataCount == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.os.ParcelUuid;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

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

private OnMidiDeviceAttachedListener midiDeviceAttachedListener;
private OnMidiDeviceDetachedListener midiDeviceDetachedListener;
private boolean autoStartDevice = true;

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

((InternalMidiInputDevice) midiInputDevice).stop();
midiInputDevice.terminate();
midiInputDevice.setOnMidiInputEventListener(null);
if (midiDeviceDetachedListener != null) {
midiDeviceDetachedListener.onMidiInputDeviceDetached(midiInputDevice);
Expand Down Expand Up @@ -397,10 +397,12 @@ public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, i
} else {
switch (BleUuidUtils.toShortValue(characteristicUuid)) {
case MODEL_NUMBER:
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, deviceName.getBytes(StandardCharsets.UTF_8));
// the running device's MODEL
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, Build.MODEL.substring(0, Math.min(Build.MODEL.length(), 20)).getBytes(StandardCharsets.UTF_8));
break;
case MANUFACTURER_NAME:
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, manufacturer.getBytes(StandardCharsets.UTF_8));
// the running device's MANUFACTURER
gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, Build.MANUFACTURER.substring(0, Math.min(Build.MANUFACTURER.length(), 20)).getBytes(StandardCharsets.UTF_8));
break;
default:
// send empty
Expand Down Expand Up @@ -487,6 +489,11 @@ private void connectMidiDevice(@NonNull BluetoothDevice device) {
midiDeviceAttachedListener.onMidiInputDeviceAttached(midiInputDevice);
midiDeviceAttachedListener.onMidiOutputDeviceAttached(midiOutputDevice);
}

if (autoStartDevice) {
midiInputDevice.start();
midiOutputDevice.start();
}
}

/**
Expand Down Expand Up @@ -565,6 +572,14 @@ public void setDeviceName(@NonNull String deviceName) {
}
}

/**
* Sets MidiInputDevice to start automatically at being connected
* @param enable true to enable, default: true
*/
public void setAutoStartDevice(boolean enable) {
autoStartDevice = enable;
}

/**
* {@link jp.kshoji.blemidi.device.MidiInputDevice} for Peripheral
*
Expand All @@ -586,13 +601,30 @@ public InternalMidiInputDevice(@NonNull BluetoothDevice bluetoothDevice) {
this.bluetoothDevice = bluetoothDevice;
}

/**
* Starts parser's thread
*/
@Override
public void start() {
midiParser.start();
}

/**
* Stops parser's thread
*/
void stop() {
@Override
public void stop() {
midiParser.stop();
}

/**
* Terminates parser's thread
*/
@Override
public void terminate() {
midiParser.terminate();
}

@Override
public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventListener) {
midiParser.setMidiInputEventListener(midiInputEventListener);
Expand All @@ -601,10 +633,19 @@ public void setOnMidiInputEventListener(OnMidiInputEventListener midiInputEventL
@NonNull
@Override
public String getDeviceName() throws SecurityException {
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
return bluetoothDevice.getAddress();
}
return bluetoothDevice.getName();
return "";
}

@NonNull
@Override
public String getManufacturer() {
return "";
}

@NonNull
@Override
public String getModel() {
return "";
}

private void incomingData(@NonNull byte[] data) {
Expand Down Expand Up @@ -650,10 +691,19 @@ public InternalMidiOutputDevice(@NonNull final BluetoothDevice bluetoothDevice,
@NonNull
@Override
public String getDeviceName() throws SecurityException {
if (TextUtils.isEmpty(bluetoothDevice.getName())) {
return bluetoothDevice.getAddress();
}
return bluetoothDevice.getName();
return "";
}

@NonNull
@Override
public String getManufacturer() {
return "";
}

@NonNull
@Override
public String getModel() {
return "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public IBinder onBind(Intent intent) {
@Override
protected void onStart() {
midiProvider = new BleMidiCentralProvider(this);
midiProvider.setAutoStartInputDevice(true);
midiProvider.setOnMidiDeviceAttachedListener(serviceMidiDeviceAttachedListener);
midiProvider.setOnMidiDeviceDetachedListener(serviceMidiDeviceDetachedListener);
midiProvider.startScanDevice(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public IBinder onBind(Intent intent) {
@Override
protected void onStart() {
midiProvider = new BleMidiPeripheralProvider(this);
midiProvider.setAutoStartDevice(true);
midiProvider.setOnMidiDeviceAttachedListener(serviceMidiDeviceAttachedListener);
midiProvider.setOnMidiDeviceDetachedListener(serviceMidiDeviceDetachedListener);
}
Expand Down
Loading

0 comments on commit c28d5aa

Please sign in to comment.