Created
November 20, 2018 00:05
-
-
Save marikavertzonis/9c0b6feac905cd248934091915ed0104 to your computer and use it in GitHub Desktop.
Similar to https://gist.github.com/marikavertzonis/6507a8c6c38dd0121537bada29a7355c this sample demonstrates detection of a known bluetooth LE device, including a specfic service, characteristic and descriptor of it. In this sample, code is leaner - focusing only on the expected values and not showing that others are found.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.7 | |
import QtQuick.Layouts 1.3 | |
import ArcGIS.AppFramework 1.0 | |
import ArcGIS.AppFramework.Devices 1.0 | |
import ArcGIS.AppFramework.Positioning 1.0 | |
App { | |
id: app | |
width: 500 | |
height: 800 | |
//These 4 property values are the same for any vívofit device. Change these values to detect other devices. | |
property string bluetoothDeviceName: "vívofit" | |
property string serviceUuid: "{9b012401-bc30-ce9a-e111-0f67e491abde}" | |
property string characteristicUuid: "{4acbcd28-7425-868e-f447-915c8f00d0cb}" | |
property string descriptorUuid: "{00002902-0000-1000-8000-00805f9b34fb}" | |
property BluetoothLEDevice bluetoothDevice: discoveryAgent.devices.count > 0 ? discoveryAgent.devices.get(0) : null | |
property BluetoothLEService bluetoothService: null | |
property BluetoothLECharacteristic bluetoothCharacteristic: null | |
property BluetoothLEDescriptor bluetoothDescriptor: null | |
DeviceDiscoveryAgent { | |
id: discoveryAgent | |
deviceFilter: function(device) { | |
return device.name === bluetoothDeviceName; | |
} | |
} | |
Component.onCompleted: { | |
log("To connect to a nearby " + bluetoothDeviceName + ", ensure sync is enabled on the device.") | |
log(new Date() + " Searching for bluetooth device: " + bluetoothDeviceName); | |
discoveryAgent.start(); | |
} | |
onBluetoothDeviceChanged: { | |
log(new Date() + " Expected bluetooth device found: "); | |
JSON.stringify(bluetoothDevice, undefined, 2).split("\n").forEach(log); | |
bluetoothDevice.connected = true; | |
discoveryAgent.stop(); | |
} | |
Connections { | |
target: bluetoothDevice | |
onConnectCompleted: log(new Date() + " Device connect completed") | |
onServiceDiscoveryCompleted: log(new Date() + " Service discovery completed") | |
onServiceDiscovered: { | |
if (service.uuid === serviceUuid) { | |
log(new Date() + " Found EXPECTED service: " + service.name + " " + service.uuid); | |
bluetoothService = service; | |
bluetoothService.discoverDetails(); | |
} | |
} | |
onErrorChanged: { | |
log(new Date() + " Found error: " + bluetoothDevice.error); | |
} | |
} | |
Connections { | |
target: bluetoothService | |
onCharacteristicsChanged: { | |
bluetoothCharacteristic = bluetoothService.characteristics.getByUuid(characteristicUuid) | |
bluetoothCharacteristic.read() | |
} | |
} | |
Connections { | |
target: bluetoothCharacteristic | |
onValueChanged: { | |
if (bluetoothCharacteristic.uuid === characteristicUuid) { | |
log(new Date() + " Found EXPECTED characteristic: " + bluetoothCharacteristic.uuid); | |
bluetoothDescriptor = bluetoothCharacteristic.descriptors.getByUuid(descriptorUuid); | |
bluetoothDescriptor.read(); | |
} | |
} | |
} | |
Connections { | |
target: bluetoothDescriptor | |
onValueChanged: { | |
if (bluetoothDescriptor.uuid === descriptorUuid) { | |
log(new Date() + " Found EXPECTED descriptor: " + bluetoothDescriptor.uuid); | |
} | |
} | |
} | |
ListView { | |
anchors.fill: parent | |
anchors.margins: 5 | |
model: messagesListModel | |
delegate: Text {text: msg} | |
clip: true | |
} | |
ListModel { | |
id: messagesListModel | |
} | |
function log(txt) { | |
console.log(txt); | |
messagesListModel.append( { msg: txt } ); | |
if (messagesListModel.count > 40) { messagesListModel.remove(0); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment