Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26029] Android : Titanium.Android.ACTION_HEADSET_PLUG Not working as expected.

GitHub Issuen/a
TypeBug
PriorityNone
StatusClosed
ResolutionInvalid
Resolution Date2018-05-17T21:15:54.000+0000
Affected Version/sRelease 7.1.1
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterAminul Islam
AssigneeJoshua Quick
Created2018-05-10T16:12:06.000+0000
Updated2018-08-06T17:37:03.000+0000

Description

We want to detect the current state of headphones connected (true) or disconnected (false). We have an API for that but no example code in documents. http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android-property-ACTION_HEADSET_PLUG I create an example code but not working.
function doClick() {
 Ti.Android.unregisterBroadcastReceiver(bc);
}
$.index.open();
var onReceived = function(e) {
 Ti.API.info("received!");
 Ti.API.info('Handling broadcast. >> ' + JSON.stringify(e));
 alert('Handling broadcast. >> ' + JSON.stringify(e));
};
var bc = Ti.Android.createBroadcastReceiver({
 onReceived : onReceived
});
Ti.Android.registerBroadcastReceiver(bc,["Titanium.Android.ACTION_HEADSET_PLUG"]);
Index.xml
<Alloy>
 
 <Window class="container"  onClose="doClose" />
 
</Alloy>
Test Environment:
Operating System
  Name                        = Microsoft Windows 10 Pro
  Version                     = 10.0.16299
  Architecture                = 32bit
  # CPUs                      = 4
  Memory                      = 17091956736
Node.js
  Node.js Version             = 8.9.1
  npm Version                 = 5.5.1
Titanium CLI
  CLI Version                 = 5.1.0
Titanium SDK
  SDK Version                 = 7.1.0.GA
Thanks

Comments

  1. Christopher Williams 2018-05-17

    Isn't this code example just wrong? The second argument to registerBroadcastReceiver should be an array of references to the Ti.Android.ACTION_* constants like so:
       Ti.Android.registerBroadcastReceiver(bc, [ Ti.Android.ACTION_HEADSET_PLUG ]);
       
    (Note that Ti.Android.ACTION_HEADSET_PLUG is a reference to a constant whose value aliases to the correct String necessary under the hood, it itself is **not** a String)
  2. Joshua Quick 2018-05-17

    This is not a bug. The below code works. I've tested it on real devices running Android OS v4.1 and v8.0. When you connect/disconnect a headset to the Android device, it'll display a "Connected" or "Disconnected" label.
       // Create a window with label displaying current headset state.
       var window = Ti.UI.createWindow();
       var label = Ti.UI.createLabel();
       window.add(label);
       window.open();
       
       // Register a headset broadcast receiver.
       var receiver = Ti.Android.createBroadcastReceiver({
       	onReceived: function(e) {
       		// Headset has been connected/disconnected. Display current state via label.
       		if (e.intent.action === Ti.Android.ACTION_HEADSET_PLUG) {
       			var isPluggedIn = (e.intent.getIntExtra("state", 0) != 0);
       			label.text = "Headset is " + (isPluggedIn ? "Connected" : "Disconnected");
       		}
       	},
       });
       Ti.Android.registerBroadcastReceiver(receiver, [Ti.Android.ACTION_HEADSET_PLUG]);
       
       // Unregister above broadcast receiver when exiting.
       window.addEventListener("close", function(e) {
       	Ti.Android.unregisterBroadcastReceiver(receiver);
       });
       
    Like what Chris has said, they're incorrectly passing the Ti.Android.ACTION_HEADSET_PLUG constant in. They should not be double quoting it. For your info, Titanium's Ti.Android.ACTION_HEADSET_PLUG constant returns string "android.intent.action.HEADSET_PLUG", which matches Google's constant here... https://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG Google documents the intent "extras" for this action (such as "state") here... https://developer.android.com/reference/android/media/AudioManager#ACTION_HEADSET_PLUG
  3. Eric Merriman 2018-08-06

    Closing as invalid. If incorrect, please reopen.

JSON Source