Android Tutorial Android Bluetooth / BluetoothAdmin Tutorial

Status
Not open for further replies.

red30

Active Member
Licensed User
I managed to get the data but when i send data from the terminal, nothing comes. But if i send data from micricontroller via bluetooth, the data comes with this error. Moreover, [00 46 00 0b ff ff 00 00] must come but only [46 00 0b ff ff 00 00] comes. Where is the first bite?
 

Attachments

Erel

Administrator
Staff member
Licensed User
Please start a new thread for this question in the questions forum.
 

padvou

Active Member
Licensed User
Thank you for this interesting post.
I try to use this code instead of having the user choose a device everytime. The devices are already paired in the OS settings menu.
But The serial1.connected returns false. Any ideas please?
B4X:
Sub StartConnection
   
    Dim PairedDevices As Map
    Dim L As List
    Dim Res As Int

    ToastMessageShow("Connecting.....",True)

    PairedDevices.Initialize
   
    Try
        PairedDevices = serial1.GetPairedDevices
    Catch
   
        serial1.Disconnect
    End Try

    If PairedDevices.Size = 0 Then
        Msgbox("Error connecting to device. Device not found","")
       
        Return
    End If

    If PairedDevices.Size = 1 Then
        Try
           
            Log(PairedDevices.Get(PairedDevices.GetKeyAt(0)))
            serial1.Connect(PairedDevices.Get(PairedDevices.GetKeyAt(0)))
            'serial1.ConnectInsecure(admin,PairedDevices.Get(PairedDevices.GetKeyAt(0)),1)
        Catch
       
            Msgbox("Connecting","Device error")
           
            serial1.Disconnect
           
        End Try
    Else
        L.Initialize

        For i = 0 To PairedDevices.Size - 1
            L.Add(PairedDevices.GetKeyAt(i))
        Next

        Res = InputList(L, "Choose device", -1)

        If Res <> DialogResponse.CANCEL Then
            serial1.Connect(PairedDevices.Get(L.Get(Res)))
            Log(PairedDevices.Get(L.Get(Res)))
        End If
    End If   
   
End Sub
 

red30

Active Member
Licensed User
The search of bluetooth devices is a very long (about 1-2 minutes) on some devices, and when it is over, I choose the device and it gives me an error - as in the picture below. Why is this happening? How to fix it?
 

Attachments

Erel

Administrator
Staff member
Licensed User
Are you running the example from the first post on both devices?
 

red30

Active Member
Licensed User
Not exactly, I took a programm from the first post as an example but used the tablet connected with bluetooth module HC-05(which is connected to microcontroller) instead of two android devices.
 

red30

Active Member
Licensed User
dear friends,
I got the example from the first post.
When I write
B4X:
serial1.Connect(connectedDevice.Mac)
- it always works truly( with all devises).
When I write
B4X:
serial1.ConnectInsecure(admin,connectedDevice.Mac,1)
- it works only with some devices. Note - usually when the search is long (~1 min) the result is fail and it errors 01.png. Why is it so?

What is the difference between these three lines?
B4X:
serial1.ConnectInsecure(admin,connectedDevice.Mac,1)
serial1.Connect3(connectedDevice.Mac,1)
serial1.Connect(connectedDevice.Mac)
What is the difference between these two lines
B4X:
AStreams.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
Sorry for stupid questions :(
 

red30

Active Member
Licensed User
At various devices search time "Device Bluetooth" different - why?
Can I change it?
 

Erel

Administrator
Staff member
Licensed User
Do you mean that it takes longer to find other devices? You cannot control it.
 

red30

Active Member
Licensed User
I meant: Why does search last not equally on different android devices? What determines this time?
 

Erel

Administrator
Staff member
Licensed User
I'm not sure. It is handled by the OS.

However it doesn't really matter. You don't need to wait for the discovery process to complete. Once a device is found you can cancel the discovery process.
 

red30

Active Member
Licensed User
I run a default search for Bluetooth devices on the android-device. It finds one unit - "zebra" (this is my devise). Screenshot_2017-03-03-21-13-02[1].png When I run a search for devices in the program BluetoothAdmin Tutorial - it finds many (often more than 15) devices (but in fact this is only one device). Screenshot_2017-03-03-21-12-24[1].png Such a bug occurs only on devices with Spreadtrum CPU. Why? How to fix?
 

Erel

Administrator
Staff member
Licensed User
Is the DeviceFound event raised 15 times? Are you running the code exactly as is?
 

red30

Active Member
Licensed User
It raises many times (4 or 8 or 9 or 15 or 23 etc.) Every time randomly. Screenshot_2017-03-05-17-14-26[1].png Screenshot_2017-03-05-17-13-36[1].png
Yes I use your programm from the first post. Screenshot_2017-03-05-17-12-40[1].png
Why so?
 
Last edited:

Erel

Administrator
Staff member
Licensed User
It indeed looks like an OS bug. You can add the found devices to a Map with the MacAddress as the key to remove all duplicates.
 

Computersmith64

Well-Known Member
Licensed User
It indeed looks like an OS bug. You can add the found devices to a Map with the MacAddress as the key to remove all duplicates.
I've noticed that issue too. The discovery will sometimes randomly list the same found device multiple times (although in my case I've only ever seen a device listed two or three times), then at other times will only list them once. Given that I've only ever seen a max of 3 instances of the same device I've never really worried about it, but I might try your suggestion.

I'm pretty sure I only ever see it after running the discovery more than once, so I wonder if the OS is caching the found devices list or something?

- Colin.
 

Erel

Administrator
Staff member
Licensed User
You should make sure that BluetoothAdmin is initialized exactly once and is a process global variable.
 
Status
Not open for further replies.
Top