- 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/qbluetoothleuartclient.h \
|
||||||
$$PWD/qbluetoothleuartdevice.h \
|
$$PWD/qbluetoothleuartdevice.h \
|
||||||
$$PWD/qbluetoothleuartdevicemodel.h
|
$$PWD/qbluetoothleuartdevicemodel.h
|
||||||
|
|
||||||
|
android {
|
||||||
|
QT += androidextras
|
||||||
|
}
|
||||||
|
|
|
@ -31,8 +31,26 @@ QBluetoothLeUartClient::~QBluetoothLeUartClient(){
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
|
|
||||||
bool QBluetoothLeUartClient::startScanningForDevices(){
|
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;
|
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();
|
this->availableDevicesModel->clear();
|
||||||
|
|
||||||
|
@ -161,10 +179,7 @@ bool QBluetoothLeUartClient::sendData(QString data, bool asynchronous){
|
||||||
|
|
||||||
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID)));
|
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID)));
|
||||||
|
|
||||||
QByteArray Data;
|
bluetoothService->writeCharacteristic(RxChar, data.toUtf8(), QLowEnergyService::WriteWithoutResponse);
|
||||||
Data.append(data);
|
|
||||||
|
|
||||||
bluetoothService->writeCharacteristic(RxChar, Data, QLowEnergyService::WriteWithoutResponse);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -204,10 +219,33 @@ void QBluetoothLeUartClient::handleScanFinished()
|
||||||
|
|
||||||
void QBluetoothLeUartClient::handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
|
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);
|
emit this->scanningErrorOccured(AdapterTurnedOffError);
|
||||||
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
|
}
|
||||||
|
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError) {
|
||||||
|
this->setState(AdapterTurnedOff);
|
||||||
emit this->scanningErrorOccured(InputOutputError);
|
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
|
else
|
||||||
emit this->scanningErrorOccured(UnknownError);
|
emit this->scanningErrorOccured(UnknownError);
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,10 @@
|
||||||
#include <QLowEnergyController>
|
#include <QLowEnergyController>
|
||||||
#include <QLowEnergyService>
|
#include <QLowEnergyService>
|
||||||
|
|
||||||
|
#ifdef Q_OS_ANDROID
|
||||||
|
#include <QtAndroidExtras>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef QBluetoothLeUart_QML
|
#ifdef QBluetoothLeUart_QML
|
||||||
#include <QQmlApplicationEngine>
|
#include <QQmlApplicationEngine>
|
||||||
#endif
|
#endif
|
||||||
|
@ -127,6 +131,8 @@ public:
|
||||||
*/
|
*/
|
||||||
enum BluetoothLeUartClientState {
|
enum BluetoothLeUartClientState {
|
||||||
Idle = 0, /*!< Waiting for instrucions */
|
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 */
|
Scanning, /*!< Scanning for devices */
|
||||||
ScanFinished, /*!< Scanning has finished, we are ready to connect */
|
ScanFinished, /*!< Scanning has finished, we are ready to connect */
|
||||||
Connecting, /*!< Trying to connect */
|
Connecting, /*!< Trying to connect */
|
||||||
|
@ -139,7 +145,8 @@ public:
|
||||||
enum BluetoothScanError {
|
enum BluetoothScanError {
|
||||||
UnknownError,
|
UnknownError,
|
||||||
AdapterTurnedOffError,
|
AdapterTurnedOffError,
|
||||||
InputOutputError
|
InputOutputError,
|
||||||
|
LocationPermissionDeniedError
|
||||||
};
|
};
|
||||||
Q_ENUM(BluetoothScanError);
|
Q_ENUM(BluetoothScanError);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue