Android Question Turning on Bluetooth on Raspberry Pi 3 running Android Things

Tareq Khan

Member
Licensed User
Hi All,
I have installed Android Things in a Raspberry Pi 3 board. I am trying to use the BLE2 v1.37 library of B4A v8.80 on this device.

I am getting the message "Not powered on." when I run the code below.

B4X:
If Manager.State <> Manager.STATE_POWERED_ON Then
   Log("Not powered on.")
Else If rp.Check(scan_rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then 
            Log("No location permission.")
Else
        'start scanning
        Manager.Scan2(Null, False)
End If
It seems to me Bluetooth is not turned-on in the RPi board. Is there anyway I can turn-on the on-board Bluetooth of the RPi which is running Android Things? I found a tutorial in https://scribles.net/auto-power-on-bluetooth-adapter-on-boot-up/ that tells how to turn it on using Linux command, but how do I issue these command when it is running Android Things?

Thanks
Tareq
 

Tareq Khan

Member
Licensed User
I solved it from searching other forum threads. :)

Added the 'Serial' library. Then added these codes:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public BTadmin As BluetoothAdmin
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    'Enable Bluetooth ---------------
        BTadmin.Initialize("BTadmin")
        If BTadmin.IsEnabled = False Then
            If BTadmin.Enable = False Then
                ToastMessageShow("Error enabling Bluetooth adapter.", True)
            Else
                ToastMessageShow("Enabling Bluetooth adapter...", False)
            End If
        End If
End Sub

Private Sub BTadmin_StateChanged (NewState As Int, OldState As Int)
    Log("BT state changed: " & NewState)
    If NewState = BTadmin.STATE_ON Then ToastMessageShow(" Bluetooth adapter Enabled", False)
   
End Sub
 
Top