public static MetaWearBoard getMetaWearBoardInstance(BluetoothLEDevice btleDevice) { MetaWearBoard board; if (!instances.TryGetValue(btleDevice.BluetoothAddress, out board)) { board = new MetaWearBoard(btleDevice); instances.Add(btleDevice.BluetoothAddress, board); } return(board); }
private async void ConnectButton_Click() { ConnectButton.IsEnabled = false; ClearBluetoothLEDevice(); try { // BT_Code: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent. bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(rootPage.SelectedBleDeviceId); //Victor add device var board = MetaWearBoard.getMetaWearBoardInstance(bluetoothLeDevice); board.Initialize(); cppBoard = board.cppBoard; IntPtr accSignal = mbl_mw_acc_get_acceleration_data_signal(cppBoard); mbl_mw_datasignal_subscribe(accSignal, accDataHandler); mbl_mw_acc_enable_acceleration_sampling(cppBoard); mbl_mw_acc_start(cppBoard); } catch (Exception ex) when((uint)ex.HResult == 0x800710df) { // ERROR_DEVICE_NOT_AVAILABLE because the Bluetooth radio is not on. } if (bluetoothLeDevice != null) { // BT_Code: GattServices returns a list of all the supported services of the device. // If the services supported by the device are expected to change // during BT usage, subscribe to the GattServicesChanged event. foreach (var service in bluetoothLeDevice.GattServices) { ServiceCollection.Add(new BluetoothLEAttributeDisplay(service)); } ConnectButton.Visibility = Visibility.Collapsed; ServiceList.Visibility = Visibility.Visible; } else { ClearBluetoothLEDevice(); rootPage.NotifyUser("Failed to connect to device.", NotifyType.ErrorMessage); } ConnectButton.IsEnabled = true; }