TNB Library
TnbCeBluetoothApi.h
[詳解]
1#pragma once
13#ifndef _WIN32_WCE
14 #error TnbCeBluetoothApi.h is only supported on Windows CE platforms.
15#endif // _WIN32_WCE
16
17
18
19#include "TnbDef.h"
20#include <winsock2.h>
21#include <bthapi.h>
22#include <bt_api.h>
23#include <bt_sdp.h>
24#pragma comment(lib,"bthguid.lib")
25#pragma comment(lib,"Ws2.lib")
26
27
28
29//TNB Library
30namespace TNB{
31
32
33
34#ifndef _TnbDOXYGEN //Document作成用シンボル
35
36//#include "osInc/Btagpub.h"
37#define IOCTL_AG_OPEN_AUDIO 0x01
38#define IOCTL_AG_CLOSE_AUDIO 0x02
39#define IOCTL_AG_CLOSE_CONTROL 0x03
40#define IOCTL_AG_SET_SPEAKER_VOL 0x04
41#define IOCTL_AG_SET_MIC_VOL 0x05
42#define IOCTL_AG_GET_SPEAKER_VOL 0x06
43#define IOCTL_AG_GET_MIC_VOL 0x07
44#define IOCTL_AG_GET_POWER_MODE 0x08
45#define IOCTL_AG_SET_POWER_MODE 0x09
46#define IOCTL_AG_OPEN_CONTROL 0x0A
47#define IOCTL_AG_SET_USE_HF_AUDIO 0x0B
48#define IOCTL_AG_SET_INBAND_RING 0x0C
49
50//#include "osInc/Bthid.h"
51#define BTHHID_IOCTL_HIDConnect 1
52#define BTHHID_IOCTL_HIDVerify 2
53#define BTHHID_IOCTL_HIDDisconnect 3
54#define BTHHID_IOCTL_HidSendControl 4
55#define BTHHID_IOCTL_HidSendInterrupt 5
56
57
58
61class CBluetoothApi : CCopyImpossible
62{
63public:
65 CBluetoothApi(void)
66 {
67 static CModule s_module;
68 m_module = s_module;
69 }
70
71 //This function activates or deactivates Bluetooth Personal Area Networking (PAN).
72 // Windows Embedded CE 6.0 and later
73 int BthActivatePAN(BOOL fActivate)
74 {
75 int (*P)(BOOL fActivate) = NULL;
76 P = Func(P, _T("BthActivatePAN"));
77 return (P == NULL) ? ERROR_FUNCTION_FAILED : P(fActivate);
78 }
79
80 //This function requests that the existing connection with the peer device be authenticated.
81 int BthAuthenticate(BT_ADDR* pba) const
82 {
83 int (*P)(BT_ADDR* pba) = NULL;
84 return Func(P, _T("BthAuthenticate"))(pba);
85 }
86
87 //This function responds to a request for the personal identification number (PIN) for a Bluetooth device.
88 int BthAnswerPairRequest(BT_ADDR* pba, int cPinLength, unsigned char *ppin)
89 {
90 int (*P)(BT_ADDR* pba, int cPinLength, unsigned char *ppin) = NULL;
91 return Func(P, _T("BthAnswerPairRequest"))(pba, cPinLength, ppin);
92 }
93
94 // This function cancels the current inquiry operation.
95 int BthCancelInquiry(void)
96 {
97 int (*P)(void) = NULL;
98 return Func(P, _T("BthCancelInquiry"))();
99 }
100
101 // This function clears the Inquiry Filter.
102 int BthClearInquiryFilter(void)
103 {
104 int (*P)(void) = NULL;
105 return Func(P, _T("BthClearInquiryFilter"))();
106 }
107
108 // This function closes the specified baseband connection.
109 int BthCloseConnection(unsigned short handle)
110 {
111 int (*P)(unsigned short handle) = NULL;
112 return Func(P, _T("BthCloseConnection"))(handle);
113 }
114
115 // This function creates an ACL connection to a device with the specified address.
116 int BthCreateACLConnection(BT_ADDR* pbt, unsigned short* phandle)
117 {
118 int (*P)(BT_ADDR* pbt, unsigned short* phandle) = NULL;
119 return Func(P, _T("BthCreateACLConnection"))(pbt, phandle);
120 }
121
122 // This function creates an SCO connection to a device with the specified address.
123 int BthCreateSCOConnection(BT_ADDR* pbt, unsigned short* phandle)
124 {
125 int (*P)(BT_ADDR* pbt, unsigned short* phandle) = NULL;
126 return Func(P, _T("BthCreateSCOConnection"))(pbt, phandle);
127 }
128
129 // This function places the ACL connection to the peer in HOLD mode.
130 int BthEnterHoldMode(BT_ADDR* pba, unsigned short hold_mode_max, unsigned short hold_mode_min, unsigned short* pinterval)
131 {
132 int (*P)(BT_ADDR* pba, unsigned short hold_mode_max, unsigned short hold_mode_min, unsigned short* pinterval) = NULL;
133 return Func(P, _T("BthEnterHoldMode"))(pba, hold_mode_max, hold_mode_min, pinterval);
134 }
135
136 //This function places the ACL connection to the peer specified by the Bluetooth address in PARK mode.
137 int BthEnterParkMode(BT_ADDR* pba, unsigned short beacon_max, unsigned short beacon_min, unsigned short* pinterval)
138 {
139 int (*P)(BT_ADDR* pba, unsigned short beacon_max, unsigned short beacon_min, unsigned short* pinterval) = NULL;
140 return Func(P, _T("BthEnterParkMode"))(pba, beacon_max, beacon_min, pinterval);
141 }
142
143 // This function places the ACL connection to the peer in SNIFF mode.
144 int BthEnterSniffMode(BT_ADDR* pba, unsigned short sniff_mode_max, unsigned short sniff_mode_min, unsigned short sniff_attempt, unsigned short sniff_timeout, unsigned short* pinterval)
145 {
146 int (*P)(BT_ADDR* pba, unsigned short sniff_mode_max, unsigned short sniff_mode_min, unsigned short sniff_attempt, unsigned short sniff_timeout, unsigned short* pinterval) = NULL;
147 return Func(P, _T("BthEnterSniffMode"))(pba, sniff_mode_max, sniff_mode_min, sniff_attempt, sniff_timeout, pinterval);
148 }
149
150 //This function forces the ACL connection to the peer to leave PARK mode.
151 int BthExitParkMode(BT_ADDR* pba)
152 {
153 int (*P)(BT_ADDR* pba) = NULL;
154 return Func(P, _T("BthExitParkMode"))(pba);
155 }
156
157 //This function forces the ACL connection to the peer to leave SNIFF mode.
158 int BthExitSniffMode(BT_ADDR* pba)
159 {
160 int (*P)(BT_ADDR* pba) = NULL;
161 return Func(P, _T("BthExitSniffMode"))(pba);
162 }
163
164 //This function retrieves the Bluetooth address based on the specified connection handle.
165 int BthGetAddress(unsigned short handle, BT_ADDR* pba) const
166 {
167 int (*P)(unsigned short handle, BT_ADDR* pba) = NULL;
168 return Func(P, _T("BthGetAddress"))(handle, pba);
169 }
170
171 //This function retrieves baseband connection data for all current connections.
172 int BthGetBasebandConnections(int cConnections, BASEBAND_CONNECTION* pConnections, int* pcConnectionsReturned)
173 {
174 int (*P)(int cConnections, BASEBAND_CONNECTION* pConnections, int* pcConnectionsReturned) = NULL;
175 return Func(P, _T("BthGetBasebandConnections"))(cConnections, pConnections, pcConnectionsReturned);
176 };
177
178 //This function retrieves the current mode of the ACL connection to the peer that is specified by the Bluetooth address. This function is equivalent to BTH_HCI_IOCTL_GET_HANDLE_MODE HCI IOCTL.
179 int BthGetCurrentMode(BT_ADDR* pba, unsigned char* pmode) const
180 {
181 int (*P)(BT_ADDR* pba, unsigned char* pmode) = NULL;
182 return Func(P, _T("BthGetCurrentMode"))(pba, pmode);
183 }
184
185 // This function obtains the hardware status.
186 int BthGetHardwareStatus(int* pistatus) const
187 {
188 int (*P)(int* pistatus) = NULL;
189 return Func(P, _T("BthGetHardwareStatus"))(pistatus);
190 }
191
192 //This function retrieves the link key for the Bluetooth device.
193 int BthGetLinkKey(BT_ADDR* pba, unsigned char key[16]) const
194 {
195 int (*P)(BT_ADDR* pba, unsigned char key[16]) = NULL;
196 return Func(P, _T("BthGetLinkKey"))(pba, key);
197 }
198
199 // This function retrieves the address of the Bluetooth peer device authentication that requires the PIN code.
200 int BthGetPINRequest(BT_ADDR* pbt) const
201 {
202 int (*P)(BT_ADDR* pbt) = NULL;
203 return Func(P, _T("BthGetPINRequest"))(pbt);
204 }
205
206 // This functions retrieves the class of device (COD ) for a specified device that has a baseband connection with the local device.
207 int BthGetRemoteCOD(BT_ADDR* pbt, unsigned int* pcod) const
208 {
209 int (*P)(BT_ADDR* pbt, unsigned int* pcod) = NULL;
210 return Func(P, _T("BthGetRemoteCOD"))(pbt, pcod);
211 }
212
213 //This function retrieves the current role for a peer Bluetooth device.
214 // Windows Embedded CE 6.0 and later
215 int BthGetRole(BT_ADDR* pbt, USHORT* pusRole)
216 {
217 int (*P)(BT_ADDR* pbt, USHORT* pusRole) = NULL;
218 P = Func(P, _T("BthGetRole"));
219 return (P == NULL) ? ERROR_FUNCTION_FAILED : P(pbt, pusRole);
220 }
221
222 //This function initiates a client query that is constrained by the information contained within a WSAQUERYSET structure. This function returns a handle that is used by the BthNsLookupServiceNext function.
223 int BthNsLookupServiceBegin(LPWSAQUERYSET pQuerySet, DWORD dwFlags, LPHANDLE lphLookup) const
224 {
225 int (*P)(LPWSAQUERYSET pQuerySet, DWORD dwFlags, LPHANDLE lphLookup) = NULL;
226 return Func(P, _T("BthNsLookupServiceBegin"))(pQuerySet, dwFlags, lphLookup);
227 }
228
229 //This function frees the handle after previous calls to the BthNsLookupServiceBegin and BthNsLookupServiceNext functions.
230 int BthNsLookupServiceEnd(HANDLE hLookup) const
231 {
232 int (*P)(HANDLE hLookup) = NULL;
233 return Func(P, _T("BthNsLookupServiceEnd"))(hLookup);
234 }
235
236 //This function retrieves the results of an SDP search.
237 int BthNsLookupServiceNext(HANDLE hLookup, DWORD dwFlags, LPDWORD lpdwBufferLength, LPWSAQUERYSET pResults) const
238 {
239 int (*P)(HANDLE hLookup, DWORD dwFlags, LPDWORD lpdwBufferLength, LPWSAQUERYSET pResults) = NULL;
240 return Func(P, _T("BthNsLookupServiceNext"))(hLookup, dwFlags, lpdwBufferLength, pResults);
241 }
242
243 //This function adds, updates, or removes service records from the local SDP database.
244 int BthNsSetService(LPWSAQUERYSET pSet, WSAESETSERVICEOP op, DWORD dwFlags)
245 {
246 int (*P)(LPWSAQUERYSET pSet, WSAESETSERVICEOP op, DWORD dwFlags) = NULL;
247 return Func(P, _T("BthNsSetService"))(pSet, op, dwFlags);
248 }
249
250 //This function requests the personal identification number (PIN) for a Bluetooth device.
251 int BthPairRequest(BT_ADDR* pba, int cPinLength, unsigned char *ppin)
252 {
253 int (*P)(BT_ADDR* pba, int cPinLength, unsigned char *ppin) = NULL;
254 return Func(P, _T("BthPairRequest"))(pba, cPinLength, ppin);
255 }
256
257 //This function performs an Inquiry operation.
258 int BthPerformInquiry(unsigned int LAP, unsigned char length, unsigned char num_responses, unsigned int cBuffer, unsigned int* pcDiscoveredDevices, BthInquiryResult* InquiryList) const
259 {
260 int (*P)(unsigned int LAP, unsigned char length, unsigned char num_responses, unsigned int cBuffer, unsigned int* pcDiscoveredDevices, BthInquiryResult* InquiryList) = NULL;
261 return Func(P, _T("BthPerformInquiry"))(LAP, length, num_responses, cBuffer, pcDiscoveredDevices, InquiryList);
262 }
263
264 //This function reads the value for the authentication setting.
265 int BthReadAuthenticationEnable(unsigned char* pae) const
266 {
267 int (*P)(unsigned char* pae) = NULL;
268 return Func(P, _T("BthReadAuthenticationEnable"))(pae);
269 }
270
271 // This function reads adapter's class of device.
272 int BthReadCOD(unsigned int* pcod) const
273 {
274 int (*P)(unsigned int* pcod) = NULL;
275 return Func(P, _T("BthReadCOD"))(pcod);
276 }
277
278 //This function retrieves the Bluetooth address of the current device.
279 int BthReadLocalAddr(BT_ADDR* pba) const
280 {
281 int (*P)(BT_ADDR* pba) = NULL;
282 return Func(P, _T("BthReadLocalAddr"))(pba);
283 }
284
285 //This function retrieves the current page timeout value.
286 int BthReadPageTimeout(unsigned short* ptimeout) const
287 {
288 int (*P)(unsigned short* ptimeout) = NULL;
289 return Func(P, _T("BthReadPageTimeout"))(ptimeout);
290 }
291
292 //This function reads the version and capabilities of Link Manager Protocol (LMP) stack on the Bluetooth device. It is a combination of the following two HCI commands: Read_Remote_Version_Information and Read_Remote_Supported_Features.
293 int BthReadRemoteVersion(BT_ADDR* pba, unsigned char* plmp_version, unsigned short* plmp_subversion, unsigned short* pmanufacturer, unsigned char* plmp_features) const
294 {
295 int (*P)(BT_ADDR* pba, unsigned char* plmp_version, unsigned short* plmp_subversion, unsigned short* pmanufacturer, unsigned char* plmp_features) = NULL;
296 return Func(P, _T("BthReadRemoteVersion"))(pba, plmp_version, plmp_subversion, pmanufacturer, plmp_features);
297 }
298
299 //This function reads the adapter's scan mode.
300 int BthReadScanEnableMask(unsigned char* pmask) const
301 {
302 int (*P)(unsigned char* pmask) = NULL;
303 return Func(P, _T("BthReadScanEnableMask"))(pmask);
304 }
305
306 //This function refuses an outstanding PIN request that is retrieved by BthGetPINRequest function.
307 int BthRefusePINRequest(BT_ADDR* pbt)
308 {
309 int (*P)(BT_ADDR* pbt) = NULL;
310 return Func(P, _T("BthRefusePINRequest"))(pbt);
311 }
312
313 // This function queries the name of a remote Bluetooth device.
314 int BthRemoteNameQuery(BT_ADDR* pba, unsigned int cBuffer, unsigned int* pcRequired, WCHAR* szString) const
315 {
316 int (*P)(BT_ADDR* pba, unsigned int cBuffer, unsigned int* pcRequired, WCHAR* szString) = NULL;
317 return Func(P, _T("BthRemoteNameQuery"))(pba, cBuffer, pcRequired, szString);
318 }
319
320 //This function removes the link key for a Bluetooth device.
321 int BthRevokeLinkKey(BT_ADDR* pba)
322 {
323 int (*P)(BT_ADDR* pba) = NULL;
324 return Func(P, _T("BthRevokeLinkKey"))(pba);
325 }
326
327 //This function revokes the personal identification number (PIN) for the Bluetooth device.
328 int BthRevokePIN(BT_ADDR* pba)
329 {
330 int (*P)(BT_ADDR* pba) = NULL;
331 return Func(P, _T("BthRevokePIN"))(pba);
332 }
333
334 //This function sets the Inquiry Filter to a specified class of device (COD).
335 int BthSetCODInquiryFilter (unsigned int cod, unsigned int codMask)
336 {
337 int (*P)(unsigned int cod, unsigned int codMask) = NULL;
338 return Func(P, _T("BthSetCODInquiryFilter"))(cod, codMask);
339 }
340
341 //This function requests that the encryption mode on an existing connection is switched with the peer device.
342 int BthSetEncryption(BT_ADDR* pba, int fOn)
343 {
344 int (*P)(BT_ADDR* pba, int fOn) = NULL;
345 return Func(P, _T("BthSetEncryption"))(pba, fOn);
346 }
347
348 //This function sets the Inquiry Filter to a specified address.
349 int BthSetInquiryFilter(BT_ADDR* pba)
350 {
351 int (*P)(BT_ADDR* pba) = NULL;
352 return Func(P, _T("BthSetInquiryFilter"))(pba);
353 }
354
355 //This function stores the link key for the Bluetooth device.
356 int BthSetLinkKey(BT_ADDR* pba, unsigned char key[16])
357 {
358 int (*P) (BT_ADDR* pba, unsigned char key[16]) = NULL;
359 return Func(P, _T("BthSetLinkKey"))(pba, key);
360 }
361
362 //This function stores the personal identification number (PIN) for the Bluetooth device.
363 int BthSetPIN(BT_ADDR* pba, int cPinLength, unsigned char* ppin)
364 {
365 int (*P)(BT_ADDR* pba, int cPinLength, unsigned char* ppin) = NULL;
366 return Func(P, _T("BthSetPIN"))(pba, cPinLength, ppin);
367 }
368
369 //This function allows an application to provide an event that will be signaled by the stack when a device without a PIN sends a PIN request.
370 int BthSetSecurityUI(HANDLE hEvent, DWORD dwStoreTimeout, DWORD dwProcTimeout)
371 {
372 int (*P)(HANDLE hEvent, DWORD dwStoreTimeout, DWORD dwProcTimeout) = NULL;
373 return Func(P, _T("BthSetSecurityUI"))(hEvent, dwStoreTimeout, dwProcTimeout);
374 }
375
376 //This function changes the current role to the specified role.
377 // Windows Embedded CE 6.0 and later
378 int BthSwitchRole(BT_ADDR* pbt, USHORT usRole)
379 {
380 int (*P)(BT_ADDR* pbt, USHORT usRole) = NULL;
381 P = Func(P, _T("BthSwitchRole"));
382 return (P == NULL) ? ERROR_FUNCTION_FAILED : P(pbt, usRole);
383 }
384
385 //This function disconnects all idle connections held by the L2CAP layer.
386 int BthTerminateIdleConnections(void)
387 {
388 int (*P)(void) = NULL;
389 return Func(P, _T("BthTerminateIdleConnections"))();
390 }
391
392 //This function sets the value for the authentication setting.
393 int BthWriteAuthenticationEnable(unsigned char ae)
394 {
395 int (*P)(unsigned char ae) = NULL;
396 return Func(P, _T("BthWriteAuthenticationEnable"))(ae);
397 }
398
399 // This function sets adapter's class of device.
400 int BthWriteCOD(unsigned int cod)
401 {
402 int (*P)(unsigned int cod) = NULL;
403 return Func(P, _T("BthWriteCOD"))(cod);
404 }
405
406 // This function reads the current link policy for the existing connection with the peer specified by its Bluetooth address.
407 int BthReadLinkPolicySettings(BT_ADDR* pba, unsigned short* plps) const
408 {
409 int (*P)(BT_ADDR* pba, unsigned short* plps) = 0;
410 return Func(P, _T("BthReadLinkPolicySettings"))(pba, plps);
411 }
412
413 // This function writes the current link policy for the existing connection with the peer specified by its Bluetooth address.
414 int BthWriteLinkPolicySettings(BT_ADDR* pba, unsigned short lps)
415 {
416 int (*P)(BT_ADDR* pba, unsigned short lps) = 0;
417 return Func(P, _T("BthWriteLinkPolicySettings"))(pba, lps);
418 }
419
420 // This function configures the page timeout.
421 int BthWritePageTimeout(unsigned short timeout)
422 {
423 int (*P)(unsigned short timeout) = NULL;
424 return Func(P, _T("BthWritePageTimeout"))(timeout);
425 }
426
427 //This function sets the adapter's scan mode.
428 int BthWriteScanEnableMask(unsigned char mask)
429 {
430 int (*P)(unsigned char mask) = NULL;
431 return Func(P, _T("BthWriteScanEnableMask"))(mask);
432 }
433
434 //Enumerate all the possible modes of operation of the bluetooth radio
435 //#include <bthutil.h>
436 enum BTH_RADIO_MODE
437 {
438 BTH_POWER_OFF,
439 BTH_CONNECTABLE,
440 BTH_DISCOVERABLE
441 };
442
443 //Use the BthSetMode function to set the Bluetooth mode of operation and reflect it in the control panel.
444 // This function is also used to persist that state across hardware insertion and reboot.
445 int BthSetMode(DWORD mode)
446 {
447 int ret=ERROR_SUCCESS;
448 switch ( mode )
449 {
450 case BTH_POWER_OFF:
451 ms_BtdStartStop(false);
452 break;
453 case BTH_CONNECTABLE:
454 ms_BtdStartStop(true);
455 BthWriteScanEnableMask(2);
456 break;
457 case BTH_DISCOVERABLE:
458 ms_BtdStartStop(true);
459 BthWriteScanEnableMask(3);
460 break;
461 default:
462 break;
463 }
464 return ERROR_SUCCESS;
465 }
466
467 // Use the BthGetMode function to retrieve the current mode of operation of the Bluetooth radio.
468 int BthGetMode(DWORD* pMode) const
469 {
470 unsigned char mask;
471 if ( BthReadScanEnableMask(&mask) != ERROR_SUCCESS )
472 {
473 *pMode = BTH_POWER_OFF;
474 }
475 else
476 {
477 if ( (mask & 0x0001) != 0 )
478 {
479 *pMode = BTH_DISCOVERABLE;
480 }
481 else
482 {
483 *pMode = BTH_CONNECTABLE;
484 }
485 }
486 return ERROR_SUCCESS;
487 }
488
489protected:
490
491 // EXPORT関数コール
492 template<typename TYP>
493 TYP* Func(TYP* typ, LPCTSTR lpsz) const
494 {
495 ForceSet(typ, ::GetProcAddress(m_module, lpsz));
496 ASSERT1( typ != NULL, "CBluetoothApi::Func", "[%s]が見つかwareりませんでした", lpsz );
497 return typ;
498 }
499
500 // 戻り値管理
501 bool m_Ret(int r) const
502 {
503 ::SetLastError(r);
504 return r == ERROR_SUCCESS;
505 }
506
507private:
508 // BTD0 コントロール
509 static bool ms_BtdStartStop(bool isEnable)
510 {
511 HANDLE hDev = ::CreateFile (L"BTD0:", GENERIC_READ | GENERIC_WRITE,
512 FILE_SHARE_READ | FILE_SHARE_WRITE,
513 NULL, OPEN_EXISTING, 0, NULL);
514 if ( hDev == INVALID_HANDLE_VALUE )
515 {
516 return false;
517 }
518 const DWORD IOCTL_SERVICE_START = CTL_CODE(FILE_DEVICE_SERVICE, 1, METHOD_BUFFERED, FILE_ANY_ACCESS);
519 const DWORD IOCTL_SERVICE_STOP = CTL_CODE(FILE_DEVICE_SERVICE, 2, METHOD_BUFFERED, FILE_ANY_ACCESS);
520 DWORD ioctl = (isEnable) ? IOCTL_SERVICE_START : IOCTL_SERVICE_STOP;
521 DWORD outSize = 0;
522 DWORD outBuffer = 0;
523 WCHAR* pArgPtr = L"card";
524 ::DeviceIoControl (hDev, ioctl, pArgPtr, sizeof(WCHAR) * (4 + 1), NULL, outSize, &outBuffer, NULL);
525 ::CloseHandle(hDev);
526 return true;
527 }
528
530 class CModule
531 {
532 public:
534 CModule(void) : m_module(::LoadLibrary(_T("btdrt.dll")))
535 {
536 ASSERT( m_module != NULL );
537 }
539 ~CModule(void)
540 {
541 if ( m_module != NULL )
542 {
543 ::FreeLibrary(m_module);
544 m_module = NULL;
545 }
546 }
548 operator HMODULE(void) const
549 {
550 return m_module;
551 }
552 private:
553 HMODULE m_module;
554 };
555 HMODULE m_module;
556};
557
558#endif // _TnbDOXYGEN //Document作成用シンボル
559
560
561
562};//TNB
TNBライブラリの定義ヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
TNB Library
Definition: TnbDoxyTitle.txt:2
ULONGLONG BT_ADDR
Bluetooth(CE) アドレス.