Android 11 webview webrtc无法使用问题

**问题:**Android 11 webview 调用webrtc无法使用, 看logcat日志会报如下错误

复制代码
[ERROR:address_tracker_linux.cc(245)] Could not send NETLINK request: Permission denied (13)

查了下相关的网络权限都有配置了还是不行,还是报这个权限问题

**原因:**跟 android11 限制读网卡信息有关系, 算webview的一个兼容bug

https://developer.android.com/about/versions/11/privacy/mac-address

解决:

方案一: 可以修改app 的targetSdkVersion小于30

方案二:发现第一次发起连接失败后再连接就正常了,所以在正式连接之前发起一次连接

TypeScript 复制代码
async function connect() {
     if (getAndroidVersion() === 11) {
          await this.fixAndroid({
            iceServers,
            iceCandidatePoolSize: 1, //  这里要设置1, 不设1也连不上,不知道为什么
          })
     }
    // 发起正式连接
    // let peer = new RTCPeerConnection(turn);
    // ......

}


function getAndroidVersion() {
  // 获取设备信息
  const deviceInfo = navigator.userAgent;
  // 解析设备信息
  const androidVersion = deviceInfo.match(/Android ([\d.]+)/);
  if (androidVersion && androidVersion!.length === 2) {
    return Number(androidVersion![1])
  }
  // 返回系统版本号
  return null;
};

async fixAndroid(turn: any) {
    console.log("fixing android webrtc bug");
    return new Promise(async (resolve, reject) => {
      try {
        let peer = new RTCPeerConnection(turn);
        let offer = await peer.createOffer({
          offerToReceiveAudio: true,
          offerToReceiveVideo: true
        })
        let rtcDesc = new RTCSessionDescription(offer);
        await peer.setLocalDescription(rtcDesc);
        setTimeout(()=> {
          peer.close()
          console.log("fixAndroid close")
          resolve()
        }, 1000)
      } catch (e) {
        resolve()
        console.error("fixAndroid", e)
      }
    })

  }

参考:

相关推荐
alexhilton4 小时前
Android XR 开发入门完全指南
android·kotlin·android jetpack
2501_915909069 小时前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
淡淡的香烟11 小时前
AndroidAI使用心得
android
chjif12 小时前
Android 设备管控开发实战:无障碍如何准确识别当前前台 App?
android
chjif12 小时前
Android 设备管控开发实战:企业受控终端的 DNS 域名策略实现
android·华为·harmonyos
c2385614 小时前
MySQL 基础用法(下):查询进阶与核心特性
android·c语言·c++·mysql
math_hongfan14 小时前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
2501_9151063215 小时前
iOS 证书类型及作用说明 账号证书与签名配置的完整解读
android·ios·小程序·https·uni-app·iphone·webview
九皇叔叔15 小时前
RHEL9 安装 MySQL 8.4.11.LTS 版本
android·mysql·adb
海兰15 小时前
【数据库】tdsql(MySQL )的事务隔离级别
android·数据库·mysql