B4A Library BtAutoPair - Programmatic Bluetooth Device Bonding

Discussion in 'Additional libraries, classes and official updates' started by leongcc, Jan 26, 2013.

  1. leongcc

    leongcc Member Licensed User

    This B4A library allows an app to pair with a Bluetooth device without human intervention. The user need not have enter the pairing key at the system generated dialog, the app will provide the pairing key.

    The reason I needed auto-pairing is because I have a Bluetooth network where new devices continously enter and exit the Bluetooth network. All the devices have different mac address but the same pin. If you are in similar situation, you might find this library useful.

    DISCLAIMER:
    - I wrote this simple library for my own use, so it doesn't quite conform to the proper documentation.
    - I am not responsible for your security and privacy.
    - I am not responsible for damages to your software and hardware.

    CONTEXT:
    1) Tested to work for Android phone and Android TV Box using USB bluetooth dongle.
    2) Tested for Android API 14.

    DESCRIPTION:
    When an app attempts to connect to a Bluetooth device that has not been previously paired, the system generated dialog will normally appear prompting the user to enter the pairing key. At this point, the app calls an internal method in android.bluetooth.BluetoothDevice, supplying the pairing key as a parameter. If you go to Android>Setting>Bluetooth, you will notice the device is listed as "paired". The dialog may still remain on the screen but it seems to disappear by itself.


    HOW TO USE:
    1) Include BtAutoPair library into your B4A IDE.
    2) Include BluetoothAdmin and BtSerial libraries and program them for your purpose.
    3) Include Broadcast library and code it to handle PAIRING_REQUEST. See the sample codes below.
    4) You might want to implement a List or Map to store the device name, mac and the corresponding pairing-key. This is to ensure the correct pairing-key is being sent to the correct device.
    For example, (pseudo-codes)
    if device="samsunk" then
    pinBytes = Array as Byte {0x31, 0x32, 0x33, 0x34}
    device="nokick" then
    pinBytes = Array as Byte {0x39, 0x39, 0x39, 0x39}
    end if



    Caller
    Code:
    Dim Broadcast As BroadCastReceiver
    ...
    Broadcast.Initialize(
    "BroadCastReceiver")
    Broadcast.addAction(
    "android.bluetooth.device.action.PAIRING_REQUEST")    
    Broadcast.SetPriority(
    2147483647)  
    Broadcast.registerReceiver(
    ""
    ...

    Sub BroadCastReceiver_OnReceive (Action As String,  i As  Object)    'Event handler
        If (Action = "android.bluetooth.device.action.PAIRING_REQUEST"Then
            
    Dim  intnt  As  Intent = i
            
    Dim autopair As BtAutoPair
            
    'The pairing pin
            Dim pinBytes() As Byte = Array as Byte {0x310x320x330x34}
            autopair.Start4(intnt, pinBytes)
        End If
    End Sub
     

    Attached Files:

    Last edited: Feb 26, 2013
  2. Stulish

    Stulish Active Member Licensed User

    can you connect without any PIN?? so just have:

    autopair.Start4(intnt, "")

    thanks
     
  3. leongcc

    leongcc Member Licensed User

    Please try and see but note that the data type of pin value is an array of bytes, not a string.


    Code:
    public void Start4(Intent intent, byte[] pin)
     
  4. alhaim

    alhaim Member Licensed User

    A working example

    Could you please attach a working example? That would be very helpful.
     
  5. alhaim

    alhaim Member Licensed User

    How do I make the device discoverable without human intervention with btautopair?
     
  6. walterf25

    walterf25 Well-Known Member Licensed User

    BTAutoconnect

    Hi there, can you post an example, i can't seem to get this to work, an example will be greatly appreciated.

    Thanks,
    Walter
     
  7. leongcc

    leongcc Member Licensed User

    I have attached to the 1st post a zip of a B4A project folder containing a simple demo.
    Open BtAutoPairDemo.b4a in B4A IDE then run with a REAL Android device.
    A simple description of the demo is at the top of the source codes.

    The demo uses passkey "111111", please remember to change it to the passkey of your test device.
     
    Last edited: Feb 26, 2013
  8. Turbo3

    Turbo3 Active Member Licensed User

    Fixed my problem by downloading missing library.

    But so far nothing happens. Changed passcode to "1234" (31,32,33,34 hex).

    Has anyone gotten the demo to work?

    Do I need to enter name and/or mac address?

    -----------------------------------------
    Ok, I now see that it does work but you need to have it still connected to the PC to see the log messages.

    I was expecting to see a message on the screen.
    -----------------------------------------

    Very nice, just what I was looking for. Thanks.
     
    Last edited: Apr 8, 2013
  9. merlin2049er

    merlin2049er Well-Known Member Licensed User

    where do you copy this library to?
     
  10. pgargom

    pgargom Member Licensed User

    Thank you,
    very useful but i have a doubt,
    how i can to send a file or text to the paired devices?

    Thank you !!
     
  11. Turbo3

    Turbo3 Active Member Licensed User

    Is there a way to test the results of AutoPair.Start4? If I use a bad passkey the code just loops forever retrying the same bad passkey. If my hard coded passkey does not work I would like to unregister the BroadCastReceiver_OnReceive and let the system ask the user for the correct passkey. Or have the app ask the user for the correct key to use and save for next time.

    Is there an "OnError" event like there is a "OnReceive" event?
     
  12. Steve_B

    Steve_B Member Licensed User

    When running the example the log reports the MAC address for both the Name and MAC. I need to see the Name to determine if I want to pair the device (HC-05).
     
  13. irda

    irda Member Licensed User

    Good work, the library works, the application connects but the pin dialog still visible, Is it possible close / hide the pin dialog?
     
  14. SteveTerrell

    SteveTerrell Active Member Licensed User

    Good stuff, does what is says on the tin!

    Have you thought of adding an un-pair function to the library? Some of us (well me at least) would find that very useful :)
     
  15. Licht2002

    Licht2002 Member Licensed User

    Hi Leongcc,

    is the error: "Not Connected: java.io.IOException: Service discovery failed" depends on the library... or do i have to search in a another direction?

    Thanks

    Tom
     
  16. metzeller_2013

    metzeller_2013 Member Licensed User

    Hope this works, My app is in kiosk mode hope the PIN dialog will receive the PIN properly
     
  17. Arf

    Arf Active Member Licensed User

    My pairing is initiated from an external device in JustWorks PIN mode, so on the tablet I get a pairing request "Do you want to pair with this device?" YES/NO, no pin required.
    Is there a way I can accept this request automatically? I have tried using pins of (0x30,0x30,0x30,0x30) and (0,0,0,0) and the pairing fails due to incorrect pin.
     
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