nativescript-headset-detection
检测耳机(连接/断开)。
npm i --save nativescript-headset-detection

NativeScript 耳机检测插件

Build Status NPM version Twitter Follow

安装

tns plugin add nativescript-headset-detection

API

isConnected

要检查任何给定时刻的耳机,请使用此方法

JavaScript

var headsetDetection = require("nativescript-headset-detection");

headsetDetection.isConnected()
.then(function (connected) { console.log("Connected? " + connected); })
.catch(function (err) { console.log("Error: " + err)});

TypeScript

import * as headsetDetection from 'nativescript-headset-detection';

headsetDetection.isConnected()
.then(connected => console.log(`Connected? ${connected}`))
.catch(err => console.log(`Error: ${err}`));

onConnectionStateChanged

要监听耳机状态的更改,请使用此方法(将其添加到高级组件如 `app.[ts|js] 中很有意义);您可以将回调函数传递进去,每次耳机(连接/断开)时都会调用该函数

JavaScript

var headsetDetection = require("nativescript-headset-detection");

headsetDetection.onConnectionStateChanged(function (connected) {
console.log("Connection changed to: " + connected);
});

TypeScript

import * as headsetDetection from 'nativescript-headset-detection';

headsetDetection.onConnectionStateChanged(connected => console.log(`Connection changed to: ${connected}`));