(BLE)WindowsでKonashiのアナログ入力を読む

ようやくできたよ・・・。
Windows 10のノートPCで動作確認。他は知らない。
ハマったポイントは、
characteristic.ReadValueAsync()メソッドが、デフォルトでキャッシュした値を返していたこと。
BluetoothCacheMode enumeration - Windows app development
にあるように、引数をunchachedにしたら、ちゃんと読めた。

以下ソース。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Popups;


namespace BreathMonitorKonashi {

    public sealed partial class MainPage : Page {

        private const string KONASHI_SERVICE = "229BFF00-03FB-40DA-98A7-B0DEF65C2D4B";
        private const string KONASHI_AIO0_CHARACTERISTIC = "229B3008-03FB-40DA-98A7-B0DEF65C2D4B";

        private GattDeviceService service;
        private GattCharacteristic characteristic;

        Timer timer;


        public MainPage() {
            this.InitializeComponent();
        }

        private async void btnConnect_Click(object sender, RoutedEventArgs e) {
            //デバイスを検索
            var selector = GattDeviceService.GetDeviceSelectorFromUuid(new Guid(KONASHI_SERVICE));
            var deviceInfomations = await DeviceInformation.FindAllAsync(selector);
            var deviceInfomation = deviceInfomations.FirstOrDefault();
            if (deviceInfomation != null) {
                //サービス取得
                this.service = await GattDeviceService.FromIdAsync(deviceInfomation.Id);

                //キャラクタリスティック取得
                var characteristics = this.service.GetCharacteristics(new Guid(KONASHI_AIO0_CHARACTERISTIC));
                if (characteristics.Count > 0) {
                    this.characteristic = characteristics.First();
                    
                    var dialog = new MessageDialog("Connected to " + deviceInfomation.Name);
                    await dialog.ShowAsync();

                    //ADC読み取りスレッド開始
                    TimerCallback timerDelegate = new TimerCallback(onTimer);
                    timer = new Timer(timerDelegate, null, 0, 100);
                }
            }
        }


        private async void onTimer(object sender) {
            if (characteristic == null) {
                return ;
            }
            
            var result = await characteristic.ReadValueAsync(BluetoothCacheMode.Uncached);  //キャッシュしてやがった!
            byte[] value = result.Value.ToArray();
            
            int val = (value[0] << 8) + value[1];   //[0]番目に上位バイト、[1]番目に下位バイト
            System.Diagnostics.Debug.WriteLine(val);

            lastVal = val;
            
        }


    }
}