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

  }

参考:

相关推荐
00后程序员张6 小时前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
悠哉清闲8 小时前
不同车型drawable不同
android·开发语言
00后程序员张10 小时前
在 iOS 设备上同时监控 CPU、GPU 与内存的方法
android·ios·小程序·https·uni-app·iphone·webview
测试_AI_一辰10 小时前
项目实践笔记 9:打卡/日报Agent项目Bug 修改与稳定性收口(v1.0)
android·开发语言·人工智能·功能测试·ai编程·ab测试
马 孔 多 在下雨11 小时前
Kotlin协程进阶王炸之作-Kotlin的协程到底是什么
android·开发语言·kotlin
冬奇Lab11 小时前
【Kotlin系列15】多平台开发实战:一次编写,多端运行
android·开发语言·kotlin
Dxy123931021611 小时前
告别默认排序:MySQL自定义排序的“炼金术”
android·数据库·mysql
请叫我大虾11 小时前
发现一个jdk中ArrayList的小BUG
android·java·bug
一起养小猫11 小时前
Flutter for OpenHarmony 实战:双控制系统实现(按钮+键盘)
android·flutter·计算机外设·harmonyos
_李小白12 小时前
【Android 美颜相机】第十八天:GPUImageChromaKeyBlendFilter 解析
android·数码相机