BLE-MIDI: change binding for BluetoothMidiService

Add a remote call addBluetoothDevice() using AIDL.
This was needed because onBind() is only called once.

Bug: 23219556
Bug: 23760886
Change-Id: Id7554ca55d596352d11dbd6ae3e403138a29c864
Signed-off-by: Phil Burk <philburk@google.com>
(cherry picked from commit 7cd06c0b9e)
This commit is contained in:
Phil Burk
2015-09-03 14:37:03 -07:00
committed by Glenn Kasten
parent 2e48f1b96e
commit ff23e1b4cb
6 changed files with 60 additions and 12 deletions

View File

@@ -344,6 +344,7 @@ LOCAL_SRC_FILES += \
media/java/android/media/IRingtonePlayer.aidl \
media/java/android/media/IVolumeController.aidl \
media/java/android/media/audiopolicy/IAudioPolicyCallback.aidl \
media/java/android/media/midi/IBluetoothMidiService.aidl \
media/java/android/media/midi/IMidiDeviceListener.aidl \
media/java/android/media/midi/IMidiDeviceOpenCallback.aidl \
media/java/android/media/midi/IMidiDeviceServer.aidl \

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.media.midi;
import android.bluetooth.BluetoothDevice;
import android.os.IBinder;
/** @hide */
interface IBluetoothMidiService
{
IBinder addBluetoothDevice(in BluetoothDevice bluetoothDevice);
}

View File

@@ -3,7 +3,8 @@ include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_SRC_FILES += \
$(call all-java-files-under,src)
LOCAL_PACKAGE_NAME := BluetoothMidiService
LOCAL_CERTIFICATE := platform

View File

@@ -8,7 +8,7 @@
<application
android:label="@string/app_name">
<service android:name="BluetoothMidiService"
<service android:name=".BluetoothMidiService"
android:permission="android.permission.BIND_MIDI_DEVICE_SERVICE">
<intent-filter>
<action android:name="android.media.midi.BluetoothMidiService" />

View File

@@ -19,6 +19,7 @@ package com.android.bluetoothmidiservice;
import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.media.midi.IBluetoothMidiService;
import android.media.midi.MidiManager;
import android.os.IBinder;
import android.util.Log;
@@ -34,25 +35,31 @@ public class BluetoothMidiService extends Service {
@Override
public IBinder onBind(Intent intent) {
if (MidiManager.BLUETOOTH_MIDI_SERVICE_INTENT.equals(intent.getAction())) {
BluetoothDevice bluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("device");
// Return the interface
return mBinder;
}
private final IBluetoothMidiService.Stub mBinder = new IBluetoothMidiService.Stub() {
public IBinder addBluetoothDevice(BluetoothDevice bluetoothDevice) {
BluetoothMidiDevice device;
if (bluetoothDevice == null) {
Log.e(TAG, "no BluetoothDevice in onBind intent");
Log.e(TAG, "no BluetoothDevice in addBluetoothDevice()");
return null;
}
BluetoothMidiDevice device;
synchronized (mDeviceServerMap) {
device = mDeviceServerMap.get(bluetoothDevice);
if (device == null) {
device = new BluetoothMidiDevice(this, bluetoothDevice, this);
device = new BluetoothMidiDevice(BluetoothMidiService.this,
bluetoothDevice, BluetoothMidiService.this);
mDeviceServerMap.put(bluetoothDevice, device);
}
}
return device.getBinder();
}
return null;
}
};
void deviceClosed(BluetoothDevice device) {
synchronized (mDeviceServerMap) {

View File

@@ -27,6 +27,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.res.XmlResourceParser;
import android.media.midi.IBluetoothMidiService;
import android.media.midi.IMidiDeviceListener;
import android.media.midi.IMidiDeviceOpenCallback;
import android.media.midi.IMidiDeviceServer;
@@ -394,7 +395,20 @@ public class MidiService extends IMidiManager.Stub {
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMidiDeviceServer server = IMidiDeviceServer.Stub.asInterface(service);
IMidiDeviceServer server = null;
if (mBluetoothDevice != null) {
IBluetoothMidiService mBluetoothMidiService = IBluetoothMidiService.Stub.asInterface(service);
try {
// We need to explicitly add the device in a separate method
// because onBind() is only called once.
IBinder deviceBinder = mBluetoothMidiService.addBluetoothDevice(mBluetoothDevice);
server = IMidiDeviceServer.Stub.asInterface(deviceBinder);
} catch(RemoteException e) {
Log.e(TAG, "Could not call addBluetoothDevice()", e);
}
} else {
server = IMidiDeviceServer.Stub.asInterface(service);
}
setDeviceServer(server);
}
@@ -411,7 +425,6 @@ public class MidiService extends IMidiManager.Stub {
intent.setComponent(new ComponentName(
MidiManager.BLUETOOTH_MIDI_SERVICE_PACKAGE,
MidiManager.BLUETOOTH_MIDI_SERVICE_CLASS));
intent.putExtra("device", mBluetoothDevice);
} else {
intent = new Intent(MidiDeviceService.SERVICE_INTERFACE);
intent.setComponent(