static void VehicleResponse(GattCharacteristic sender, GattValueChangedEventArgs args) { var rawBytes = readGattResult(args.CharacteristicValue); var anki_vehicle_msg = new anki_vehicle_msg(); anki_vehicle_msg.size = rawBytes[0]; anki_vehicle_msg.msg_id = rawBytes[1]; if (rawBytes.Length > 2) { anki_vehicle_msg.payload = new byte[rawBytes.Length - 2]; Array.Copy(rawBytes, 2, anki_vehicle_msg.payload, 0, rawBytes.Length - 2); } switch (anki_vehicle_msg.msg_id) { case (byte)AnkiMessage.ANKI_VEHICLE_MSG_V2C_PING_RESPONSE: { Console.WriteLine("[read] PING_RESPONSE\n"); break; } case (byte)AnkiMessage.ANKI_VEHICLE_MSG_V2C_VERSION_RESPONSE: { Console.WriteLine("[read] VERSION_RESPONSE: {0}", BitConverter.ToUInt16(anki_vehicle_msg.payload, 0)); break; } case (byte)AnkiMessage.ANKI_VEHICLE_MSG_V2C_BATTERY_LEVEL_RESPONSE: { Console.WriteLine("[read] BATTERY_RESPONSE: {0}", BitConverter.ToUInt16(anki_vehicle_msg.payload, 0)); break; } default: { Console.WriteLine("[read] RESPONSE: {0}", GetHexStringFrom(anki_vehicle_msg.payload)); break; } } }
public static async void Interact(anki_vehicle vehicle, GattCharacteristic readChar, GattCharacteristic writeChar) { try { var writer = new Windows.Storage.Streams.DataWriter(); byte[] rawBytes = null; GattCommunicationStatus result; readChar.ValueChanged += VehicleResponse; if (readChar.CharacteristicProperties == (GattCharacteristicProperties.Read | GattCharacteristicProperties.Notify)) { result = await readChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); } while (true) { Console.Write("Please enter your command: "); string input = Console.ReadLine(); if (input == "exit") { return; } switch (input) { case "ping": { anki_vehicle_msg msg = new anki_vehicle_msg(); msg.size = ANKI_VEHICLE_MSG_BASE_SIZE; msg.msg_id = (byte)AnkiMessage.ANKI_VEHICLE_MSG_C2V_PING_REQUEST; rawBytes = getBytes(msg); writer.WriteBytes(rawBytes); result = await writeChar.WriteValueAsync(writer.DetachBuffer()); break; } case "get-version": { anki_vehicle_msg msg = new anki_vehicle_msg(); msg.size = ANKI_VEHICLE_MSG_BASE_SIZE; msg.msg_id = (byte)AnkiMessage.ANKI_VEHICLE_MSG_C2V_VERSION_REQUEST; rawBytes = getBytes(msg); writer.WriteBytes(rawBytes); result = await writeChar.WriteValueAsync(writer.DetachBuffer()); break; } case "get-battery": { anki_vehicle_msg msg = new anki_vehicle_msg(); msg.size = ANKI_VEHICLE_MSG_BASE_SIZE; msg.msg_id = (byte)AnkiMessage.ANKI_VEHICLE_MSG_C2V_BATTERY_LEVEL_REQUEST; rawBytes = getBytes(msg); writer.WriteBytes(rawBytes); result = await writeChar.WriteValueAsync(writer.DetachBuffer()); break; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }