public MainPage(IBleStack ble_stack) { Speed speed1 = new Speed(2, Speed.Unit.MeterPerSec); Speed speed2 = new Speed(7.2, Speed.Unit.KmPerHour); bool comp = (speed1 == speed2); comp = (speed1 != speed2); StartDiscoveryCommand = new RelayCommand(StartDiscovery); StopDiscoveryCommand = new RelayCommand(StopDiscovery); ConnectCommand = new RelayCommand(Connect); NotifCommand = new RelayCommand(Notifications); BindingContext = this; InitializeComponent(); lvDevices.ItemsSource = BleDevices; _ble_stack = ble_stack; _ble_stack.DeviceDiscovered += IBleStack_DeviceDiscovered; _ble_stack.DeviceLost += IBleStack_DeviceLost; _ble_stack.DeviceUpdated += IBleStack_DeviceUpdated; _ble_stack.DiscoveryComplete += IBleStack_DiscoveryComplete; }
private void IBleStack_DiscoveryComplete(IBleStack sender, BleDiscoveryStatus status) { ExecuteOnMainThread.BeginInvoke(() => { string discovery_status = "Discovery status : "; switch (status) { case BleDiscoveryStatus.BDS_COMPLETE: { discovery_status += "Complete!"; break; } case BleDiscoveryStatus.BDS_CANCELED: { discovery_status += "Canceled!"; break; } case BleDiscoveryStatus.BDS_FAILED: { discovery_status += "Failed!"; break; } default: { discovery_status += "Unknown..."; break; } } lbStatus.Text = discovery_status; }); }
private void IBleStack_DeviceUpdated(IBleStack sender, BleDeviceInformation device_info) { foreach (BleDeviceInformation ble_device_info in BleDevices) { if (ble_device_info.Id == device_info.Id) { ble_device_info.MacAddress = device_info.MacAddress; ble_device_info.IsConnectable = device_info.IsConnectable; ble_device_info.IsConnected = device_info.IsConnected; break; } } }
private void IBleStack_DeviceLost(IBleStack sender, BleDeviceInformation device_info) { int index = 0; foreach (BleDeviceInformation ble_device_info in BleDevices) { if (ble_device_info.Id == device_info.Id) { break; } else { index++; } } if (index < BleDevices.Count) { ExecuteOnMainThread.Invoke(() => { BleDevices.RemoveAt(index: index); }); } }
private void IBleStack_DeviceDiscovered(IBleStack sender, BleDeviceInformation device_info) { ExecuteOnMainThread.Invoke(() => { BleDevices.Add(device_info); }); }