13 using System.Security.Cryptography;
14 using System.Threading;
15 using Windows.Devices.Bluetooth.GenericAttributeProfile;
16 using Windows.Storage.Streams;
23 public static string ATS_UUID =
"0000fee1-0000-1000-8000-00805f9b34fb";
25 public static string HMS_UUID =
"0000180d-0000-1000-8000-00805f9b34fb";
26 public static string SNS_UUID =
"0000fee0-0000-1000-8000-00805f9b34fb";
29 public static string ATC_UUID =
"00000009-0000-3512-2118-0009af100700";
31 public static string HRM_UUID =
"00002a37-0000-1000-8000-00805f9b34fb";
32 public static string HMC_UUID =
"00002a39-0000-1000-8000-00805f9b34fb";
33 public static string SNC_UUID =
"00000001-0000-3512-2118-0009af100700";
86 if (servicesResult.Status == GattCommunicationStatus.Success && servicesResult.Services.Count > 0)
88 GattDeviceService service = servicesResult.Services[0];
90 GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsForUuidAsync(
new Guid(
ATC_UUID));
91 if (characteristicsResult.Status == GattCommunicationStatus.Success && characteristicsResult.Characteristics.Count > 0)
93 GattCharacteristic characteristic = characteristicsResult.Characteristics[0];
96 GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
97 if (status == GattCommunicationStatus.Success)
99 characteristic.ValueChanged += (GattCharacteristic sender, GattValueChangedEventArgs args) =>
101 DataReader reader = DataReader.FromBuffer(args.CharacteristicValue);
102 byte[] messageType =
new byte[3]; reader.ReadBytes(messageType);
104 if (messageType[1] == 0x01)
106 if (messageType[2] == 0x01)
112 callback.Invoke(
this,
false);
116 else if (messageType[1] == 0x02)
118 byte[] number =
new byte[reader.UnconsumedBufferLength];
119 reader.ReadBytes(number);
123 using (Aes aes = Aes.Create())
126 aes.Mode = CipherMode.ECB;
127 aes.Padding = PaddingMode.None;
129 ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key,
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
130 using (MemoryStream stream =
new MemoryStream())
132 using (CryptoStream cryptoStream =
new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
134 cryptoStream.Write(number, 0, number.Length);
135 cryptoStream.FlushFinalBlock();
136 encNumber = stream.ToArray();
142 using (MemoryStream stream =
new MemoryStream())
144 stream.Write(
new byte[] { 0x03, 0x08 }, 0, 2);
145 stream.Write(encNumber, 0, encNumber.Length);
149 else if (messageType[1] == 0x03)
151 if (messageType[2] == 0x01)
153 callback.Invoke(
this,
true);
157 callback.Invoke(
this,
false);
165 byte[] hash =
new SHA256Managed().ComputeHash(Guid.NewGuid().ToByteArray());
169 using (MemoryStream stream =
new MemoryStream())
171 stream.Write(
new byte[] { 0x01, 0x08 }, 0, 2);
192 if (servicesResult.Status == GattCommunicationStatus.Success && servicesResult.Services.Count > 0)
194 GattDeviceService service = servicesResult.Services[0];
195 GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsForUuidAsync(
new Guid(
SNC_UUID));
196 if (characteristicsResult.Status == GattCommunicationStatus.Success && characteristicsResult.Characteristics.Count > 0)
198 GattCharacteristic characteristic = characteristicsResult.Characteristics[0];
206 if (servicesResult.Status == GattCommunicationStatus.Success && servicesResult.Services.Count > 0)
208 GattDeviceService service = servicesResult.Services[0];
211 GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsForUuidAsync(
new Guid(
HRM_UUID));
212 if (characteristicsResult.Status == GattCommunicationStatus.Success && characteristicsResult.Characteristics.Count > 0)
216 GattCommunicationStatus status = await
HMRcharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
217 if (status == GattCommunicationStatus.Success)
219 HMRcharacteristic.ValueChanged += (GattCharacteristic sender, GattValueChangedEventArgs args) =>
221 DataReader reader = DataReader.FromBuffer(args.CharacteristicValue);
222 ushort heartrate = reader.ReadUInt16();
238 characteristicsResult = await service.GetCharacteristicsForUuidAsync(
new Guid(
HMC_UUID));
239 if (characteristicsResult.Status == GattCommunicationStatus.Success && characteristicsResult.Characteristics.Count > 0)
OnHeartrateChangedHandler HeartrateChanged
GattCharacteristic HMRcharacteristic
BluetoothLEDevice Device
MiBand(BLEManager bluetoothLEManager)
GattDeviceService HMSService
void StopMonitorHeartrate()
Stop heart rate reading
async void StartMonitorHeartrate()
async void Write(GattCharacteristic c, byte[] data)
delegate void OnHeartrateChangedHandler(MiBand device, ushort value)
delegate void OnAuthHandler(MiBand device, bool status)
GattCharacteristic SNSCharacteristic
GattCharacteristic HMCCharacteristic
async void Authenticate(OnAuthHandler callback=null)