Skip to content

Commit d03efaf

Browse files
committed
Complete example including events
1 parent 591ecdf commit d03efaf

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

ExampleClient/Program.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ private static async Task Main(string[] args)
1212
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("10.20.30.40"), 21067);
1313
QRParser parser = new QRParser("X-HM://00UQ3OYGA9CTI");
1414

15-
//Randomly generate keys and keep them in a safe place (or let the controller do it for you)
15+
//Randomly generate keys and keep them in a safe place (or let the controller do it for you using the other Controller constructor)
1616
byte[] DeviceID = Encoding.UTF8.GetBytes(Guid.Parse("cafef517-351a-4280-93f6-995d3b881587").ToString("D", CultureInfo.InvariantCulture));
1717
byte[] LTPK = [0xD8, 0x54, 0x66, 0x74, 0x20, 0xB0, 0x0F, 0xF7, 0x18, 0x67, 0xB6, 0xBD, 0x97, 0x6A, 0xE9, 0xE0, 0x34, 0x50, 0xD2, 0x18, 0x24, 0xEA, 0x5D, 0x72, 0x7A, 0xD8, 0xAD, 0xA2, 0x60, 0xEE, 0x59, 0xC3];
1818
byte[] LTSK = [0x9E, 0x07, 0x26, 0x1A, 0xA1, 0x36, 0x61, 0xDE, 0x6E, 0x73, 0x49, 0xBC, 0x0D, 0x91, 0x08, 0x9B, 0xEF, 0x58, 0x61, 0x5C, 0x5B, 0x2A, 0xC6, 0x01, 0x7C, 0x7C, 0xC4, 0xA1, 0x92, 0x37, 0x8D, 0x0C, 0xD8, 0x54, 0x66, 0x74, 0x20, 0xB0, 0x0F, 0xF7, 0x18, 0x67, 0xB6, 0xBD, 0x97, 0x6A, 0xE9, 0xE0, 0x34, 0x50, 0xD2, 0x18, 0x24, 0xEA, 0x5D, 0x72, 0x7A, 0xD8, 0xAD, 0xA2, 0x60, 0xEE, 0x59, 0xC3];
1919

20-
//Pair with the device (only 1 time) and then can connect for future sessions
2120
Controller controller = new Controller(LTSK, LTPK, DeviceID);
21+
//Pair with the device (only 1 time) and then call connect for future sessions
2222
var pairing = await controller.Pair(parser.SetupPin, ep);
23-
HomeKitEndPoint connection = await controller.Connect(ep, pairing.PairingID, pairing.LTPK);
23+
HomeKitEndPoint connection = await controller.Connect(ep, pairing.ID, pairing.LTPK);
2424

2525
//Once connected you can read and write any characteristics on any services
2626
AccessoryInformation info = (connection.Accessories[0].Services.First(s => s is AccessoryInformation) as AccessoryInformation)!;
@@ -31,7 +31,21 @@ private static async Task Main(string[] args)
3131
Console.WriteLine($"Device Version: {await info.FirmwareRevision.GetValue()}");
3232
Console.WriteLine($"Device Software Version: {info.SoftwareRevision?.LastValue}");
3333

34+
//Any characteristic that supports events can be subscribed to
35+
if (info.ConfiguredName != null)
36+
{
37+
await info.ConfiguredName.ToggleNotifications(true); //Subscribe
38+
info.ConfiguredName.Updated += ConfiguredName_Updated;
39+
}
40+
3441
//It's that easy
3542
Console.ReadLine();
3643
}
44+
45+
private static void ConfiguredName_Updated(object? sender, string? newValue)
46+
{
47+
Console.WriteLine("Previous Name: " + ((ConfiguredName)sender!).LastValue);
48+
Console.WriteLine("New Value: " + newValue);
49+
//After this event is handled, LastValue is set to newValue
50+
}
3751
}

0 commit comments

Comments
 (0)