private void HandleGetClosestDevice(MGBTCommandData cmd) { if (cmd.Status == 0) { InvocationHelper.InvokeIfRequired(this, new Action(() => { List <MGBTDevice> devices = MGBTDevice.FromArray(cmd.data); if (devices.Count > 0) { string riderName = "ILLEGAL"; string address = MacBytesToString(devices[0].Address).ToUpperInvariant(); if (riderNames.ContainsKey(address)) { riderName = riderNames[address]; } closestDeviceLbl.Text = devices[0].ToString() + "; " + riderName; } })); } else { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus(String.Format("Error response: {0}", cmd.Status)); })); } }
public static List <MGBTDevice> FromArray(byte[] data) { List <MGBTDevice> devices = new List <MGBTDevice>(); if ((data != null) && (data.Length > 0)) { try { var reader = new BinaryReader(new MemoryStream(data)); while (reader.BaseStream.Position < reader.BaseStream.Length) { var s = new MGBTDevice(); s.address = reader.ReadBytes(s.address.Length); s.rssi = reader.ReadInt16(); s.measuredPower = reader.ReadInt16(); s.measurePowerCorrection = reader.ReadInt16(); devices.Add(s); } } catch (Exception e) { } } return(devices); }
private void HandleListAllowedDevices(MGBTCommandData cmd) { if ((cmd.data.Length > 2) && (cmd.Status == 1)) { InvocationHelper.InvokeIfRequired(this, new Action(() => { if (cmd.data[0] == 0) { AllowedDevicesLbx.Items.Clear(); } byte[] devicesData = new byte[cmd.data.Length - 2]; Array.Copy(cmd.data, 2, devicesData, 0, devicesData.Length); List <MGBTDevice> devices = MGBTDevice.FromArray(devicesData); AllowedDevicesLbx.Items.AddRange(devices.ToArray()); })); } else { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus(String.Format("Data length or error response: {0}", cmd.Status)); })); } }
private void HandleListDetectedDevicesDeviceData(byte[] devicesData) { List <MGBTDevice> devices = MGBTDevice.FromArray(devicesData); detectedDevicesLbx.Items.AddRange(devices.ToArray()); }