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

  }

参考:

相关推荐
风浅月明2 小时前
[Android]应用内更新问题
android
当归10242 小时前
接雨水的算法
android·java·算法
猿小帅014 小时前
androidnetflix手机版遥控器操作
android·framework
l and4 小时前
Android Http-server 本地 web 服务
android
CYRUS STUDIO4 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·汇编·arm开发·arm·逆向·jni
消失的旧时光-19435 小时前
Android 串口通信
android
风浅月明5 小时前
[Android]使用WorkManager循环执行任务
android
_extraordinary_6 小时前
Linux权限(一)
android·linux·excel
人生!?7 小时前
给小米/红米手机root(工具基本为官方工具)——KernelSU篇
android·linux·智能手机
古苏8 小时前
Android输入事件传递流程系统源码级解析
android