Skip to content

Commit

Permalink
Merge branch 'release/v.0.0.7'
Browse files Browse the repository at this point in the history
Conflicts:
	library/build.gradle
	library/repository/jp/kshoji/ble-midi/maven-metadata.xml
	library/repository/jp/kshoji/ble-midi/maven-metadata.xml.md5
	library/repository/jp/kshoji/ble-midi/maven-metadata.xml.sha1
  • Loading branch information
kshoji committed Mar 31, 2015
2 parents 942d1ff + 35f5cc8 commit c473c5f
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 16 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ group = 'jp.kshoji'
uploadArchives {
repositories.mavenDeployer {
repository url: 'file://' + file('repository').absolutePath
pom.version = '0.0.6'
pom.version = '0.0.7'
pom.artifactId = 'ble-midi'
}
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8399d9eb0e0d731bc81254762da77319
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c817c7845b0eb3f2a0df056375227fbb85782ddc
24 changes: 24 additions & 0 deletions library/repository/jp/kshoji/ble-midi/0.0.7/ble-midi-0.0.7.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>jp.kshoji</groupId>
<artifactId>ble-midi</artifactId>
<version>0.0.7</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>jp.kshoji</groupId>
<artifactId>javax-sound-midi</artifactId>
<version>0.0.2</version>
<type>aar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>support-annotations</artifactId>
<version>21.0.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
89673aed8c0b52b53a09a1137ff82f7c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e612d6a35d5e67b571ca79ca3c4dab665d8777c9
3 changes: 2 additions & 1 deletion library/repository/jp/kshoji/ble-midi/maven-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
<version>0.0.6</version>
<version>0.0.7</version>
</versions>
<lastUpdated>20150302031602</lastUpdated>
<lastUpdated>20150331030008</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c15107f5e2c02e4e9c2d20167816916f
04d16e77c877fa8e88460604ba735e40
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6a74d79b7c0472191907cecf267e9a7c8531d7cd
4231f69f67134ee0ecf6411f76f79fc284fc373c
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,20 @@ public final void sendMidiSystemExclusive(@NonNull byte[] systemExclusive) {
byte[] timestampAddedSystemExclusive = new byte[systemExclusive.length + 2];
System.arraycopy(systemExclusive, 0, timestampAddedSystemExclusive, 1, systemExclusive.length);

// split into 20 bytes. BLE can't send more than 20 bytes by default MTU.
byte[] writeBuffer = new byte[20];

long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
timestampAddedSystemExclusive[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));

// extend a byte for timestamp LSB, before the last byte('F7')
timestampAddedSystemExclusive[systemExclusive.length + 1] = systemExclusive[systemExclusive.length - 1];
// set first byte to timestamp LSB
timestampAddedSystemExclusive[0] = (byte) (0x80 | (timestamp & 0x7f));

// split into 20 bytes. BLE can't send more than 20 bytes by default MTU.
byte[] writeBuffer = new byte[20];
for (int i = 0; i < timestampAddedSystemExclusive.length; i += 19) {
writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));
timestampAddedSystemExclusive[systemExclusive.length] = (byte) (0x80 | (timestamp & 0x7f));
// Don't send 0xF7 timestamp LSB inside of SysEx(MIDI parser will fail) 0x7f -> 0x7e
timestampAddedSystemExclusive[systemExclusive.length] = (byte) (0x80 | (timestamp & 0x7e));

if (i + 20 <= timestampAddedSystemExclusive.length) {
if (i + 19 <= timestampAddedSystemExclusive.length) {
System.arraycopy(timestampAddedSystemExclusive, i, writeBuffer, 1, 19);
} else {
// last message
Expand All @@ -118,6 +121,9 @@ public final void sendMidiSystemExclusive(@NonNull byte[] systemExclusive) {
System.arraycopy(timestampAddedSystemExclusive, i, writeBuffer, 1, timestampAddedSystemExclusive.length - i);
}

// timestamp MSB
writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));

transferData(writeBuffer);

timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package jp.kshoji.blemidi.service;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import jp.kshoji.blemidi.device.MidiInputDevice;
import jp.kshoji.blemidi.device.MidiOutputDevice;
import jp.kshoji.blemidi.listener.OnMidiDeviceAttachedListener;
import jp.kshoji.blemidi.listener.OnMidiDeviceDetachedListener;
import jp.kshoji.blemidi.util.Constants;

/**
* Abstract Service for BLE MIDI
*
* @author K.Shoji
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
abstract class AbstractBleMidiService extends Service {

private final Set<MidiInputDevice> midiInputDevices = new HashSet<>();
private final Set<MidiOutputDevice> midiOutputDevices = new HashSet<>();

private OnMidiDeviceAttachedListener midiDeviceAttachedListener = null;
private OnMidiDeviceDetachedListener midiDeviceDetachedListener = null;

private boolean isRunning = false;

protected abstract void onStart();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!isRunning) {
Log.d(Constants.TAG, "MIDI service starting.");

onStart();

isRunning = true;
}

return START_REDELIVER_INTENT; // must be restarted if stopped by the system, We must respond to midi events(!)
}

@Override
public void onDestroy() {
super.onDestroy();

midiInputDevices.clear();

midiOutputDevices.clear();

Log.d(Constants.TAG, "MIDI service stopped.");
}

/**
* Set {@link jp.kshoji.blemidi.listener.OnMidiDeviceDetachedListener} to listen MIDI devices have been connected
*
* @param midiDeviceAttachedListener the event listener
*/
public void setOnMidiDeviceAttachedListener(@Nullable OnMidiDeviceAttachedListener midiDeviceAttachedListener) {
this.midiDeviceAttachedListener = midiDeviceAttachedListener;
}

/**
* Set {@link jp.kshoji.blemidi.listener.OnMidiDeviceDetachedListener} to listen MIDI devices have been disconnected
*
* @param midiDeviceDetachedListener the event listener
*/
public void setOnMidiDeviceDetachedListener(@Nullable OnMidiDeviceDetachedListener midiDeviceDetachedListener) {
this.midiDeviceDetachedListener = midiDeviceDetachedListener;
}

/**
* Get {@link java.util.Set} of{@link jp.kshoji.blemidi.device.MidiInputDevice} to send MIDI events.
*
* @return the Set of MidiInputDevice
*/
@NonNull
public Set<MidiInputDevice> getMidiInputDevices() {
return Collections.unmodifiableSet(midiInputDevices);
}

/**
* Get {@link java.util.Set} of{@link jp.kshoji.blemidi.device.MidiOutputDevice} to send MIDI events.
*
* @return the Set of MidiOutputDevice
*/
@NonNull
public Set<MidiOutputDevice> getMidiOutputDevices() {
return Collections.unmodifiableSet(midiOutputDevices);
}

protected OnMidiDeviceAttachedListener serviceMidiDeviceAttachedListener = new OnMidiDeviceAttachedListener() {

@Override
public void onMidiInputDeviceAttached(@NonNull MidiInputDevice midiInputDevice) {
midiInputDevices.add(midiInputDevice);

if (midiDeviceAttachedListener != null) {
midiDeviceAttachedListener.onMidiInputDeviceAttached(midiInputDevice);
}
}

@Override
public void onMidiOutputDeviceAttached(@NonNull MidiOutputDevice midiOutputDevice) {
midiOutputDevices.add(midiOutputDevice);

if (midiDeviceAttachedListener != null) {
midiDeviceAttachedListener.onMidiOutputDeviceAttached(midiOutputDevice);
}
}
};

protected OnMidiDeviceDetachedListener serviceMidiDeviceDetachedListener = new OnMidiDeviceDetachedListener() {

@Override
public void onMidiInputDeviceDetached(@NonNull MidiInputDevice midiInputDevice) {
midiInputDevice.setOnMidiInputEventListener(null);
midiInputDevices.remove(midiInputDevice);

if (midiDeviceDetachedListener != null) {
midiDeviceDetachedListener.onMidiInputDeviceDetached(midiInputDevice);
}
}

@Override
public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevice) {
midiOutputDevices.remove(midiOutputDevice);

if (midiDeviceDetachedListener != null) {
midiDeviceDetachedListener.onMidiOutputDeviceDetached(midiOutputDevice);
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package jp.kshoji.blemidi.service;

import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.NonNull;

import jp.kshoji.blemidi.central.BleMidiCentralProvider;

/**
* Service for BLE MIDI Central
*
* @author K.Shoji
*/
public final class BleMidiCentralService extends AbstractBleMidiService {
private BleMidiCentralProvider midiProvider = null;

/**
* Binder for this Service
*/
public class LocalBinder extends Binder {

/**
* Get the Service
*
* @return the Service
*/
@NonNull
public BleMidiCentralService getService() {
return BleMidiCentralService.this;
}
}

private final IBinder binder = new LocalBinder();

@Override
public IBinder onBind(Intent intent) {
return binder;
}

@Override
protected void onStart() {
midiProvider = new BleMidiCentralProvider(this);
midiProvider.setOnMidiDeviceAttachedListener(serviceMidiDeviceAttachedListener);
midiProvider.setOnMidiDeviceDetachedListener(serviceMidiDeviceDetachedListener);
midiProvider.startScanDevice(0);
}

@Override
public void onDestroy() {
super.onDestroy();

stopScanDevice();
}

/**
* Starts scanning devices
*/
public void startScanDevice() {
if (midiProvider != null) {
midiProvider.startScanDevice(0);
}
}

/**
* Stops scanning devices
*/
public void stopScanDevice() {
if (midiProvider != null) {
midiProvider.stopScanDevice();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package jp.kshoji.blemidi.service;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.NonNull;

import jp.kshoji.blemidi.peripheral.BleMidiPeripheralProvider;

/**
* Service for BLE MIDI Peripheral
*
* @author K.Shoji
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public final class BleMidiPeripheralService extends AbstractBleMidiService {
private BleMidiPeripheralProvider midiProvider = null;

/**
* Binder for this Service
*/
public class LocalBinder extends Binder {

/**
* Get the Service
*
* @return the Service
*/
@NonNull
public BleMidiPeripheralService getService() {
return BleMidiPeripheralService.this;
}
}

private final IBinder binder = new LocalBinder();

@Override
public IBinder onBind(Intent intent) {
return binder;
}

@Override
protected void onStart() {
midiProvider = new BleMidiPeripheralProvider(this);
midiProvider.setOnMidiDeviceAttachedListener(serviceMidiDeviceAttachedListener);
midiProvider.setOnMidiDeviceDetachedListener(serviceMidiDeviceDetachedListener);
}

@Override
public void onDestroy() {
super.onDestroy();

stopAdvertising();
}

/**
* Starts advertising
*/
public void startAdvertising() {
if (midiProvider != null) {
midiProvider.startAdvertising();
}
}

/**
* Stops advertising
*/
public void stopAdvertising() {
if (midiProvider != null) {
midiProvider.stopAdvertising();
}
}
}
Loading

0 comments on commit c473c5f

Please sign in to comment.