Android Question I need b4a code to use BT setcommuniactiondevice / Clearcommunicationdevice , for Bluetooth MIC audio recording

abdulla

Member
Licensed User
Longtime User
Hello ,

on android version up to latest one android 33 , when recording or capturing audio from wireless Bluetooth microphone , i am using b4a , so my app does not capture the audio form the
paired and connected Bluetooth device unless or until i execute the below code :

B4X:
Sub StartBluetoothSco()

 

   Dim r As Reflector

   r.Target = r.GetContext

   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")

   r.RunMethod("startBluetoothSco")

   Log("startBluetoothSco")

 

End Sub

and also when i need to stop the recording from the wireless BT mic , i just have to execute the below code

B4X:
Sub stopBluetoothSco()
  
Dim r As Reflector
   r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
r.RunMethod("stopBluetoothSco")
Log("stopBluetoothSco")

End Sub


meaning that startbluetoothsco will let android switch recording from built in mic to the paired and connected microphone , then stopbluetoothsco will switch the mic to the built_in Mic back again .


Now My Question , Android 14 SDK 34 , the above startblutoothsco & stopbluetoothsco will be deprecated can not be used any more , so we can not use them any more ,, alternatively or the alternative way that i have found that SETCOMMUNICATIONDEVICE(device) has been available in android since i think android 11 as alternative way of doing it so when we need to switch audio recording to the BT MIC
we just have to use setcommunicationdevice(device) then when we need to clear it out or switch the audio recording back to the built in MIC we just need to execute Clearcommunicationdevice(device) could any body have a B4A code to do that just like the above code for startblutoothsco / stopbluetoothsco, it does not have to b the same as the above code , it could be deferent but to do the needed functionality , i needed an alternative way for Android 14 sdk 34 to this so it will keep working on android 14 and above ,

again could any body help to provide B4A code to start and stop audio Recording from BT Mic using setcommunicationdevice(device) / clearcommunicationdevice(device)

and by the way i am using Audiorecord and also audiotrack to record audio then to play it back .

i will really appreciate if any body can provide that code , and thanks in advance .
 

abdulla

Member
Licensed User
Longtime User
Hello ,

on android version up to latest one android 33 , when recording or capturing audio from wireless Bluetooth microphone , i am using b4a , so my app does not capture the audio form the
paired and connected Bluetooth device unless or until i execute the below code :

B4X:
Sub StartBluetoothSco()

 

   Dim r As Reflector

   r.Target = r.GetContext

   r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")

   r.RunMethod("startBluetoothSco")

   Log("startBluetoothSco")

 

End Sub

and also when i need to stop the recording from the wireless BT mic , i just have to execute the below code

B4X:
Sub stopBluetoothSco()
 
Dim r As Reflector
   r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "audio", "java.lang.String")
r.RunMethod("stopBluetoothSco")
Log("stopBluetoothSco")

End Sub


meaning that startbluetoothsco will let android switch recording from built in mic to the paired and connected microphone , then stopbluetoothsco will switch the mic to the built_in Mic back again .


Now My Question , Android 14 SDK 34 , the above startblutoothsco & stopbluetoothsco will be deprecated can not be used any more , so we can not use them any more ,, alternatively or the alternative way that i have found that SETCOMMUNICATIONDEVICE(device) has been available in android since i think android 11 as alternative way of doing it so when we need to switch audio recording to the BT MIC
we just have to use setcommunicationdevice(device) then when we need to clear it out or switch the audio recording back to the built in MIC we just need to execute Clearcommunicationdevice(device) could any body have a B4A code to do that just like the above code for startblutoothsco / stopbluetoothsco, it does not have to b the same as the above code , it could be deferent but to do the needed functionality , i needed an alternative way for Android 14 sdk 34 to this so it will keep working on android 14 and above ,

again could any body help to provide B4A code to start and stop audio Recording from BT Mic using setcommunicationdevice(device) / clearcommunicationdevice(device)

and by the way i am using Audiorecord and also audiotrack to record audio then to play it back .

i will really appreciate if any body can provide that code , and thanks in advance .
hello . in google i found this following code , to enable and disable speakerphone mode using set or clear communicationdevice :

see this link :
https://developer.android.com/refer...nicationDevice(android.media.AudioDeviceInfo)

here is the code :

B4X:
// Get an AudioManager instance
 AudioManager audioManager = Context.getSystemService(AudioManager.class);
 AudioDeviceInfo speakerDevice = null;
 List devices = audioManager.getAvailableCommunicationDevices();
 for (AudioDeviceInfo device : devices) {
     if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
         speakerDevice = device;
         break;
     }
 }
 if (speakerDevice != null) {
     // Turn speakerphone ON.
     boolean result = audioManager.setCommunicationDevice(speakerDevice);
     if (!result) {
         // Handle error.
     }
     // Turn speakerphone OFF.
     audioManager.clearCommunicationDevice();
 }

so the question here , is how can we find the attached and connected bluetooth device to the android phone and select it or clear it , i mean weather its MIC , SPEAKER or Speaker_microphone , head set that has speakers and a MIC


so thanks again and i appreciate any help .
 
Upvote 0

abdulla

Member
Licensed User
Longtime User
hello . in google i found this following code , to enable and disable speakerphone mode using set or clear communicationdevice :

see this link :
https://developer.android.com/refer...nicationDevice(android.media.AudioDeviceInfo)

here is the code :

B4X:
// Get an AudioManager instance
 AudioManager audioManager = Context.getSystemService(AudioManager.class);
 AudioDeviceInfo speakerDevice = null;
 List devices = audioManager.getAvailableCommunicationDevices();
 for (AudioDeviceInfo device : devices) {
     if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
         speakerDevice = device;
         break;
     }
 }
 if (speakerDevice != null) {
     // Turn speakerphone ON.
     boolean result = audioManager.setCommunicationDevice(speakerDevice);
     if (!result) {
         // Handle error.
     }
     // Turn speakerphone OFF.
     audioManager.clearCommunicationDevice();
 }

so the question here , is how can we find the attached and connected bluetooth device to the android phone and select it or clear it , i mean weather its MIC , SPEAKER or Speaker_microphone , head set that has speakers and a MIC


so thanks again and i appreciate any help .
hello again , i just wanted to clear up some points here , i am looking only TO ENABLE THE BLUETOOTH MICROPHONE ONLY AS TO SWITCH FROM THE BULT IN PHONE MIC TO THE BLUETOOTH MIC ONLY and then back to phone MIC when done recording .android device generally automatically switches from built in speakers to BT speaker once its paired and connected , BUT NOT THE MICROPHONE ,
 
Upvote 0
Top