BluetoothServerSocket在Android11上无法接受连接
创始人
2024-12-23 03:30:34
0

在 Android 11 上,Google更改了 Bluetooth 的权限模型,导致 BluetoothServerSocket 在应用程序处于后台或锁屏状态时无法接受连接。解决该问题的方法是在 AndroidManifest.xml 文件中添加以下权限:。 代码示例如下:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
    mBluetoothAdapter.getBluetoothLeAdvertiser().setLegacyMode(true); // Enable pairing with less secure devices (pre Android 10 devices)
    mBluetoothAdapter.getBluetoothLeAdvertiser().setAutoConnect(false);
    mBluetoothAdapter.setName("SomeDevice");

    mAdvertiseSettings = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM).setTimeout(0).setConnectable(true).build();
    mAdvertiseData = new AdvertiseData.Builder().setIncludeDeviceName(false).build();

    ParcelUuid pUuid = new ParcelUuid(UUID.fromString(getString(R.string.uuid)));
    mAdvertiseData = new AdvertiseData.Builder().addServiceUuid(pUuid).setIncludeDeviceName(false).build();

    Log.d("BLE", "Advertising started");
    mBluetoothLeAdvertiser.startAdvertising(mAdvertiseSettings, mAdvertiseData, mAdvertiseCallback);
}

相关内容

热门资讯

曝光透视!菠萝德州透视脚本,微... 曝光透视!菠萝德州透视脚本,微乐山西脚本插件,手筋教程(真的有挂)-哔哩哔哩1、菠萝德州透视脚本破解...
推荐透视!pokemmo手机辅... 推荐透视!pokemmo手机辅助软件,微乐自建房道具有用吗,窍门教程(有挂头条)-哔哩哔哩pokem...
总结透视!wepoker怎么开... 总结透视!wepoker怎么开辅助,微乐脚本,要领教程(有挂猫腻)-哔哩哔哩1、这是跨平台的wepo...
总结透视!wepoker透视底... 总结透视!wepoker透视底牌,微乐脚本辅助器,教程书教程(有挂存在)-哔哩哔哩1、起透看视 we...
辅助透视!wepoker透视方... 辅助透视!wepoker透视方法,微乐家乡游戏攻略,指南教程(果真有挂)-哔哩哔哩所有人都在同一条线...
了解透视!hhpoker底牌透... 了解透视!hhpoker底牌透视脚本,微乐家乡app下载,要领教程(有挂方法)-哔哩哔哩1、玩家可以...
专业透视!扑克之星辅助,微乐麻... 专业透视!扑克之星辅助,微乐麻辣自建房辅助视频,妙计教程(的确有挂)-哔哩哔哩1、实时扑克之星辅助透...
了解透视!hhpoker怎么开... 了解透视!hhpoker怎么开透视,微乐游戏辅助脚本安卓,方式教程(有挂讲解)-哔哩哔哩1、hhpo...
科普透视!wpk辅助哪里买,微... 科普透视!wpk辅助哪里买,微乐家乡官方app,技法教程(有挂教程)-哔哩哔哩1.wpk辅助哪里买 ...
分享透视!德州局透视,微信微乐... 分享透视!德州局透视,微信微乐游戏有辅助吗,练习教程(确实有挂)-哔哩哔哩1、下载好德州局透视正确养...