EddyVerbruggen / HeadsetDetection-PhoneGap-Plugin

:headphones: A PhoneGap plugin for detection of a headset (wired or bluetooth)
34 stars 27 forks source link

Ionic2 #8

Open sonoftheweb opened 7 years ago

sonoftheweb commented 7 years ago

I would like to know if it works on Ionic2.

EddyVerbruggen commented 7 years ago

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

sonoftheweb commented 7 years ago

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

sonoftheweb commented 7 years ago

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?

Nayan014 commented 6 years ago

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