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)
      }
    })

  }

参考:

相关推荐
肖。35487870948 小时前
[技巧-11]AndroidManifest.xml完善小技巧。
android
小羊子说8 小时前
Android 车机开发中常用的adb 脚本(更新中)
android·linux·adb·性能优化·车载系统
用户7607495397838 小时前
Android页面四大布局运行结果
android
风往哪边走8 小时前
搜索框自定义
android
用户8249281925369 小时前
把android资源类型详解
android
IT观测9 小时前
深度分析俩款主流移动统计工具Appvue和openinstall
android·java·数据库
用户338675581959 小时前
Android 四种常用布局完全解析(附实战项目截图)
android
用户5087532168449 小时前
Android 资源类型全解析:深入理解四种常用布局
android
XiaoLeisj9 小时前
Android 文件存储实战:从应用私有目录读写到网络文件落盘与公共存储接入
android·java·网络·文件操作
恋猫de小郭10 小时前
Android Studio Panda 2 ,支持 AI 用 Vibe Coding 创建项目
android·前端·flutter