diff --git a/.gitignore b/.gitignore index 29b636a..ea0080b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -*.iml \ No newline at end of file +*.iml +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 4797769..c13fe37 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,6 @@ The successCallback (first argument) is a boolean (true or false). Couldn't be e If you need to respond to removal or added headset while your app i running, you can use the `HeadsetDetection.registerRemoteEvents` function and listen for either `headsetAdded` or `headsetRemoved`: -Note that changes to the Bluetooth headset state on Android are not supported currently. So on Android you'll only get an event when a wired headset is disconnected. On iOS for both wired and Bluetooth headsets. I only recently figured out this glitch when creating the [NativeScript Headset Detection plugin](https://github.com/EddyVerbruggen/nativescript-headset-detection), which does support this feature on Android. - ```js document.addEventListener('deviceready', function() { window.HeadsetDetection.registerRemoteEvents(function(status) { diff --git a/package.json b/package.json index f347746..e55411f 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/EddyVerbruggen/HeadsetDetection-PhoneGap-Plugin.git" + "url": "git+https://github.com/RamseyInHouse/HeadsetDetection-PhoneGap-Plugin.git" }, "keywords": [ "cordova", @@ -28,4 +28,4 @@ "url": "https://github.com/EddyVerbruggen/HeadsetDetection-PhoneGap-Plugin/issues" }, "homepage": "https://github.com/EddyVerbruggen/HeadsetDetection-PhoneGap-Plugin#readme" -} +} \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 3a52097..d81c479 100755 --- a/plugin.xml +++ b/plugin.xml @@ -1,8 +1,6 @@ - + Headset Detection @@ -42,6 +40,7 @@ + diff --git a/src/android/nl/xservices/plugins/HeadsetDetection.java b/src/android/nl/xservices/plugins/HeadsetDetection.java index 27f5ace..cead2f3 100644 --- a/src/android/nl/xservices/plugins/HeadsetDetection.java +++ b/src/android/nl/xservices/plugins/HeadsetDetection.java @@ -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; @@ -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; @@ -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); } } }; @@ -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 {