private void _btManager_AdvertismentReceived(IBlueteraManager sender, BlueteraAdvertisement args) { Dispatcher.InvokeAsync(async() => { try { // avoid race if (ApplicationState != ApplicationStateType.Scanning) { return; } _btManager.StopScan(); _dataRateMeter.Reset(); ApplicationState = ApplicationStateType.Connecting; UpdateControls(); // Try connecting to Bluetera _bluetera = await _btManager.Connect(args.Address); // This method will either connect or throw _bluetera.ConnectionStatusChanged += _bluetera_ConnectionStatusChanged; _bluetera.DownlinkMessageReceived += _bluetera_DownlinkMessageReceived; // Start IMU. Methods will throw on failure await StartImu(); // update UI ApplicationState = ApplicationStateType.Connected; UpdateControls(); } catch (BlueteraException ex) { _bluetera?.Disconnect(); _bluetera = null; ApplicationState = ApplicationStateType.Error; UpdateControls(); } }); }
static void Main(string[] args) { // Note // When running in non-UWP desktop applications like this one, you must either: // - pair with the Bluetera from Windows Settings before running this example // - uncomment the relevant lines in the sdkConnect() method Console.WriteLine("Running"); BlueteraManager sdk = BlueteraManager.Instance; sdk.AdvertismentReceived += BlueteraDevice_AdvertismentReceived; bool running = true; Console.WriteLine("\n\n'q' - quit\n's' - start scan\n't' - stop scan\n'c' - connect\n'd' - disconnect\n'e' - send Echo\n'i' - configure and start IMU\n'j' - stop IMU\n'l' - start IMU and logging\n\n"); while (running) { var key = Console.ReadKey(true); switch (key.KeyChar) { case 'q': Console.WriteLine("Quitting"); running = false; break; case 's': Console.WriteLine("Starting Scan"); sdk.StartScan(); break; case 't': Console.WriteLine("Stopping Scan"); sdk.StopScan(); break; case 'c': Console.WriteLine("Connecting"); sdk.StopScan(); device = sdk.Connect(lastAddress, autopair: true).Result; // Note: non-UWP apps (like this one) should set autopair:true. See BlueteraSdk.Connect() for more info device.ConnectionStatusChanged += Device_ConnectionStatusChanged; device.DownlinkMessageReceived += Device_DownlinkMessageReceived; Console.WriteLine($"Device connection status: {device.ConnectionStatus}"); break; case 'd': Console.WriteLine("Disconnecting"); if (device != null) { device.Disconnect(); device = null; } break; case 'e': { Console.WriteLine("Sending echo"); var result = SendEcho().Result; Console.WriteLine($"GattCommunicationStatus: {result}"); } break; case 'i': { Console.WriteLine("Configuring and starting IMU"); var result = StartImu().Result; Console.WriteLine($"GattCommunicationStatus: {result}"); } break; case 'j': { Console.WriteLine("Stopping IMU"); var result = StopImu().Result; Console.WriteLine($"GattCommunicationStatus: {result}"); } break; case 'l': { Console.WriteLine("Configuring and starting IMU with logging to SD card"); var result = StartLogging().Result; Console.WriteLine($"GattCommunicationStatus: {result}"); } break; default: Console.WriteLine("Invalid command"); break; } } sdk.DisposeAll(); }