Android Question Bluetooth won't connect

saunwin

Active Member
Licensed User
Longtime User
Hi Folks, My tablet won't connect to my HC-06 module. I've re-booted both ends (many times) and my error is java.io.IOException: read failed, socket might closed or timeout, read ret: -1. Any ideas appreciated. TIA
 

saunwin

Active Member
Licensed User
Longtime User
I suspected the same Martin, but I'm experimenting here with several PSU's and batteries - I thought it was a power problem, but I'm not so sure now. The board can now pull lots of power (well the 3v3 reg is 800ma), but doesn't seem to make any difference. Besides I had this working beautifully before I started to tinker with it (Changed to the new BT serial example from the old one).
 
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
Thanks Erel. I hadn't seen that example. But it must be something I've done because it's been working fine using THIS example. I had some changes to make so I changed to THIS one and I've broken it :(. It only pairs 1 in ten times. I have changed the HC06 module, so maybe I have a duff one - I'll try that later.
 
Upvote 0

MartinR

Member
Licensed User
I think there is a significant difference between your first and second 'THIS's, in that the second uses prefix-mode streams. (In prefix mode you have to send the length of the message first, as a four-byte integer, most significant byte first for 'bigendian', then the message itself.)

But I'm not clear whether the problem you are having is with pairing, connecting, or exchanging messages.

Does the tablet manage to pair with the HC-06? Try getting the tablet to forget the HC-06, then try to re-pair. If successful the HC-06 itself is probably OK

Does the LED on the HC-06 flash on-off-on-off...? It should come on permanently when it connects to some other BT device.

The Bluetooth code has a line commented out to initialise the async stream non-prefix mode. Try un-commenting it (and commenting out the line that initialises in prefix mode) and see if you get any further.
 
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
Thanks for the reply Martin. I'm NOT using prefix mode (I'm only sending a single byte periodically). The tablet will pair, but won't connect (i.e. it see's the HC-06 and puts it in the list), It won't connect (well 9 times out of ten it won't connect). The HC-06 LED goes solid once connected (rare event !), it just sits there flashing when trying to connect.
 
Upvote 0

MartinR

Member
Licensed User
it just sits there flashing when trying to connect.
Familiar, frustrating scenario!

The code I use (you will recognise a lot of it from other threads) has the following:
B4X:
Sub btnOpenComms_Click
  Dim PairedDevices As Map
    PairedDevices=serial1.GetPairedDevices
    Dim L As List
    L.Initialize
    For i = 0 To PairedDevices.Size-1
        L.Add(PairedDevices.GetKeyAt(i))
    Next
    Dim res As Int
    res = InputList(L,"Choose device",-1)
    If res <> DialogResponse.CANCEL Then
        serial1.Connect(PairedDevices.Get(L.Get(res)))
    End If
    ProgressDialogShow("Please wait while establishing Bluetooth communications...")
End Sub

Sub serial1_connected(success As Boolean)
    ProgressDialogHide
    If success Then
        Msgbox("Bluetooth connection established OK.","Bluetooth")
        DoEvents    'execute the previous instruction!
    Else
        Label1.Text="Failed to establish Bluetooth connection."
    End If
End Sub

btnOpenComms is obviously a button to open the communications with the HC-06. Serial1 is declared as Serial in the Process_Globals sub, and initialised with "Serial1" as the event name in Acitivity_Create. I'm not sure now whether the (deprecated) DoEvents line is needed. I must have put it in for a reason...

This works fairly reliably, but just occasionally the HC-06 won't play ball and I have to turn it off and on again, even though it is flashing away apparently ready to connect.
 
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
Hello Martin,

Many thanks for the code. Yes it is very similar to Erel's example that I've been using. Last night I was getting 90% connectivity (from 10% the previous evening using the same code) - Go figure. Anyway, I've just copy/pasted your routine above and all seems well, so I'm a happy bunny. Again Many thanks Martin.
 
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
I think I've got to the bottom of this.
I have three tablets. a Blaupunkt and two generic tablets of different sizes (Both rockwell chip based). One of the (R/well Rd30sdk) tablets (an old 9.7") works fine. The other two are flaky when connecting to BT. No idea why, probably the BT chipsets used. Another reason to go BLE I guess. BTW - the latest HC-06 devices I got from china (ebay) do not have the LED fitted - So a bit of a ball ache when fault finding.

Footnote - I also suspect the 2 tablets that are flaky also have BTLE onboard and is probably something to do with it.
 
Last edited:
Upvote 0
Top