Android Question Bluetooth Listening ......java.io.IOException: Bad file number

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi all

java.io.IOException: Bad file number appeared in the following function.

B4X:
Sub com_listen
   
   Dim d As Long

   ' // delay here for 1.5 seconds
   d = DateTime.Add(DateTime.Now + (DateTime.TicksPerSecond  * 1.5),0,0,0)
   
   Do While d > DateTime.Now
     DoEvents
   Loop
   
   Try
     If Not(btCom.IsEnabled) Then
       Log("com_listen() initialised")
       btCom.Initialize("btCom")
     End If
     
     btCom.Listen
     bCOMListening = True
     Log(DateTime.time(DateTime.Now) & " Listening for Connection")
   Catch
     Log(DateTime.time(DateTime.Now) & " com_listen: " & LastException.Message)
     StopService("")
   End Try
   
End Sub

Any reason why this would happen. I have attached the log data from the app to show how long it took before this occurred, it then produced java.io.IOException: Out of memory error

Regards

John
 

Attachments

  • bt_errorlog_listen.txt
    22.9 KB · Views: 242

Jmu5667

Well-Known Member
Licensed User
Longtime User
the delay is legacy code, I have removed it, and also the try catch block, will run tests and upload results as soon as I have them.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Here you go Erel, if you need a more detailed explanation on what we are doing please ask.
 

Attachments

  • bt_errorlog_listen2.txt
    13.9 KB · Views: 253
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
This is the tmr code;

B4X:
Sub tmrServiceHook_tick
   
   ' // turn off timer
   tmrServiceHook.Enabled = False
   
   Try
     ' // bluetooth is not enabled
     If Not(btAdmin.IsEnabled) Then
       If Not(btStarting) Then
         ToastMessageShow("Switching on Bluetooth",False)
         btStarting = True
         shutDownBT = True
         ' // start bluetooth
         btAdmin.Enable
       End If
     Else
       ' // do main processing
       btStarting = False
       do_com_service
     End If
   Catch
     ' // disconnect
     ToastMessageShow("tmrServiceHook_tick() " & LastException.Message,False)
     com_disconnect
     com_listen
   End Try
   
   ' // turn on timer
   tmrServiceHook.Enabled = True

End Sub
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
I have an app on the PC. It sends data to the phone. The app can close the connection after receiving an acknowledge from the phone that the data was received. It then waits for 5 seconds before sending the data again. The problem only seems to occur when the PC closes the connection. I am currently running test where it does not close the connection ans send the data every 5 seconds. The non closure method seems to work ok.

The purpose of the PC app is to test the phone app. We have developed a bluetooth device www.ursosbutton.com and need to test the integrity of the phone app due to the nature of our device.

Make sense ?

So far the non closure method has been running for 5+ minutes without fault.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Update:

The PC app is sending a packet every 30 seconds and not closing the connection. The phone app is closing the connection after 10 seconds as no data is received. This seems to work fine as it has been running for 20+ mins.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Update:

I seemed to discovered something. http://stackoverflow.com/questions/3031796/disconnect-a-bluetooth-socket-in-android.
My solution to this problem is to reset the adapter if I get and error on disconnect/listen/write.

B4X:
Sub com_listen
  
   Try
     If Not(btCom.IsEnabled) Then
       Log("com_listen() initialised")
       btCom.Initialize("btCom")
     End If
     
     btCom.Listen
     bCOMListening = True
     Log(DateTime.time(DateTime.Now) & " Listening for Connection")
   Catch
     Log(DateTime.time(DateTime.Now) & " com_listen: " & LastException.Message)
     btAdmin.Disable
     StopService("")
   End Try
   
End Sub

I have a global service that will restart the serial service, that how it keeps going.

Regards

John.
 
Upvote 0
Top