Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bluetooth headset detection. #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
*.iml
*.iml
.vscode
7 changes: 3 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-headsetdetection"
version="3.0.0">
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-headsetdetection" version="3.0.0">

<name>Headset Detection</name>

Expand Down Expand Up @@ -42,6 +40,7 @@
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
</config-file>

Expand Down
47 changes: 33 additions & 14 deletions src/android/nl/xservices/plugins/HeadsetDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;

import android.bluetooth.BluetoothHeadset;
import android.content.Context;
import android.media.AudioManager;
import android.content.Intent;
Expand All @@ -23,6 +24,9 @@ public class HeadsetDetection extends CordovaPlugin {

private static final String ACTION_DETECT = "detect";
private static final String ACTION_EVENT = "registerRemoteEvents";
private static final int DEFAULT_STATE = -1;
private static final int DISCONNECTED = 0;
private static final int CONNECTED = 1;
protected static CordovaWebView mCachedWebView = null;

BroadcastReceiver receiver;
Expand All @@ -37,23 +41,20 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
mCachedWebView = webView;
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
this.receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(LOG_TAG, "Headset is unplugged");
mCachedWebView.sendJavascript("cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetRemoved();");
break;
case 1:
Log.d(LOG_TAG, "Headset is plugged");
mCachedWebView.sendJavascript("cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetAdded();");
break;
default:
Log.d(LOG_TAG, "I have no idea what the headset state is");
}
int status = getConnectionStatus(intent.getAction(), intent);

if (status == CONNECTED) {
Log.d(LOG_TAG, "Headset is connected");
mCachedWebView.sendJavascript("cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetAdded();");
} else if (status == DISCONNECTED) {
Log.d(LOG_TAG, "Headset is disconnected");
mCachedWebView.sendJavascript("cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetRemoved();");
} else {
Log.d(LOG_TAG, "Headset state is unknown: " + status);
}
}
};
Expand Down Expand Up @@ -91,6 +92,24 @@ public void onReset() {
removeHeadsetListener();
}

private int getConnectionStatus(String action, Intent intent) {
int state = DEFAULT_STATE;
int normalizedState = DEFAULT_STATE;
if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
state = intent.getIntExtra("state", DEFAULT_STATE);
} else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, DEFAULT_STATE);
}

if ((state == 1 && action.equals(Intent.ACTION_HEADSET_PLUG)) || (state == 2 && action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED))) {
normalizedState = CONNECTED;
} else if (state == 0) {
normalizedState = DISCONNECTED;
}

return normalizedState;
}

private void removeHeadsetListener() {
if (this.receiver != null) {
try {
Expand Down