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

  }

参考:

相关推荐
用户69371750013842 小时前
曾经安卓开发人手一个的 EventBus,为什么现在没人爱用了?
android·android studio
九皇叔叔3 小时前
MySQL ORDER BY 性能优化详解:Using filesort、联合索引、ASC/DESC 与覆盖索引
android·adb
OriginCoding4 小时前
用 AI Agent 协作完成一个 Android TOTP 应用:从需求边界到 v1.0.0
android·ai编程
杉氧6 小时前
React 灵魂:深入剖析 Hooks 与副作用(Side Effects)陷阱
android·前端·react native
又见情义7 小时前
RK3568 + RTL8211F 网络唤醒(WOL)功能适配全记录
android·网络·驱动开发
用户0934077735147 小时前
HarmonyOS WPS Open SDK 实践:registerApp 鉴权与就绪门禁
android·typescript·harmonyos
jike_20267 小时前
会议离线录音APP:无网络记录能力对比
android·智能手机·语音识别
always_TT8 小时前
【Python 字符串格式化:format() 方法】
android·开发语言·python
恋猫de小郭9 小时前
AI 时代,一个优化 Flutter 的重复代码工具 Deslop
android·前端·flutter
SendTomo9 小时前
send.wang:基于浏览器WebRTC实现无客户端文件互传
网络·python·网络协议·webrtc·p2p