Last active
August 7, 2018 06:29
-
-
Save marikavertzonis/6507a8c6c38dd0121537bada29a7355c to your computer and use it in GitHub Desktop.
Sample demonstrates detection of a known bluetooth LE device, including a specfic service, characteristic and descriptor of it
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 ArcGIS.AppFramework 1.0 | |
import ArcGIS.AppFramework.Devices 1.0 | |
import ArcGIS.AppFramework.Positioning 1.0 | |
App { | |
id: app | |
width: 900 | |
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; | |
} | |
} | |
BinaryData { | |
id: binaryData | |
data: bluetoothCharacteristic.value | |
} | |
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: { | |
log(new Date() + " Found available service: " + service.name + " " + service.uuid); | |
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: { | |
for (var i = 0; i < bluetoothService.characteristics.count; i++) { | |
var c = bluetoothService.characteristics.get(i); | |
log(new Date() + " Found available characteristic: " + i + " " + c.name + " " + c.uuid); | |
if (c.uuid === characteristicUuid) { | |
bluetoothCharacteristic = c; | |
bluetoothCharacteristic.read(); | |
} | |
} | |
} | |
} | |
Connections { | |
target: bluetoothCharacteristic | |
onValueChanged: { | |
if (bluetoothCharacteristic.uuid === characteristicUuid) { | |
log(new Date() + " Found EXPECTED characteristic: " + binaryData.base64); | |
for (var j = 0; j < bluetoothCharacteristic.descriptors.count; j++) { | |
var d = bluetoothCharacteristic.descriptors.get(j); | |
log(new Date() + " Found available descriptor: " + d.name + " " + d.uuid); | |
if (d.uuid === descriptorUuid) { | |
bluetoothDescriptor = d; | |
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} | |
} | |
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