102 lines
2.6 KiB
QML
102 lines
2.6 KiB
QML
import QtQuick 2.15
|
|
import QtQuick.Controls 2.15
|
|
import QtQuick.Window 2.15
|
|
import de.itsblue.bluetoothleuart 1.0
|
|
|
|
Window {
|
|
width: 640
|
|
height: 480
|
|
visible: true
|
|
title: qsTr("Hello World")
|
|
|
|
Page {
|
|
id: app
|
|
|
|
anchors.fill: parent
|
|
|
|
property int remoteState: -1
|
|
property bool foundLamp: false
|
|
|
|
QBluetoothLeUart {
|
|
id: ble
|
|
Component.onCompleted: {
|
|
ble.startScanningForDevices()
|
|
}
|
|
|
|
onFoundNewDevice: {
|
|
console.log("Found a device: name: " + device.name + " address: " + device.address)
|
|
if(device.address === "08:B6:1F:38:17:16") {
|
|
app.foundLamp = true
|
|
ble.stopScanningForDevices()
|
|
ble.connectToDevice(device)
|
|
}
|
|
}
|
|
|
|
onConnectedToDevice: {
|
|
console.log("Connected")
|
|
ble.sendData("s")
|
|
}
|
|
|
|
onDataReceived: {
|
|
console.log("Data received: " + data)
|
|
if(data.startsWith("s:")) {
|
|
app.remoteState = parseInt(data.slice(2), 16)
|
|
}
|
|
}
|
|
|
|
onStateChanged: {
|
|
console.log("state", state)
|
|
switch(state) {
|
|
case QBluetoothLeUart.ScanFinished:
|
|
case QBluetoothLeUart.Idle:
|
|
if(!app.foundLamp) ble.startScanningForDevices()
|
|
break;
|
|
case QBluetoothLeUart.Connecting:
|
|
app.foundLamp = false
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
Loader {
|
|
anchors.fill: parent
|
|
sourceComponent:
|
|
ble.state === QBluetoothLeUart.Connected && app.remoteState !== -1 ? controllerComponent :
|
|
ble.state === QBluetoothLeUart.LocationPermissionDenied ? permissionComponent:
|
|
ble.state === QBluetoothLeUart.AdapterTurnedOff ? bluetoothOffComponent:
|
|
loadingComponent
|
|
}
|
|
|
|
Component {
|
|
id: loadingComponent
|
|
Page {
|
|
anchors.fill: parent
|
|
BusyIndicator {
|
|
anchors.centerIn: parent
|
|
}
|
|
}
|
|
}
|
|
|
|
Component {
|
|
id: controllerComponent
|
|
ControllerPage {
|
|
}
|
|
}
|
|
|
|
Component {
|
|
id: permissionComponent
|
|
PermissionPage {
|
|
}
|
|
}
|
|
|
|
Component {
|
|
id: bluetoothOffComponent
|
|
BluetoothOffPage {
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|