private async void _server_ConnectionRequest(object sender, ResonanceListeningServerConnectionRequestEventArgs <BluetoothAdapter> e) { IResonanceTransporter transporter = ResonanceTransporter.Builder .Create() .WithAdapter(e.Accept()) .WithJsonTranscoding() .Build(); transporter.StateChanged += (x, ee) => { if (ee.NewState != ResonanceComponentState.Connected) { InvokeUI(() => { ConnectedDevices.Remove(transporter); }); } }; transporter.RegisterRequestHandler <ChatMessageRequest, ChatMessageResponse>(OnChatRequest); await transporter.ConnectAsync(); InvokeUI(() => { ConnectedDevices.Add(transporter); if (SelectedDevice == null) { SelectedDevice = transporter; } }); }
private async void DeviceFound(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs btAdv) { if (!ConnectedDevices.Contains(btAdv.Advertisement.LocalName) && _devices.Contains(btAdv.Advertisement.LocalName)) { await Dispatcher.RunAsync(CoreDispatcherPriority.Low, async() => { var device = await BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress); if (device.GattServices.Any()) { ConnectedDevices.Add(device.Name); device.ConnectionStatusChanged += async(sender, args) => { if (sender.ConnectionStatus == BluetoothConnectionStatus.Disconnected) { await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { ConnectedDevices .Remove( sender .Name); }); } }; SetupWaxStream(device); } else if (device.DeviceInformation.Pairing.CanPair && !device.DeviceInformation.Pairing.IsPaired) { await device.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None); } }); } }
void device_Disconnected(object sender, EventArgs e) { ReportWiimote wiimote = (ReportWiimote)sender; MsHidDeviceInfo deviceInfo = (MsHidDeviceInfo)wiimote.DeviceInfo; wiimote.Disconnected -= device_Disconnected; ConnectedDevices.Remove(wiimote); MsHidDeviceProviderHelper.SetDevicePathConnected(deviceInfo.DevicePath, false); OnDeviceDisconnected(new DeviceEventArgs(wiimote)); }
protected BluetoothLEManager() { CentralManager = new CBCentralManager(DispatchQueue.CurrentQueue); CentralManager.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) => { Console.WriteLine("DiscoveredPeripheral: " + e.Peripheral.Name); DiscoveredDevices.Add(e.Peripheral); DeviceDiscovered(this, e); }; CentralManager.UpdatedState += (object sender, EventArgs e) => { Console.WriteLine("UpdatedState: " + CentralManager.State); }; CentralManager.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) => { Console.WriteLine("ConnectedPeripheral: " + e.Peripheral.Name); // When a peripheral gets connected, add that peripheral to our running list of // connected peripherals if (!ConnectedDevices.Contains(e.Peripheral)) { ConnectedDevices.Add(e.Peripheral); } // raise our connected event DeviceConnected(sender, e); }; CentralManager.DisconnectedPeripheral += (object sender, CBPeripheralErrorEventArgs e) => { Console.WriteLine("DisconnectedPeripheral: " + e.Peripheral.Name); // When a peripheral disconnects, remove it from our running list. if (ConnectedDevices.Contains(e.Peripheral)) { ConnectedDevices.Remove(e.Peripheral); } // Raise our disconnected event DeviceDisconnected(sender, e); }; }
protected BluetoothLeManager() { CentralBleManager = new CBCentralManager(DispatchQueue.CurrentQueue); DiscoveredDevices = new List <CBPeripheral>(); CentralBleManager.DiscoveredPeripheral += (sender, e) => { Mvx.Trace("DiscoveredPeripheral: {0}", e.Peripheral.Name); DiscoveredDevices.Add(e.Peripheral); DeviceDiscovered(this, e); }; CentralBleManager.UpdatedState += (sender, e) => { Mvx.Trace("UpdatedState: {0}", CentralBleManager.State); }; CentralBleManager.ConnectedPeripheral += (sender, e) => { Mvx.Trace("ConnectedPeripheral: " + e.Peripheral.Name); // when a peripheral gets connected, add that peripheral to our running list of connected peripherals if (!ConnectedDevices.Contains(e.Peripheral)) { ConnectedDevices.Add(e.Peripheral); } // raise our connected event DeviceConnected(sender, e); }; CentralBleManager.DisconnectedPeripheral += (sender, e) => { Mvx.Trace("DisconnectedPeripheral: " + e.Peripheral.Name); // when a peripheral disconnects, remove it from our running list. if (ConnectedDevices.Contains(e.Peripheral)) { ConnectedDevices.Remove(e.Peripheral); } // raise our disconnected event DeviceDisconnected(sender, e); }; }
protected Adapter() { CentralManager = new CBCentralManager(DispatchQueue.CurrentQueue); CentralManager.DiscoveredPeripheral += (object sender, CBDiscoveredPeripheralEventArgs e) => { Console.WriteLine("DiscoveredPeripheral: " + e.Peripheral.Name); Device d = new Device(e.Peripheral); if (!ContainsDevice(DiscoveredDevices, e.Peripheral)) { DiscoveredDevices.Add(d); DeviceDiscovered(this, new DeviceDiscoveredEventArgs() { Device = d }); } }; CentralManager.UpdatedState += (object sender, EventArgs e) => { Console.WriteLine("UpdatedState: " + CentralManager.State); _stateChanged.Set(); }; CentralManager.ConnectedPeripheral += (object sender, CBPeripheralEventArgs e) => { Console.WriteLine("ConnectedPeripheral: " + e.Peripheral.Name); // When a peripheral gets connected, add that peripheral to our running list of // connected peripherals if (!ContainsDevice(ConnectedDevices, e.Peripheral)) { Device d = new Device(e.Peripheral); ConnectedDevices.Add(new Device(e.Peripheral)); // raise our connected event DeviceConnected(sender, new DeviceConnectionEventArgs() { Device = d }); } }; CentralManager.DisconnectedPeripheral += (object sender, CBPeripheralErrorEventArgs e) => { Console.WriteLine("DisconnectedPeripheral: " + e.Peripheral.Name); // when a peripheral disconnects, remove it from our running list. IDevice foundDevice = null; foreach (var d in ConnectedDevices) { if (d.ID == Guid.ParseExact(e.Peripheral.Identifier.AsString(), "d")) { foundDevice = d; } } if (foundDevice != null) { ConnectedDevices.Remove(foundDevice); } // raise our disconnected event DeviceDisconnected(sender, new DeviceConnectionEventArgs() { Device = new Device(e.Peripheral) }); }; CentralManager.FailedToConnectPeripheral += (object sender, CBPeripheralErrorEventArgs e) => { // raise the failed to connect event DeviceFailedToConnect(this, new DeviceConnectionEventArgs() { Device = new Device(e.Peripheral), ErrorMessage = e.Error.Description }); }; }