- add android manifest
- add more fonts - add proper checks for bluetooth state and permission
This commit is contained in:
parent
6ad924f426
commit
35ff374658
3 changed files with 57 additions and 8 deletions
|
@ -27,3 +27,7 @@ HEADERS += \
|
|||
$$PWD/qbluetoothleuartclient.h \
|
||||
$$PWD/qbluetoothleuartdevice.h \
|
||||
$$PWD/qbluetoothleuartdevicemodel.h
|
||||
|
||||
android {
|
||||
QT += androidextras
|
||||
}
|
||||
|
|
|
@ -31,8 +31,26 @@ QBluetoothLeUartClient::~QBluetoothLeUartClient(){
|
|||
// ------------------------------
|
||||
|
||||
bool QBluetoothLeUartClient::startScanningForDevices(){
|
||||
if(this->state != Idle && this->state != ScanFinished)
|
||||
if(this->state != Idle && this->state != AdapterTurnedOff && this->state != ScanFinished && this->state != LocationPermissionDenied)
|
||||
return false;
|
||||
#ifdef Q_OS_ANDROID
|
||||
else if(this->state == LocationPermissionDenied) {
|
||||
// try to get permission
|
||||
QtAndroid::PermissionResultMap resultMap = QtAndroid::requestPermissionsSync({"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"}, 5000);
|
||||
bool resultBool = true;
|
||||
for(QtAndroid::PermissionResult result : resultMap) {
|
||||
if(result != QtAndroid::PermissionResult::Granted) {
|
||||
resultBool = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!resultBool) {
|
||||
emit this->scanningErrorOccured(LocationPermissionDeniedError);
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
this->availableDevicesModel->clear();
|
||||
|
||||
|
@ -161,10 +179,7 @@ bool QBluetoothLeUartClient::sendData(QString data, bool asynchronous){
|
|||
|
||||
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID)));
|
||||
|
||||
QByteArray Data;
|
||||
Data.append(data);
|
||||
|
||||
bluetoothService->writeCharacteristic(RxChar, Data, QLowEnergyService::WriteWithoutResponse);
|
||||
bluetoothService->writeCharacteristic(RxChar, data.toUtf8(), QLowEnergyService::WriteWithoutResponse);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -204,10 +219,33 @@ void QBluetoothLeUartClient::handleScanFinished()
|
|||
|
||||
void QBluetoothLeUartClient::handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
|
||||
{
|
||||
if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
|
||||
qWarning() << "Scanning ERROR: " << error;
|
||||
|
||||
this->availableDevices.clear();
|
||||
this->availableDevicesModel->clear();
|
||||
|
||||
if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError) {
|
||||
this->setState(AdapterTurnedOff);
|
||||
emit this->scanningErrorOccured(AdapterTurnedOffError);
|
||||
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
|
||||
}
|
||||
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError) {
|
||||
this->setState(AdapterTurnedOff);
|
||||
emit this->scanningErrorOccured(InputOutputError);
|
||||
}
|
||||
#ifdef Q_OS_ANDROID
|
||||
else if (error == QBluetoothDeviceDiscoveryAgent::UnknownError) {
|
||||
// check for permission error
|
||||
QtAndroid::PermissionResult fineLocationAccess = QtAndroid::checkPermission("android.permission.ACCESS_FINE_LOCATION");
|
||||
QtAndroid::PermissionResult coarseLocationAccess = QtAndroid::checkPermission("android.permission.ACCESS_COARSE_LOCATION");
|
||||
|
||||
if(fineLocationAccess != QtAndroid::PermissionResult::Granted || coarseLocationAccess != QtAndroid::PermissionResult::Granted) {
|
||||
this->setState(LocationPermissionDenied);
|
||||
emit this->scanningErrorOccured(LocationPermissionDeniedError);
|
||||
}
|
||||
else
|
||||
emit this->scanningErrorOccured(UnknownError);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
emit this->scanningErrorOccured(UnknownError);
|
||||
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
#include <QLowEnergyController>
|
||||
#include <QLowEnergyService>
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include <QtAndroidExtras>
|
||||
#endif
|
||||
|
||||
#ifdef QBluetoothLeUart_QML
|
||||
#include <QQmlApplicationEngine>
|
||||
#endif
|
||||
|
@ -127,6 +131,8 @@ public:
|
|||
*/
|
||||
enum BluetoothLeUartClientState {
|
||||
Idle = 0, /*!< Waiting for instrucions */
|
||||
AdapterTurnedOff, /*!< The bluetooth adapter is turned off */
|
||||
LocationPermissionDenied, /*!< The location permssion was denied and we are therfor unable to scan! */
|
||||
Scanning, /*!< Scanning for devices */
|
||||
ScanFinished, /*!< Scanning has finished, we are ready to connect */
|
||||
Connecting, /*!< Trying to connect */
|
||||
|
@ -139,7 +145,8 @@ public:
|
|||
enum BluetoothScanError {
|
||||
UnknownError,
|
||||
AdapterTurnedOffError,
|
||||
InputOutputError
|
||||
InputOutputError,
|
||||
LocationPermissionDeniedError
|
||||
};
|
||||
Q_ENUM(BluetoothScanError);
|
||||
|
||||
|
|
Loading…
Reference in a new issue