@@ -52,7 +52,7 @@ static HANDLE OpenBluetoothDevice(const GUID* interfaceGUID)
52
52
53
53
if (SetupDiGetDeviceInterfaceDetail (hDevInfo, &deviceInterfaceData, pInterfaceDetailData, size, &size, &devInfoData))
54
54
{
55
- OutputDebugFormat (_T (" Found PowerMate Bluetooth: %s" ), pInterfaceDetailData->DevicePath );
55
+ OutputDebugFormat (_T (" Found PowerMate Bluetooth: %s\n " ), pInterfaceDetailData->DevicePath );
56
56
hResult = CreateFile (
57
57
pInterfaceDetailData->DevicePath ,
58
58
GENERIC_READ,
@@ -83,55 +83,35 @@ static void ValueChangedEventHandler(BTH_LE_GATT_EVENT_TYPE EventType, PVOID Eve
83
83
84
84
PBLUETOOTH_GATT_VALUE_CHANGED_EVENT ValueChangedEventParameters = (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT)EventOutParameter;
85
85
86
- switch (ValueChangedEventParameters->CharacteristicValue ->DataSize )
86
+ if (ValueChangedEventParameters->CharacteristicValue ->DataSize == 1 )
87
87
{
88
- case 0 :
88
+ char data = ValueChangedEventParameters->CharacteristicValue ->Data [0 ];
89
+ switch (data)
89
90
{
90
- OutputDebugFormat (" Notification obtained ValueChangedEventParameters->CharacteristicValue->DataSize=0\n " );
91
+ case 101 :
92
+ OutputDebugFormat (" Notification obtained Knob Press\n " );
93
+ ToggleMute ();
91
94
break ;
92
- }
93
- case 1 :
94
- {
95
- char data = ValueChangedEventParameters->CharacteristicValue ->Data [0 ];
96
- switch (data)
97
- {
98
- case 104 :
99
- OutputDebugFormat (" Notification obtained Knob Right\n " );
100
- IncreaseVolume ();
101
- break ;
102
95
103
- case 103 :
104
- OutputDebugFormat (" Notification obtained Knob Left\n " );
105
- DecreaseVolume ();
106
- break ;
107
-
108
- case 101 :
109
- OutputDebugFormat (" Notification obtained Knob Press\n " );
110
- ToggleMute ();
111
- break ;
96
+ case 103 :
97
+ OutputDebugFormat (" Notification obtained Knob Left\n " );
98
+ DecreaseVolume ();
99
+ break ;
112
100
113
- default :
114
- OutputDebugFormat (" Notification obtained Unknown atom %d\n " , data);
115
- break ;
116
- }
101
+ case 104 :
102
+ OutputDebugFormat (" Notification obtained Knob Right\n " );
103
+ IncreaseVolume ();
117
104
break ;
118
- }
105
+
119
106
default :
120
- {
121
- #if 0
122
- char hex[256];
123
- char buf = hex;
124
- for (ULONG i = 0; i < ValueChangedEventParameters->CharacteristicValue->DataSize; i++)
125
- {
126
- size_t count = hex + sizeof(hex) - buf - 1;
127
- int result = snprintf(buf, count, "%0X", ValueChangedEventParameters->CharacteristicValue->Data[i]);
128
- buf += result;
129
- }
130
- OutputDebugFormat("Notification obtained %s\n", buf);
107
+ OutputDebugFormat (" Notification obtained unknown event\n " );
131
108
break ;
132
- #endif
133
109
}
134
110
}
111
+ else
112
+ {
113
+ OutputDebugFormat (" Notification obtained unknown data size %d\n " , ValueChangedEventParameters->CharacteristicValue ->DataSize );
114
+ }
135
115
}
136
116
137
117
HANDLE hBluetoothDevice = INVALID_HANDLE_VALUE;
@@ -162,10 +142,6 @@ bool StartupPowerMateBluetooth()
162
142
{
163
143
OutputDebugFormat (" BluetoothGATTGetServices returned unexpected HRESULT: %d\n " , hr);
164
144
}
165
- else
166
- {
167
- OutputDebugFormat (" Got %d services from the device\n " , serviceCount);
168
- }
169
145
if (serviceCount)
170
146
{
171
147
pServiceBuffer = (PBTH_LE_GATT_SERVICE)malloc (sizeof (BTH_LE_GATT_SERVICE) * serviceCount);
@@ -187,10 +163,6 @@ bool StartupPowerMateBluetooth()
187
163
{
188
164
OutputDebugFormat (" BluetoothGATTGetCharacteristics returned unexpected HRESULT: %d\n " , hr);
189
165
}
190
- else
191
- {
192
- OutputDebugFormat (" Got %d characteristics from the device\n " , characteristicCount);
193
- }
194
166
if (characteristicCount)
195
167
{
196
168
pCharacteristicBuffer = (PBTH_LE_GATT_CHARACTERISTIC)malloc (sizeof (BTH_LE_GATT_CHARACTERISTIC) * characteristicCount);
@@ -205,120 +177,31 @@ bool StartupPowerMateBluetooth()
205
177
}
206
178
207
179
// iterate the characteristics and attach event handler
208
- PBTH_LE_GATT_CHARACTERISTIC currentCharacteristic = NULL ;
209
- for (int characteristicIndex = 0 ; characteristicIndex < characteristicCount; characteristicIndex++)
180
+ if (characteristicCount >= 2 )
210
181
{
211
- currentCharacteristic = &pCharacteristicBuffer[characteristicIndex];
212
-
213
- USHORT descriptorCount;
214
- hr = BluetoothGATTGetDescriptors (hBluetoothDevice, currentCharacteristic, 0 , NULL , &descriptorCount, BLUETOOTH_GATT_FLAG_NONE);
215
- if (HRESULT_FROM_WIN32 (ERROR_MORE_DATA) != hr)
216
- {
217
- OutputDebugFormat (" BluetoothGATTGetDescriptors returned unexpected HRESULT: %d\n " , hr);
218
- }
219
- else
220
- {
221
- OutputDebugFormat (" Characteristic %d has %d descriptors\n " , characteristicIndex, descriptorCount);
222
- }
223
-
224
- AutoFreePointer<BTH_LE_GATT_DESCRIPTOR> pDescriptorBuffer = NULL ;
225
- if (descriptorCount)
226
- {
227
- pDescriptorBuffer = (PBTH_LE_GATT_DESCRIPTOR)malloc (sizeof (BTH_LE_GATT_DESCRIPTOR) * descriptorCount);
228
- ZeroMemory (pDescriptorBuffer, sizeof (BTH_LE_GATT_DESCRIPTOR) * descriptorCount);
229
-
230
- hr = BluetoothGATTGetDescriptors (hBluetoothDevice, currentCharacteristic, descriptorCount, pDescriptorBuffer, &descriptorCount, BLUETOOTH_GATT_FLAG_NONE);
231
- if (S_OK != hr)
232
- {
233
- OutputDebugFormat (" BluetoothGATTGetDescriptors returned unexpected HRESULT: %d\n " , hr);
234
- }
235
-
236
- for (int descriptorIndex = 0 ; descriptorIndex < descriptorCount; descriptorIndex++)
237
- {
238
- PBTH_LE_GATT_DESCRIPTOR currentDescriptor = &pDescriptorBuffer[descriptorIndex];
239
-
240
- USHORT descValueDataSize;
241
- hr = BluetoothGATTGetDescriptorValue (hBluetoothDevice, currentDescriptor, 0 , NULL , &descValueDataSize, BLUETOOTH_GATT_FLAG_NONE);
242
- if (HRESULT_FROM_WIN32 (ERROR_MORE_DATA) != hr)
243
- {
244
- OutputDebugFormat (" BluetoothGATTGetDescriptorValue returned unexpected HRESULT: %d\n " , hr);
245
- }
246
- else
247
- {
248
- OutputDebugFormat (" Characteristic %d, descriptor %d has value data size %d\n " , characteristicIndex, descriptorIndex, descValueDataSize);
249
- }
250
- AutoFreePointer<BTH_LE_GATT_DESCRIPTOR_VALUE> pDescValueBuffer = (PBTH_LE_GATT_DESCRIPTOR_VALUE)malloc (descValueDataSize);
251
- ZeroMemory (pDescValueBuffer, descValueDataSize);
252
- hr = BluetoothGATTGetDescriptorValue (hBluetoothDevice, currentDescriptor, (ULONG)descValueDataSize, pDescValueBuffer, NULL , BLUETOOTH_GATT_FLAG_NONE);
253
- if (S_OK != hr)
254
- {
255
- OutputDebugFormat (" BluetoothGATTGetDescriptorValue returned unexpected HRESULT: %d\n " , hr);
256
- }
257
-
258
- // you may also get a descriptor that is read (and not notify) and i am guessing the attribute handle is out of limits
259
- // we set all descriptors that are notifiable to notify us via IsSubstcibeToNotification
260
- if (currentDescriptor->DescriptorType != CharacteristicUserDescription)
261
- {
262
- BTH_LE_GATT_DESCRIPTOR_VALUE newValue;
263
- ZeroMemory (&newValue, sizeof (BTH_LE_GATT_DESCRIPTOR_VALUE));
264
- newValue.DescriptorType = ClientCharacteristicConfiguration;
265
- newValue.ClientCharacteristicConfiguration .IsSubscribeToNotification = TRUE ;
266
-
267
- hr = BluetoothGATTSetDescriptorValue (hBluetoothDevice, currentDescriptor, &newValue, BLUETOOTH_GATT_FLAG_NONE);
268
- if (S_OK != hr)
269
- {
270
- if (E_ACCESSDENIED != hr)
271
- {
272
- OutputDebugFormat (" BluetoothGATTGetDescriptorValue returned unexpected HRESULT: %d\n " , hr);
273
- }
274
- }
275
- else
276
- {
277
- OutputDebugFormat (" Set notification for service handle %d\n " , currentDescriptor->ServiceHandle );
278
- }
279
- }
280
- }
281
- }
282
-
283
- // set the appropriate callback function when the descriptor change value
284
- BLUETOOTH_GATT_EVENT_HANDLE hValueChangedEvent = INVALID_HANDLE_VALUE;
182
+ // 2 is the magic characteristic, BTLE is obnoxious about characteristic identity
183
+ PBTH_LE_GATT_CHARACTERISTIC currentCharacteristic = &pCharacteristicBuffer[2 ];
285
184
if (currentCharacteristic->IsNotifiable )
286
185
{
287
- OutputDebugFormat (" Setting Notification for ServiceHandle %d\n " , currentCharacteristic->ServiceHandle );
288
-
289
186
BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION eventRegistration;
290
187
ZeroMemory (&eventRegistration, sizeof (BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION));
291
188
eventRegistration.Characteristics [0 ] = *currentCharacteristic;
292
189
eventRegistration.NumCharacteristics = 1 ;
190
+ BLUETOOTH_GATT_EVENT_HANDLE hValueChangedEvent = INVALID_HANDLE_VALUE;
293
191
hr = BluetoothGATTRegisterEvent (hBluetoothDevice, CharacteristicValueChangedEvent, &eventRegistration, ValueChangedEventHandler, NULL , &hValueChangedEvent, BLUETOOTH_GATT_FLAG_NONE);
294
192
if (S_OK != hr)
295
193
{
296
194
OutputDebugFormat (" BluetoothGATTRegisterEvent returned unexpected HRESULT: %d\n " , hr);
297
195
}
298
- }
299
-
300
- if (currentCharacteristic->IsReadable )
301
- {
302
- USHORT valueDataSize;
303
- hr = BluetoothGATTGetCharacteristicValue (hBluetoothDevice, currentCharacteristic, 0 , NULL , &valueDataSize, BLUETOOTH_GATT_FLAG_NONE);
304
- if (HRESULT_FROM_WIN32 (ERROR_MORE_DATA) != hr)
305
- {
306
- OutputDebugFormat (" BluetoothGATTGetCharacteristicValue returned unexpected HRESULT: %d\n " , hr);
307
- }
308
- AutoFreePointer<BTH_LE_GATT_CHARACTERISTIC_VALUE> pValueBuffer = (PBTH_LE_GATT_CHARACTERISTIC_VALUE)malloc (valueDataSize);
309
- ZeroMemory (pValueBuffer, valueDataSize);
310
- hr = BluetoothGATTGetCharacteristicValue (hBluetoothDevice, currentCharacteristic, (ULONG)valueDataSize, pValueBuffer, NULL , BLUETOOTH_GATT_FLAG_NONE);
311
- if (S_OK != hr)
312
- {
313
- OutputDebugFormat (" BluetoothGATTGetCharacteristicValue returned unexpected HRESULT: %d\n " , hr);
314
- }
315
-
316
- OutputDebugFormat (" Read characterstic value: " );
317
- for (ULONG dataIndex = 0 ; dataIndex < pValueBuffer->DataSize ; dataIndex++)
196
+ else
318
197
{
319
- OutputDebugFormat (" %0X " , pValueBuffer-> Data [dataIndex] );
198
+ OutputDebugFormat (" Registered for Event Notification \n " );
320
199
}
321
- OutputDebugFormat (" \n " );
200
+ }
201
+ else
202
+ {
203
+ OutputDebugFormat (" Expected characteristic isn't notifiable!\n " );
204
+ return false ;
322
205
}
323
206
}
324
207
0 commit comments