Android Question android.,media.AudioManager - setWiredDeviceConnectionState

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi All

I need to get some help converting some java to B4A. Here the link http://stackoverflow.com/questions/33422325/how-to-hide-headset-icon


It nedd to uses reflection as mentioned in the link.

Here's the code:

B4X:
AudioManager manager =(AudioManager) aContext.getSystemService(Context.AUDIO_SERVICE);

try{int deviceType =AudioSystem.DEVICE_OUT_WIRED_HEADSET;
// turn off headset
int state =0;
Class<?> cl =Class.forName("android.media.AudioManager");
Method method = cl.getMethod("setWiredDeviceConnectionState",newClass[]{Integer.TYPE,Integer.TYPE,String.class}); 
method.invoke(manager,newObject[]{ deviceType, state,"device"});}

catch(Exception e)

{ iLog.error("disableHeadset, exception {}",ExceptionUtils.getStackTrace(e));}

Thanks

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Not tested:
B4X:
Dim jo As JavaObject
jo.InitializeContext
Dim r As Reflector
r.Target = jo.RunMethod("getSystemService", Array("audio")
r.RunMethod4("setWiredDeviceConnectionState", Array (deviceType, state, "device"), Array As String("java.lang.int", java.lang.int", "java.lang.String"))
Hi Erel

Many thanks for the help. That worked fine.

code updated so anyone else can use it

B4X:
   Dim jo As JavaObject
   jo.InitializeContext
   
   Dim r As Reflector
   r.Target = jo.RunMethod("getSystemService", Array("audio"))
   r.RunMethod4("setWiredDeviceConnectionState", Array (4, 0, "device"), _
           Array As String("java.lang.int", "java.lang.int", "java.lang.String"))

A good reference is https://github.com/android/platform...ter/media/java/android/media/AudioSystem.java for const values.

Regards

John.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello again

The code below failed on API 23 (Marshmallow), any ideas?

B4X:
Sub reroute_speakerphone(state As Int)

   Dim jo As JavaObject
   jo.InitializeContext
   
   Dim r As Reflector
   
   Try
     r.Target = jo.RunMethod("getSystemService", Array("audio"))
     r.RunMethod4("setWiredDeviceConnectionState", Array (4, state, "device"), _
           Array As String("java.lang.int", "java.lang.int", "java.lang.String"))
   Catch
     ToastMessageShow(LastException.Message,True)
   End Try

End Sub

There is reference here, but it's all Java to me :)
https://github.com/AndroidSDKSource...3/blob/master/android/media/AudioManager.java

@lemonisdead
@stevel05

Regard

JOhn
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello John,
As usual, I don't have the reply but, couldn't it be an issue with the new permissions for SDK-23 ? Do we have any error code ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Of course it compiles, you're accessing it using Reflection. Ignore that question.

I get a method not found error, is that the same error?

And it runs on my Android 5.1.1 device.

A little more reading and it looks like it was a private method. They may have just removed it.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I can't find any other reference to it on Google, or Google developers.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
My 50cts : could the appcompat version fix the problem ? I had got the same even on Lollipop and the latest code version fixed (perhaps something related to the chipset I had thought)
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Found this

https://github.com/AndroidSDKSource...3/blob/master/android/media/AudioManager.java goto line 3122, this seems to take 4 parms unlike the current method which takes 3.


B4X:
 /**
  * Indicate wired accessory connection state change.
  * @param device type of device connected/disconnected (AudioManager.DEVICE_OUT_xxx)
  * @param state  new connection state: 1 connected, 0 disconnected
  * @param name  device name
  * {@hide}
  */
  public void setWiredDeviceConnectionState(int type, int state, String address, String name) {
  IAudioService service = getService();
  try {
  service.setWiredDeviceConnectionState(type, state, address, name,
  mApplicationContext.getOpPackageName());
  } catch (RemoteException e) {
  Log.e(TAG, "Dead object in setWiredDeviceConnectionState "+e);
  }
  }
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you tried the appcompat ?

As it doesn't instantiate the class directly but calls getSystemService, we can only use the class that the system gives us. Unless it has been moved to a new class somewhere, which I couldn't find any documentation for. The documentation in the link is for AudioManager, which is what we've asked for in the code. If it just needed different parameters we would get a parameters not matched error.

I would guess that if the functionality is working in an app, there must be a different approach to achieving the same thing.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi @stevel05 we have an unrooted HTC M9 and keycut seems to work on it. I know that the source of keycut is out that as I have come across it, however my java skills as a bit week
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Last edited:
Upvote 0
Top