[TIMOB-26029] Android : Titanium.Android.ACTION_HEADSET_PLUG Not working as expected.
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | None |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2018-05-17T21:15:54.000+0000 |
Affected Version/s | Release 7.1.1 |
Fix Version/s | n/a |
Components | Android |
Labels | n/a |
Reporter | Aminul Islam |
Assignee | Joshua Quick |
Created | 2018-05-10T16:12:06.000+0000 |
Updated | 2018-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
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:
(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)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.
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'sTi.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_PLUGClosing as invalid. If incorrect, please reopen.