[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 thatTi.Android.ACTION_HEADSET_PLUGis 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// 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); });Ti.Android.ACTION_HEADSET_PLUGconstant in. They should not be double quoting it. For your info, Titanium'sTi.Android.ACTION_HEADSET_PLUGconstant 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.