Loading android/pandora/server/src/com/android/pandora/A2dp.kt +17 −19 Original line number Diff line number Diff line Loading @@ -28,6 +28,8 @@ import android.util.Log import io.grpc.Status import io.grpc.stub.StreamObserver import java.io.Closeable import java.io.PrintWriter import java.io.StringWriter import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel Loading Loading @@ -91,8 +93,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { .first() if (state == BluetoothProfile.STATE_DISCONNECTED) { Log.e(TAG, "openSource failed, A2DP has been disconnected") throw Status.UNKNOWN.asException() throw RuntimeException("openSource failed, A2DP has been disconnected") } } Loading Loading @@ -124,8 +125,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { .first() if (state == BluetoothProfile.STATE_DISCONNECTED) { Log.e(TAG, "waitSource failed, A2DP has been disconnected") throw Status.UNKNOWN.asException() throw RuntimeException("waitSource failed, A2DP has been disconnected") } } Loading @@ -146,8 +146,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "start: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot start") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot start") } audioTrack!!.play() Loading @@ -171,13 +170,11 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "suspend: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot suspend") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot suspend") } if (!bluetoothA2dp.isA2dpPlaying(device)) { Log.e(TAG, "Device is already suspended, cannot suspend") throw Status.UNKNOWN.asException() throw RuntimeException("Device is already suspended, cannot suspend") } val a2dpPlayingStateFlow = Loading @@ -201,8 +198,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "isSuspended: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot get suspend state") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot get suspend state") } val isSuspended = bluetoothA2dp.isA2dpPlaying(device) Loading @@ -216,8 +212,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "close: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot close") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot close") } val a2dpConnectionStateChangedFlow = Loading Loading @@ -270,9 +265,13 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { ) } } override fun onError(t: Throwable?) { Log.e(TAG, t.toString()) responseObserver.onError(t) override fun onError(t: Throwable) { t.printStackTrace() val sw = StringWriter() t.printStackTrace(PrintWriter(sw)) responseObserver.onError( Status.UNKNOWN.withCause(t).withDescription(sw.toString()).asException() ) } override fun onCompleted() { responseObserver.onNext(PlaybackAudioResponse.getDefaultInstance()) Loading @@ -290,8 +289,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "getAudioEncoding: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot getAudioEncoding") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot getAudioEncoding") } // For now, we only support 44100 kHz sampling rate. Loading android/pandora/server/src/com/android/pandora/A2dpSink.kt +2 −5 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ import android.content.Intent import android.content.IntentFilter import android.media.* import android.util.Log import io.grpc.Status import io.grpc.stub.StreamObserver import java.io.Closeable import kotlinx.coroutines.CoroutineScope Loading Loading @@ -85,8 +84,7 @@ class A2dpSink(val context: Context) : A2DPImplBase(), Closeable { .first() if (state == BluetoothProfile.STATE_DISCONNECTED) { Log.e(TAG, "waitStream failed, A2DP has been disconnected") throw Status.UNKNOWN.asException() throw RuntimeException("waitStream failed, A2DP has been disconnected") } } Loading @@ -100,8 +98,7 @@ class A2dpSink(val context: Context) : A2DPImplBase(), Closeable { val device = request.sink.connection.toBluetoothDevice(bluetoothAdapter) Log.i(TAG, "close: device=$device") if (bluetoothA2dpSink.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot close") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot close") } val a2dpConnectionStateChangedFlow = Loading android/pandora/server/src/com/android/pandora/Gatt.kt +2 −5 Original line number Diff line number Diff line Loading @@ -26,7 +26,6 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.util.Log import io.grpc.Status import io.grpc.stub.StreamObserver import java.io.Closeable import java.util.UUID Loading Loading @@ -77,8 +76,7 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable { val mtu = request.mtu Log.i(TAG, "exchangeMTU MTU=$mtu") if (!GattInstance.get(request.connection.address).mGatt.requestMtu(mtu)) { Log.e(TAG, "Error on requesting MTU $mtu") throw Status.UNKNOWN.asException() throw RuntimeException("Error on requesting MTU $mtu") } ExchangeMTUResponse.newBuilder().build() } Loading Loading @@ -383,8 +381,7 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable { private suspend fun tryDiscoverServices(gattInstance: GattInstance) { if (!gattInstance.servicesDiscovered() && !gattInstance.mGatt.discoverServices()) { Log.e(TAG, "Error on discovering services for $gattInstance") throw Status.UNKNOWN.asException() throw RuntimeException("Error on discovering services for $gattInstance") } else { gattInstance.waitForDiscoveryEnd() } Loading android/pandora/server/src/com/android/pandora/GattInstance.kt +4 −13 Original line number Diff line number Diff line Loading @@ -56,11 +56,7 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte ) {} private var mGattInstanceValuesRead = arrayListOf<GattInstanceValueRead>() class GattInstanceValueWrote( var uuid: UUID?, var handle: Int, var status: AttStatusCode ) {} class GattInstanceValueWrote(var uuid: UUID?, var handle: Int, var status: AttStatusCode) {} private var mGattInstanceValueWrote = GattInstanceValueWrote(null, 0, AttStatusCode.UNKNOWN_ERROR) companion object GattManager { Loading Loading @@ -320,7 +316,8 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte characteristic.getInstanceId(), AttStatusCode.UNKNOWN_ERROR ) if (mGatt.writeCharacteristic( if ( mGatt.writeCharacteristic( characteristic, value, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT Loading @@ -329,7 +326,6 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte waitForWriteEnd() } return mGattInstanceValueWrote } public suspend fun writeDescriptorBlocking( Loading @@ -341,15 +337,10 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte descriptor.getInstanceId(), AttStatusCode.UNKNOWN_ERROR ) if (mGatt.writeDescriptor( descriptor, value ) == BluetoothStatusCodes.SUCCESS ) { if (mGatt.writeDescriptor(descriptor, value) == BluetoothStatusCodes.SUCCESS) { waitForWriteEnd() } return mGattInstanceValueWrote } public fun disconnectInstance() { Loading android/pandora/server/src/com/android/pandora/GattServerManager.kt +7 −1 Original line number Diff line number Diff line Loading @@ -68,7 +68,13 @@ class GattServerManager( ByteArray(negociatedMtu) ) } else { server.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, ByteArray(512 - offset)) server.sendResponse( device, requestId, BluetoothGatt.GATT_SUCCESS, offset, ByteArray(512 - offset) ) } } Loading Loading
android/pandora/server/src/com/android/pandora/A2dp.kt +17 −19 Original line number Diff line number Diff line Loading @@ -28,6 +28,8 @@ import android.util.Log import io.grpc.Status import io.grpc.stub.StreamObserver import java.io.Closeable import java.io.PrintWriter import java.io.StringWriter import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel Loading Loading @@ -91,8 +93,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { .first() if (state == BluetoothProfile.STATE_DISCONNECTED) { Log.e(TAG, "openSource failed, A2DP has been disconnected") throw Status.UNKNOWN.asException() throw RuntimeException("openSource failed, A2DP has been disconnected") } } Loading Loading @@ -124,8 +125,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { .first() if (state == BluetoothProfile.STATE_DISCONNECTED) { Log.e(TAG, "waitSource failed, A2DP has been disconnected") throw Status.UNKNOWN.asException() throw RuntimeException("waitSource failed, A2DP has been disconnected") } } Loading @@ -146,8 +146,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "start: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot start") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot start") } audioTrack!!.play() Loading @@ -171,13 +170,11 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "suspend: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot suspend") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot suspend") } if (!bluetoothA2dp.isA2dpPlaying(device)) { Log.e(TAG, "Device is already suspended, cannot suspend") throw Status.UNKNOWN.asException() throw RuntimeException("Device is already suspended, cannot suspend") } val a2dpPlayingStateFlow = Loading @@ -201,8 +198,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "isSuspended: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot get suspend state") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot get suspend state") } val isSuspended = bluetoothA2dp.isA2dpPlaying(device) Loading @@ -216,8 +212,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "close: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot close") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot close") } val a2dpConnectionStateChangedFlow = Loading Loading @@ -270,9 +265,13 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { ) } } override fun onError(t: Throwable?) { Log.e(TAG, t.toString()) responseObserver.onError(t) override fun onError(t: Throwable) { t.printStackTrace() val sw = StringWriter() t.printStackTrace(PrintWriter(sw)) responseObserver.onError( Status.UNKNOWN.withCause(t).withDescription(sw.toString()).asException() ) } override fun onCompleted() { responseObserver.onNext(PlaybackAudioResponse.getDefaultInstance()) Loading @@ -290,8 +289,7 @@ class A2dp(val context: Context) : A2DPImplBase(), Closeable { Log.i(TAG, "getAudioEncoding: device=$device") if (bluetoothA2dp.getConnectionState(device) != BluetoothA2dp.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot getAudioEncoding") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot getAudioEncoding") } // For now, we only support 44100 kHz sampling rate. Loading
android/pandora/server/src/com/android/pandora/A2dpSink.kt +2 −5 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ import android.content.Intent import android.content.IntentFilter import android.media.* import android.util.Log import io.grpc.Status import io.grpc.stub.StreamObserver import java.io.Closeable import kotlinx.coroutines.CoroutineScope Loading Loading @@ -85,8 +84,7 @@ class A2dpSink(val context: Context) : A2DPImplBase(), Closeable { .first() if (state == BluetoothProfile.STATE_DISCONNECTED) { Log.e(TAG, "waitStream failed, A2DP has been disconnected") throw Status.UNKNOWN.asException() throw RuntimeException("waitStream failed, A2DP has been disconnected") } } Loading @@ -100,8 +98,7 @@ class A2dpSink(val context: Context) : A2DPImplBase(), Closeable { val device = request.sink.connection.toBluetoothDevice(bluetoothAdapter) Log.i(TAG, "close: device=$device") if (bluetoothA2dpSink.getConnectionState(device) != BluetoothProfile.STATE_CONNECTED) { Log.e(TAG, "Device is not connected, cannot close") throw Status.UNKNOWN.asException() throw RuntimeException("Device is not connected, cannot close") } val a2dpConnectionStateChangedFlow = Loading
android/pandora/server/src/com/android/pandora/Gatt.kt +2 −5 Original line number Diff line number Diff line Loading @@ -26,7 +26,6 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.util.Log import io.grpc.Status import io.grpc.stub.StreamObserver import java.io.Closeable import java.util.UUID Loading Loading @@ -77,8 +76,7 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable { val mtu = request.mtu Log.i(TAG, "exchangeMTU MTU=$mtu") if (!GattInstance.get(request.connection.address).mGatt.requestMtu(mtu)) { Log.e(TAG, "Error on requesting MTU $mtu") throw Status.UNKNOWN.asException() throw RuntimeException("Error on requesting MTU $mtu") } ExchangeMTUResponse.newBuilder().build() } Loading Loading @@ -383,8 +381,7 @@ class Gatt(private val context: Context) : GATTImplBase(), Closeable { private suspend fun tryDiscoverServices(gattInstance: GattInstance) { if (!gattInstance.servicesDiscovered() && !gattInstance.mGatt.discoverServices()) { Log.e(TAG, "Error on discovering services for $gattInstance") throw Status.UNKNOWN.asException() throw RuntimeException("Error on discovering services for $gattInstance") } else { gattInstance.waitForDiscoveryEnd() } Loading
android/pandora/server/src/com/android/pandora/GattInstance.kt +4 −13 Original line number Diff line number Diff line Loading @@ -56,11 +56,7 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte ) {} private var mGattInstanceValuesRead = arrayListOf<GattInstanceValueRead>() class GattInstanceValueWrote( var uuid: UUID?, var handle: Int, var status: AttStatusCode ) {} class GattInstanceValueWrote(var uuid: UUID?, var handle: Int, var status: AttStatusCode) {} private var mGattInstanceValueWrote = GattInstanceValueWrote(null, 0, AttStatusCode.UNKNOWN_ERROR) companion object GattManager { Loading Loading @@ -320,7 +316,8 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte characteristic.getInstanceId(), AttStatusCode.UNKNOWN_ERROR ) if (mGatt.writeCharacteristic( if ( mGatt.writeCharacteristic( characteristic, value, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT Loading @@ -329,7 +326,6 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte waitForWriteEnd() } return mGattInstanceValueWrote } public suspend fun writeDescriptorBlocking( Loading @@ -341,15 +337,10 @@ class GattInstance(val mDevice: BluetoothDevice, val mTransport: Int, val mConte descriptor.getInstanceId(), AttStatusCode.UNKNOWN_ERROR ) if (mGatt.writeDescriptor( descriptor, value ) == BluetoothStatusCodes.SUCCESS ) { if (mGatt.writeDescriptor(descriptor, value) == BluetoothStatusCodes.SUCCESS) { waitForWriteEnd() } return mGattInstanceValueWrote } public fun disconnectInstance() { Loading
android/pandora/server/src/com/android/pandora/GattServerManager.kt +7 −1 Original line number Diff line number Diff line Loading @@ -68,7 +68,13 @@ class GattServerManager( ByteArray(negociatedMtu) ) } else { server.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, ByteArray(512 - offset)) server.sendResponse( device, requestId, BluetoothGatt.GATT_SUCCESS, offset, ByteArray(512 - offset) ) } } Loading