ChannelClient WearableListenerService ApiException 13 on getInputStream

Viewed 91

I am sending multiple files from wearable to mobile using ChannelClient. The files are sent and read using getOutputStream and getInputStream. A WearableListenerService is implemented on the mobile side with onChannelOpened and onChannelClosed.

The first execution of onChannelOpened is executed correctly. The channel is closed afterward. However, when the wearable opens a new channel and sends the second file, the second execution of onChannelOpened crashes the app as this exception is thrown.

    2022-04-22 06:21:58.882 22855-22887/com.example.pg_android_frontend E/AndroidRuntime: FATAL EXCEPTION: WearableListenerService
    Process: com.example.pg_android_frontend, PID: 22855
    java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 13: 
        at com.google.android.gms.tasks.Tasks.zzb(Unknown Source:61)
        at com.google.android.gms.tasks.Tasks.await(Unknown Source:19)
        at com.example.pg_android_frontend.DataLayerListenerService.onChannelOpened(DataLayerListenerService.kt:23)
        at com.google.android.gms.wearable.WearableListenerService$zza.onChannelOpened(Unknown Source:2)
        at com.google.android.gms.wearable.internal.zzas.onChannelOpened(Unknown Source:4)
        at com.google.android.gms.wearable.internal.zzaw.zza(Unknown Source:8)
        at com.google.android.gms.wearable.zzt.run(Unknown Source:3)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.android.gms.wearable.WearableListenerService$zzc.dispatchMessage(Unknown Source:6)
        at android.os.Looper.loop(Looper.java:223)
        at android.os.HandlerThread.run(HandlerThread.java:67)
     Caused by: com.google.android.gms.common.api.ApiException: 13: 
        at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source:4)
        at com.google.android.gms.common.internal.zai.zaf(Unknown Source:2)
        at com.google.android.gms.common.internal.zaj.onComplete(Unknown Source:6)
        at com.google.android.gms.common.api.internal.BasePendingResult.zaa(Unknown Source:172)
        at com.google.android.gms.common.api.internal.BasePendingResult.setResult(Unknown Source:131)
        at com.google.android.gms.common.api.internal.BaseImplementation$ApiMethodImpl.setResult(Unknown Source:36)
        at com.google.android.gms.wearable.internal.zzgm.zza(Unknown Source:6)
        at com.google.android.gms.wearable.internal.zzgs.zza(Unknown Source:10)
        at com.google.android.gms.wearable.internal.zzel.dispatchTransaction(Unknown Source:53)
        at com.google.android.gms.internal.wearable.zzb.onTransact(Unknown Source:12)
        at android.os.Binder.execTransactInternal(Binder.java:1154)
        at android.os.Binder.execTransact(Binder.java:1123)

My onChannelOpened and onChannelClosed listener, exception is thrown at comment:

override fun onChannelOpened(channel: ChannelClient.Channel?) {
    super.onChannelOpened(channel)
    if (channel != null) {
        var channelClient = Wearable.getChannelClient(this)
        var inputStream = Tasks.await(channelClient.getInputStream(channel)) **//EXCEPTION HERE**

        val reader = BufferedReader(inputStream.reader())
        var content: String
        try {
            content = reader.readText()
        } finally {
            reader.close()
        }
        inputStream.close()  
    }
}

override fun onInputClosed(channel: ChannelClient.Channel, i: Int, i1: Int) {
    super.onInputClosed(channel, i, i1)
    Wearable.getChannelClient(this).close(channel)
}

And the code used to open the channel and send the data from wearable to mobile:

private fun sendData (context: Context?) {
    Thread {
        var channelClient = Wearable.getChannelClient(context)
        var channel = Tasks.await(channelClient.openChannel(nodeId, path))
        var outputStream = Tasks.await(channelClient.getOutputStream(channel))

        val data = "large amt of data"
                    
        outputStream.write(data)
        outputStream.flush()
        outputStream.close()
     }.start()
}                 
0 Answers
Related