tns plugin add nativescript-headset-detection
To check for a headset at any given moment, use this method:
var headsetDetection = require("nativescript-headset-detection");
headsetDetection.isConnected()
.then(function (connected) { console.log("Connected? " + connected); })
.catch(function (err) { console.log("Error: " + err)});
import * as headsetDetection from 'nativescript-headset-detection';
headsetDetection.isConnected()
.then(connected => console.log(`Connected? ${connected}`))
.catch(err => console.log(`Error: ${err}`));
To listen to changes to the headset state, use this one (adding it to a high level component like `app.[ts|js] makes sense); you can pass in a callback function that gets invoked whenever a headset is (dis)connected:
var headsetDetection = require("nativescript-headset-detection");
headsetDetection.onConnectionStateChanged(function (connected) {
console.log("Connection changed to: " + connected);
});
import * as headsetDetection from 'nativescript-headset-detection';
headsetDetection.onConnectionStateChanged(connected => console.log(`Connection changed to: ${connected}`));