Android Tutorial Android Bluetooth / BluetoothAdmin Tutorial

Discussion in 'Tutorials & Examples' started by Erel, Feb 7, 2012.

Thread Status:
Not open for further replies.
  1. red30

    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?
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question in the questions forum.
     
  3. padvou

    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?
    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this in the questions forum.
     
  5. red30

    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?
     

    Attached Files:

    • 01.png
      01.png
      File size:
      21.3 KB
      Views:
      200
    • 02.jpg
      02.jpg
      File size:
      23.7 KB
      Views:
      165
  6. Erel

    Erel Administrator Staff Member Licensed User

    Are you running the example from the first post on both devices?
     
  7. red30

    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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for your question.
     
  9. red30

    red30 Active Member Licensed User

    dear friends,
    I got the example from the first post.
    When I write
    Code:
    serial1.Connect(connectedDevice.Mac)
    - it always works truly( with all devises).
    When I write
    Code:
    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?
    Code:
    serial1.ConnectInsecure(admin,connectedDevice.Mac,1)
    serial1.Connect3(connectedDevice.Mac,
    1)
    serial1.Connect(connectedDevice.Mac)
    What is the difference between these two lines
    Code:
    AStreams.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
    AStream.InitializePrefix(Main.serial1.InputStream, 
    True, Main.serial1.OutputStream, "AStream")
    Sorry for stupid questions :(
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You should use Connect. ConnectInsecure is a "workaround method" which is less relevant for newer devices.

    Please start a new thread for the other questions (make sure to go over AsyncStreams tutorial: http://www.b4x.com/search?query=AsyncStreams)
     
    red30 likes this.
  11. red30

    red30 Active Member Licensed User

    At various devices search time "Device Bluetooth" different - why?
    Can I change it?
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Do you mean that it takes longer to find other devices? You cannot control it.
     
    red30 likes this.
  13. red30

    red30 Active Member Licensed User

    I meant: Why does search last not equally on different android devices? What determines this time?
     
  14. Erel

    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 likes this.
  15. red30

    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?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    Is the DeviceFound event raised 15 times? Are you running the code exactly as is?
     
  17. red30

    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: Mar 5, 2017
  18. Erel

    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.
     
    red30 likes this.
  19. Computersmith64

    Computersmith64 Well-Known Member Licensed User

    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.
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    You should make sure that BluetoothAdmin is initialized exactly once and is a process global variable.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice