QT中对蓝牙权限的申请,整理一下
-
蓝牙的连接类中,使用权限申请
public: static void requestBluetoothPermission(BLE_XX *instance); signal: void permessionGrant(const int status); void BLE_XX::requestBluetoothPermission(BLE_XX *instance) { QBluetoothPermission permission; permission.setCommunicationModes(QBluetoothPermission::Access); auto *app = QCoreApplication::instance(); if (!app) { qWarning() << "No QCoreApplication instance"; return; } // 检查当前权限状态 Qt::PermissionStatus currentStatus = app->checkPermission(permission); // 有权限就直接返回 if (currentStatus == Qt::PermissionStatus::Granted){ s_btReady = true; emit instance->permessionGrant(0); return; } app->requestPermission( permission, nullptr, [instance](const QPermission &perm) { if (perm.status() == Qt::PermissionStatus::Granted) { qInfo() << "Bluetooth permission granted"; s_btReady = true; // QTimer::singleShot(5000, [instance](){ // // instance->searchDevices(); // // instance->searchIfSerachable(instance); // }); emit instance->permessionGrant(1); } else { qWarning() << "Bluetooth permission denied"; s_btReady = false; emit instance->permessionGrant(2); } } ); } void BLE_XX::checkBluetoothPermission() { requestBluetoothPermission(this); } -
在主程序中调用
BLE_XX *c_ble = new BLE_XX(); engine.rootContext()->setContextProperty("c_bluetooth", c_ble); QObject::connect(c_ble, &BLE_XX::permessionGrant, &g1, [&g1](int status){ switch(status){ case 1: g1.changeDevice(0); g1.startSearchDevice(); break; } }); c_ble->checkBluetoothPermission();在此程序中,实例化时也绑定了槽,如果没有权限,在授权后需要重新实例化。在结构上,没有把权限的检查申请放在蓝牙连接类的构造函数中,因为在初次实例化时不能触发槽函数
捎带产生的另一个问题
环境没有做什么改动,在Debug模式下正常,但是切换到Release时就不正常了,提示:
ninja: no work to do
什么也没干,因为没生成so文件的问题,无法继续。依赖习惯了自动的配置,这个还是拖了一点时间,后来比较发现,在项目中,正常应该是这样的:
# 构建目录
/xxx/build/xx_arm64_v8a-Release
# 构建步骤
cmake --build /xxx/build/xx_arm64_v8a-Release --target all
# 清除的步骤
cmake --build /xxx/build/xx_arm64_v8a-Release --target clean
这个错误来源于构建步骤这一步出现了错误,配置成了一个不是来源的目录
cmake --build /xxx/build/xx_arm64_v8a-Release --target xxx_other_files