Android Question IOexception when bluetooth Listen called

Arf

Well-Known Member
Licensed User
Longtime User
I'm getting an IOexception when setting bluetooth to listen on my android 4.4.4 tablet.
When my app starts, bluetooth admin gets enabled, and Listen is called only when the Admin_StateChanged function has a new state of STATE_ON.

when I start listening at that point, I get:

** Activity (main) Resume **
** Service (unit_comms) Create **
** Service (unit_comms) Start **
admin statechange. old = STATE_ON new = STATE_TURNING_OFF
admin statechange. old = STATE_TURNING_OFF new = STATE_ON
StartToListen
Error occurred on line: 0 (Unit_Comms)
java.io.IOException: Error: -1
at android.bluetooth.BluetoothAdapter.createNewRfcommSocketAndRecord(BluetoothAdapter.java:1240)
at android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:1156)
at anywheresoftware.b4a.objects.Serial.Listen2(Serial.java:233)
at anywheresoftware.b4a.objects.Serial.Listen(Serial.java:221)
at com.medchipsolutions.spiroconnectmobile.unit_comms._startlistening(unit_comms.java:316)
at com.medchipsolutions.spiroconnectmobile.unit_comms._admin_statechanged(unit_comms.java:692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.Serial$BluetoothAdmin$1$1.run(Serial.java:343)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Pause, UserClosed = true **
** Service (unit_comms) Destroy **


Any ideas? Or how can I get more info on this error?
 

Arf

Well-Known Member
Licensed User
Longtime User
Mmmm, seems to have cleared itself after a tablet reboot.
I do remember facing a similar issue ages ago, bluetooth adaptor seems to get into a funk and a reboot was the only solution I found.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Aaaaaaargh it's doing it again.. Need to figure out what causes the socket to be inaccesible.
Is there any deeper level of log messaging that I can access somehow?
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Aaaaaaargh it's doing it again.. Need to figure out what causes the socket to be inaccesible.
Is there any deeper level of log messaging that I can access somehow?
uncheck "Filter" in Log Window
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Ok so, looking at my logs from Admin_StateChange,I can see that my app behaves perfectly fine when I encounter the expected chain of events when I restart the bluetooth module. This is the expected chain of events I usually see:

** Service (unit_comms) Start **
admin statechange. old = STATE_ON new = STATE_TURNING_OFF
admin statechange. old = STATE_TURNING_OFF new = STATE_OFF
admin statechange. old = STATE_OFF new = STATE_TURNING_ON
admin statechange. old = STATE_TURNING_ON new = STATE_ON
StartListen2
listening
connected: true

and, I noticed that there is a deviation from this just before my listen call creates an exception, in this case, the states encountered in Admin_StateChange go like this:

** Service (unit_comms) Start **
admin statechange. old = STATE_ON new = STATE_TURNING_OFF
admin statechange. old = STATE_TURNING_OFF new = STATE_ON
StartListen2
listening failed

So the state goes from STATE_TURNING OFF straight to STATE_ON when the problem occurs, whereas usually, it goes from TURNING_ON to STATE_ON.
Why's it doing that?!?

I tried to change my code so that the trigger to listen is 'oldstate = TURNING_ON AND newstate = STATE_ON' instead of merely 'newstate = STATE_ON', but then it never listens when it does the TURNING_OFF to STATE_ON thing as that mix of states does not occur.

Any help graciously received!
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
I have a TryCatch around serial.listen and I do automatically reset the bluetooth when this situation occurs, however the bluetooth reset doesn't seem to clear the condition, so it goes into an endless loop of doing the same thing. Only restarting the tablet seems to clear it, even turning BT off and on in Settings doesn't clear it.

I am doing some further experimentation with it. This tablet is a Nexus 7 - the earliest version and was on droind 4.2.2 or something when I got it. I upgraded it to droid 5, and it as unbearably slow, so I ended up flashing it with "nakasi-ktu84p-factory-9482e892".

I wonder if maybe the original OS was not the same, and this bluetooth behaviour has something to do with that?
 
Upvote 0

Similar Threads

Top