Giter Club home page Giter Club logo

headsetdetection-phonegap-plugin's Introduction

HeadsetDetection-PhoneGap-Plugin

by Eddy Verbruggen / @eddyverbruggen

  1. Description
  2. Installation
  3. Usage
  4. Credits
  5. License

1. Description

Detect a wired or wireless (Bluetooth) headset, connected to you phone.

  • Works on Android, probably every version you'd care about.
  • Works on iOS 6+

2. Installation

$ cordova plugin add cordova-plugin-headsetdetection
$ cordova prepare

3. Usage

<button onclick="window.HeadsetDetection.detect(function(detected) {alert(detected)})">headphone detected?</button>

The successCallback (first argument) is a boolean (true or false). Couldn't be easier :)

3.1 Usage with event-based detection

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, which does support this feature on Android.

document.addEventListener('deviceready', function() {
    window.HeadsetDetection.registerRemoteEvents(function(status) {
        switch (status) {
            case 'headsetAdded':
                alert('Headset was added');
            break;
            case 'headsetRemoved':
                alert('Headset was removed');
            break;
        };
    });
}, false);

4. CREDITS

This plugin was created by and enhanced for Plugman / PhoneGap Build by Eddy Verbruggen.

The awesome registerRemoteEvents function was added by Gerhard Sletten.

5. License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

headsetdetection-phonegap-plugin's People

Contributors

eddyverbruggen avatar gerhardsletten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

headsetdetection-phonegap-plugin's Issues

Unable to make it work

Hey, I installed it with the lines:

$ cordova plugin add https://github.com/EddyVerbruggen/HeadsetDetection-PhoneGap-Plugin.git
$ cordova prepare

But it did not work.

I used the example:

    try {
        window.plugins.headsetdetection.detect(function(detected) {
            // alert(detected)
            document.getElementById('catch').innerHTML = 'OK ' + detected;
        })
    } catch (e) {
        document.getElementById('catch').innerHTML = 'ERROR' + e;
    }

I was receiving a CANNOT READ PROPERTY 'DETECT' OF UNDEFINED.

I added the HeadsetDetection.js to do a test and I don't get the error but the callback is never called.

I am using Phonegap desktop for windows and the android app.

What am I missing? I did not find any answer to this, but I am not sure what to look for either...

How can I remove the registerRemoteEvents listener?

If the customer enables this option, then only I have to detect it automatically otherwise, I want to remove this listener, but currently, if I once add this registerRemoteEvents event I can not able to remove it.

Question: Headset Type

Hi there,

thanks for that great Plugin.
I just wondered - would it be possible to distinguish between a "cabled" and a "wireless" Headset somehow?

Thanks!

Why this plugin doesn't work when cordova-plugin-media is recording

Hi Eddy, I'm a big fan of your cordova plugins thanks a lot.
I am facing an issue on this one, I can make it work super nicely but when cordova-plugin-media is actually recording your headset detection event doesn't trigger.
I don't see why so far and I cannot find in your code any potential reason.
Would you mind checking ?
Thanks

HeadsetDetection.m:9:33: error: no visible @interface for 'CDVPluginResult' declares the selector 'toSuccessCallbackString

Hi @EddyVerbruggen ,
when building the plugin with an ionic app for ios, I'm getting an error with cordova-ios-4 but it's working with [email protected] with a deprecation warning (see details below).
Sorry not being able to help you as I do not have any knowledge about objectiveC ...
Thank you very much for the fix,
Mirko

[email protected] :
/Users/mirko/myApp080316/platforms/ios/CPM App/Plugins/nl.x-services.plugins.headsetdetection/HeadsetDetection.m:9:9: warning: 'writeJavascript:' is deprecated: Deprecated in Cordova 3.6. Use the CDVCommandDelegate equivalent of evalJs:. This will be removed in 4.0.0 [-Wdeprecated-declarations]

[self writeJavascript:[result toSuccessCallbackString:callbackId]];

^

[email protected] , [email protected] , [email protected] :
/Users/mirko/myApp080316/platforms/ios/CPM App/Plugins/nl.x-services.plugins.headsetdetection/HeadsetDetection.m:9:33: error: no visible @interface for 'CDVPluginResult' declares the selector 'toSuccessCallbackString:'

[self writeJavascript:[result toSuccessCallbackString:callbackId]];

                 ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

** BUILD FAILED **

Ionic2

I would like to know if it works on Ionic2.

plugin not working on android 5.1.1

I tried the plugin and Android 5 it was working fine. But on Android 5.1.1 it is not working. I mean not detecting headset.
Below are the codes.

function plays() {
window.HeadsetDetection.detect(function(detected) {
alert('hi');
window.plugins.insomnia.keepAwake()
cordova.plugins.tonegenerator.play(250)
})

document.addEventListener('deviceready', function() {
window.HeadsetDetection.registerRemoteEvents(function(status) {
switch (status) {
case 'headsetAdded':
// alert('hi');
window.plugins.insomnia.keepAwake()
cordova.plugins.tonegenerator.play(250)
break;
case 'headsetRemoved':
alert('Please connect to Headset/Earpiece then play.');
break;
};
});
}, false);
Any advise what am i doing wrong?
}

registerRemoteEvents only fires when I register it

Unfortunately the registerRemoteEvents only fires when I register it, and it always returns "headsetRemoved" (even if the BT headset is already connected). When I connect/disconnect a BT headset, the event won't fire. The HeadsetDetection.detect function works well though.

(Android 10, Huawei P30 Pro)

NativeScript port

Hi Eddy,
I was wondering how hard is it to port this cordova plugin to a nativescript module ?
Do I have to know the Android and IOS developer API reference ?
Thanks,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.