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

Ionic2 #8

Open
sonoftheweb opened this issue Feb 9, 2017 · 4 comments
Open

Ionic2 #8

sonoftheweb opened this issue Feb 9, 2017 · 4 comments

Comments

@sonoftheweb
Copy link

I would like to know if it works on Ionic2.

@EddyVerbruggen
Copy link
Owner

That's still Cordova, so why wouldn't it?

@sonoftheweb
Copy link
Author

I figured how to make it work. Jolly good show.

@sonoftheweb
Copy link
Author

Hi @EddyVerbruggen

I seem to be having some more issues. In a page, I am trying to use a function in typescript:

setHeadSetDetected(){
    this.platform.ready().then(() => {
      (<any>window).plugins.HeadsetDetection.detect(function (detected) {
        alert(detected);
      });
    });
  }

and call it in view like:

<button ion-button full color="primary" *ngIf="status == 'null'" (click)="setHeadSetDetected()">Detect Headphones</button>

I keep getting this error when I run on a real device:

TypeError: Cannot read property 'HeadsetDetection' of undefined

What do you think is wrong here?

@sonoftheweb sonoftheweb reopened this Feb 10, 2017
@Nayan014
Copy link

Nayan014 commented Jan 30, 2018

Hi @sonoftheweb

This works for me.

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {
    this.audioInit();
  }

  audioJackStatus(){
    console.log('checking headphone');
    try {
      (<any>window).HeadsetDetection.detect(function(detected){
        if(detected){
          alert('Headphone connected')
        }
        else{
          alert('No Headphone Found.')
        }
      })
    } catch (error) {
      alert(error); 
    }
  }

  audioInit(){
    try {
        document.addEventListener('deviceready', function() {
            (<any>window).HeadsetDetection.registerRemoteEvents(function(status) {
                switch (status) {
                  case 'headsetAdded':
                    alert('Headset connected');
                  break;
                  case 'headsetRemoved':
                    alert('Headset removed');
                  break;
                };
            });
        }, false);
      } catch (error) {
        alert(error);
      }    
  }
}

Git Repo
https://github.com/Nayan014/headphone-ionic3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants