更新记录
1.0.0(2023-03-15)
实时监听有线耳机/无线蓝牙耳机插拔,判断当前是否有耳机连接
平台兼容性
Android | Android CPU类型 | iOS |
---|---|---|
适用版本区间:4.4 - 12.0 | armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 | 适用版本区间:9 - 16 |
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios
注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择
实时监听有线耳机/无线蓝牙耳机插拔,判断当前是否有耳机连接
插件声明
var listenEarPhone = uni.requireNativePlugin("wrs-listenEarPhone");
- 开始监听有线耳机/无限耳机插拔
listenEarPhone.setCallback((resp) => {
this.showMsg(JSON.stringify(resp));
switch (uni.getSystemInfoSync().platform) {
case 'android': {
var action = resp.action;
if (action) {
var msg = "";
switch (action) {
// 蓝牙耳机
case "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED":
var profileConnectionState = resp.profileConnectionState;
switch (profileConnectionState) {
// STATE_DISCONNECTED
case 0:
msg = "蓝牙耳机断开连接";
break;
// STATE_CONNECTING
case 1:
msg = "蓝牙耳机连接中...";
break;
// STATE_CONNECTED
case 2:
msg = "蓝牙耳机已经连接";
break
// STATE_DISCONNECTING
case 3:
msg = "蓝牙耳机正在断开连接...";
break
}
break;
// 有线耳机
case "android.intent.action.HEADSET_PLUG":
if (resp.state) {
if (resp.state == 0) { // 拔出
msg = "有线耳机拔出";
} else if (resp.state == 1) { // 插入
msg = "有线耳机插入";
}
}
if (resp.microphone) {
if (resp.microphone == 0) { // 没麦克风
msg = "有线耳机没有麦克风";
} else if (resp.microphone == 1) { // 有麦克风
msg = "有线耳机有麦克风";
}
}
break;
case "android.media.AUDIO_BECOMING_NOISY":
msg = "有线耳机拔出或蓝牙耳机断开连接";
break;
default:
break;
}
this.showMsg(msg);
}
}
break;
case 'ios': {
var msg = "";
var AVAudioSessionRouteChangeReason = resp.AVAudioSessionRouteChangeReason;
switch (AVAudioSessionRouteChangeReason) {
// AVAudioSessionRouteChangeReasonUnknown
case 0:
msg = "AVAudioSessionRouteChangeReasonUnknown";
break;
// AVAudioSessionRouteChangeReasonNewDeviceAvailable
case 1:
msg = "连接耳机";
break;
// AVAudioSessionRouteChangeReasonOldDeviceUnavailable
case 2:
msg = "拔出耳机";
break;
// AVAudioSessionRouteChangeReasonCategoryChange
case 3:
msg = "AVAudioSessionRouteChangeReasonCategoryChange";
break;
// AVAudioSessionRouteChangeReasonOverride
case 4:
msg = "AVAudioSessionRouteChangeReasonOverride";
break;
// AVAudioSessionRouteChangeReasonWakeFromSleep
case 6:
msg = "AVAudioSessionRouteChangeReasonWakeFromSleep";
break;
// AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory
case 7:
msg = "AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory";
break;
// AVAudioSessionRouteChangeReasonRouteConfigurationChange
case 8:
msg = "AVAudioSessionRouteChangeReasonRouteConfigurationChange";
break;
default:
break;
}
this.showMsg(msg);
}
break;
default:
break;
}
});
- 取消监听
listenEarPhone.cancelCallback();
- 判断当前是否有耳机连接
listenEarPhone.getCurState((resp) => {
this.showMsg(JSON.stringify(resp));
switch (uni.getSystemInfoSync().platform) {
case 'android': {
var profileConnectionState = resp.profileConnectionState;
var isWiredHeadsetOn = resp.isWiredHeadsetOn;
var isBluetoothScoOn = resp.isBluetoothScoOn;
var isBluetoothA2dpOn = resp.isBluetoothA2dpOn;
var msg = "";
if (isWiredHeadsetOn || isBluetoothScoOn || isBluetoothA2dpOn) {
msg = "当前有连接耳机";
} else {
msg = "当前没有连接耳机";
}
this.showMsg(msg);
}
break;
case 'ios': {
var msg = "";
var currentRoute = resp.currentRoute;
if (currentRoute) {
msg = "当前没有连接耳机";
for(var i = 0; i < currentRoute.length; i ++) {
var route = currentRoute[i];
var portType = route.portType;
if(portType == "Speaker") { // 没有连接耳机
msg = "没有连接耳机";
} else if(portType == "BluetoothA2DPOutput") { // 连接的是蓝牙耳机
msg = "连接的是蓝牙耳机";
} else { // 连接的是有线耳机或其他,这里可以根据各自的连接情况判断对应的值
msg = "连接的是有线耳机或其他";
}
}
} else {
this.showMsg("没有监测到数据");
}
this.showMsg(msg);
}
break;
default:
break;
}
});
支持定制,联系方式 QQ:252797991