private void TryWriteValue(object obj) { try { byte[] data = { BitConverter.GetBytes(EditableValue)[0] }; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(_parentPeripheral.Connection, _handle, data); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("connection={0}, handle={1}, data={2}", _parentPeripheral.Connection, Handle, BitConverter.ToString(data))); MessageWriter.BLEWrite(cmd); } catch (Exception ex) { MessageWriter.LogWrite("Error writing value!"); } }
private void SaveValue(object obj) { // Save value if (SelectedPeripheral.Characteristics.ContainsKey("RheostatSaveCommand")) { byte connection = SelectedPeripheral.Connection; ushort handle = SelectedPeripheral.Services["RheostatCalibrationService"].Characteristics["RheostatSaveCommand"].ValueAttribute.Handle; byte[] value = new byte[] { 0x01 }; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(connection, handle, value); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("handle={0}, att_handle={1}, data={2}", connection, handle, BitConverter.ToString(value))); MessageWriter.BLEWrite(cmd); } }
private void LPMExit() { // Wake up byte[] lpm_bit = new byte[] { 0x01 }; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(Connection, Characteristics["LPM"].ValueAttribute.Handle, lpm_bit); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("connection={0}, handle={1}, data={2}", Connection, Characteristics["LPM"].ValueAttribute.Handle, BitConverter.ToString(lpm_bit))); MessageWriter.BLEWrite(cmd); /* Track uptime * _uptimeDispatcher.IsEnabled = true; * if (_uptime.IsRunning == false) * { * _uptime.Start(); * }*/ }
private void OpenGATTEditor(object obj) { // Create window MessageWriter.LogWrite("Opening attribute editor..."); Window editor = new EditGATTValueWindow(); editor.DataContext = this; // Check current value byte[] cmd = bglib.BLECommandATTClientReadByHandle(_parentPeripheral.Connection, _handle); MessageWriter.LogWrite("ble_cmd_att_client_read_by_handle: ", string.Format("connection={0}, handle={1}", _parentPeripheral.Connection, _handle)); MessageWriter.BLEWrite(cmd); editor.Show(); }
public void Connect() { // Connection parameters float conn_interval_min = 40; // in ms float conn_interval_max = 60; // in ms float timeout = 2560; // in ms byte latency = 0; // Form a direct connection byte[] cmd = bglib.BLECommandGAPConnectDirect( Address, AddressType, (ushort)(conn_interval_min / 1.25F), (ushort)(conn_interval_max / 1.25F), (ushort)(timeout / 10F), latency); // 125ms interval, 125ms window, active scanning MessageWriter.LogWrite("ble_cmd_gap_connect_direct: ", string.Format("address={0}, address_type={1}, conn_interval_min={2}; conn_interval_max={3}, timeout={4}, latency={5}", BitConverter.ToString(Address), AddressType, conn_interval_min, conn_interval_max, timeout, latency)); MessageWriter.BLEWrite(cmd); // Initialize connecting timeout timer DispatcherTimer connectionTimer = new DispatcherTimer(); connectionTimer.Tick += new EventHandler(CheckConnection); connectionTimer.Interval = new TimeSpan(0, 0, 3); connectionTimer.Start(); // Track uptime _uptimeDispatcher.IsEnabled = true; if (_uptime.IsRunning == false) { _uptime.Start(); } // New save file SaveFile = null; ConnectionState = ConnState.Connecting; }
private void LPMEnter() { // Wake up byte[] lpm_bit = new byte[] { 0x00 }; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(Connection, Characteristics["LPM"].ValueAttribute.Handle, lpm_bit); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("connection={0}, handle={1}, data={2}", Connection, Characteristics["LPM"].ValueAttribute.Handle, BitConverter.ToString(lpm_bit))); MessageWriter.BLEWrite(cmd); /* Stop tracking uptime * _uptimeDispatcher.IsEnabled = false; * if (_uptime.IsRunning == true) * { * _uptime.Reset(); * NotifyPropertyChanged("Uptime"); * } * * // New save file * SaveFile = null;*/ }
public void Disconnect() { if (LowPowerMode == LPM.Disabled) { MessageWriter.LogWrite(Name + " entering LPM..."); LPMEnter(); } // Disconnect byte[] cmd = bglib.BLECommandConnectionDisconnect(Connection); MessageWriter.LogWrite("ble_cmd_connection_disconnect: ", string.Format("connection={0}", Connection)); MessageWriter.BLEWrite(cmd); // Stop tracking uptime _uptimeDispatcher.IsEnabled = false; if (_uptime.IsRunning == true) { _uptime.Reset(); NotifyPropertyChanged("Uptime"); } }
private void SendValue(object obj) { _rheostatPrev = RheostatValue; // Convert to byte array byte[] value = BitConverter.GetBytes(RheostatValue); if (BitConverter.IsLittleEndian) { value = value.Reverse().ToArray(); } // Set command bits to write to wiper (command 1) value[0] = (byte)(value[0] | 0x04); // Write rheostat value to peripheral if (SelectedPeripheral.Services.ContainsKey("RheostatCalibrationService")) { byte connection = SelectedPeripheral.Connection; ushort handle = SelectedPeripheral.Services["RheostatCalibrationService"].Characteristics["RheostatValue"].ValueAttribute.Handle; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(connection, handle, value); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("handle={0}, att_handle={1}, data={2}", connection, handle, BitConverter.ToString(value))); MessageWriter.BLEWrite(cmd); } else { // Legacy Write to rheostat (TODO V3 remove) byte connection = SelectedPeripheral.Connection; ushort handle = SelectedPeripheral.Characteristics["RheostatValue"].ValueAttribute.Handle; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(connection, handle, value); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("handle={0}, att_handle={1}, data={2}", connection, handle, BitConverter.ToString(value))); MessageWriter.BLEWrite(cmd); return; } }