예제 #1
0
        async void OnAppServiceConnectionRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            var deferral = args.GetDeferral();

            var requestData = args.Request.Message;

            if (!this.IsValidRequestDataSet(requestData, out Guid characteristicUuid, out string data))
            {
                await args.SendResponseAsync(this.GetFailureValueSet("invalid data"));

                deferral.Complete();
                return;
            }

            var deviceId = SettingsHelper.GetValue(Constants.LastSavedDeviceIdSettingKey, string.Empty);

            if (string.IsNullOrWhiteSpace(deviceId))
            {
                await args.SendResponseAsync(this.GetFailureValueSet("device not found"));

                deferral.Complete();
                return;
            }

            var errorMessage = await DeviceManager.ConnectAsync(deviceId, isBackgroundActivity : true);

            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                await args.SendResponseAsync(this.GetFailureValueSet("connection failed"));

                deferral.Complete();
                return;
            }

            var result = await DeviceManager.WriteByteArrayToCharacteristicAsync(characteristicUuid, Encoding.UTF8.GetBytes(data));

            await args.SendResponseAsync(this.GetSuccessValueSet());

            await DeviceManager.DisconnectAsync(removeLastDeviceInfo : false);

            deferral.Complete();
        }