Android Question Bluetooth Button (need tutorial)

appie21

Active Member
Licensed User
Longtime User
It will not work. You cannot connect to a headset with SPP (serial profile). The connection is handled by the OS.

Hello

I have now the bluetooth button working.

I see in the log: (Intent) Intent { act=android.intent.action.VOICE_COMMAND flg=0x10000000 cmp=b4a.example/.main }

If i want to start a voice recognition the VR comes in a loop and start over and over again

B4X:
Sub Activity_Resume

    If  Activity.GetStartingIntent.Action = "android.intent.action.VOICE_COMMAND" Then
        vr.Listen
    End If

    Log(Activity.GetStartingIntent)
End Sub

How to fix these
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What did you change for the intent to start working?

About the loop:
B4X:
Sub Process_Globals
 Private PrevIntent As Intent
End Sub

Sub Activity_Resume
 If Activity.GetStartingIntent <> PrevIntent And Activity.GetStartingIntent.Action = "android.intent.action.VOICE_COMMAND" Then
   ...
 End If
 PrevIntent = Activity.GetStartingIntent
End Sub
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
The loop will stay here is my code

What I want is if the app is started and i prees the button of my bluetooth button . the VoiceRecognition will start

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private PrevIntent As Intent
    Private vr As VoiceRecognition
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("1")
vr.Initialize("vr")

End Sub

Sub Activity_Resume 
    Dim wIn As Intent
    wIn = Activity.GetStartingIntent 
    If wIn.Action = "android.intent.action.ASSIST" Or wIn.Action = "android.intent.action.VOICE_COMMAND" Then     
    vr.Listen
    End If
PrevIntent = Activity.GetStartingIntent

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    StopBluetoothSco
End Sub

Sub StopBluetoothSco() 
    Dim r As Reflector
    ToastMessageShow("Stop sco", False)
 
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    r.RunMethod("stopBluetoothSco")
End Sub

Sub StartBluetoothSco()
    Dim r As Reflector
    ToastMessageShow("Start sco", False)
 
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
    r.RunMethod("startBluetoothSco")
End Sub

I get this sample from another topic

I see now that it also start the app by pressing the button that is not need i only will call vr.listen when bluetooth button is pressed

#Solved!!!
 
Last edited:
Upvote 0

appie21

Active Member
Licensed User
Longtime User
Itry to start the bluetooth action with a Service module. As test I pop up a msgbox

But when i push the bluetooth button i not see she msgbox. What do I wrong

main code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

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("Layout1")

End Sub

Sub Activity_Resume
Log (Activity.GetStartingIntent)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

the intent what i see if i press the button:
(Intent) Intent { act=android.intent.action.VOICE_COMMAND flg=0x10000000 cmp=b4a.example/.main }

the service module:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
 
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
Msgbox(StartingIntent.Action, "test")
End Sub

Sub Service_Destroy

End Sub

and the manifest

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
AddPermission(android.permission.BLUETOOTH)
AddPermission(android.permission.BLUETOOTH_ADMIN)
AddPermission(android.permission.BROADCAST_STICKY)

AddActivityText(main,  <intent-filter android:priority="1000">
        <action android:name="android.intent.action.VOICE_COMMAND" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
)

AddActivityText(S1,  <intent-filter android:priority="1000">
        <action android:name="android.intent.action.VOICE_COMMAND" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
)

if i use the metode in the previos post the activity will start and start over what makes my program slow (the Bvoice Recognition will be slow then..)
 
Upvote 0

Similar Threads

Top