[D7]Api Windows et appariement bluetooth
Bonjour, une question simple pour une réponse sûrement assez compliqué.
Comment faire un appariement Bluetooth par programmation via l'APi windows ? et comment assigner soi-même les ports sortants et entrants ?
Avancement appariement mais problème avec jwabluetoothapis.pas
Bonjour, voilà je continue mes recherches sur le Bluetooth (1/ Pour pouvoir faire l'appariement par programmation; 2/ Pour retrouver les ports COMs associés, en particulier le sortant).
Après recherches, j'ai trouvé l'ensemble d'unité (win32_1_2) sur sourceforge correspondant à l'accès à l'API windows, dont l'unité " jwabluetoothapis.pas " pour les fonctions Bluetooth, mon problème actuel est que la fonction " BluetoothAuthenticateDevice " ne marche pas ou bien je ne sais pas l'utiliser correctement.
Sachant que je voudrais appeler
Code:
1 2 3 4 5 6
| function BluetoothAuthenticateDevice(
hwndParent: HWND;
hRadio: THandle;
pbtbi: PBLUETOOTH_DEVICE_INFO;
pszPasskey: PWideChar;
ulPasskeyLength: ULONG): DWORD; stdcall; |
avec les paramètres suivants : hwndParent à nil et fournir la clé, puisque d'après la doc:
Citation:
// There are two modes of operation. "Wizard mode" and "Blind mode."
//
// "Wizard mode" is invoked when the pszPasskey is NULL. This will cause
// the "Bluetooth Connection Wizard" to be invoked. The user will be
// prompted to enter a passkey during the wizard after which the
// authentication request will be sent. The user will see the success
// or failure of the authentication attempt. The user will also be
// given the oppurtunity to try to fix a failed authentication.
//
// "Blind mode" is invoked when the pszPasskey is non-NULL. This will
// cause the computer to send a authentication request to the remote
// device. No UI is ever displayed. The Bluetooth status code will be
// mapped to a Win32 Error code.
//
// Parameters:
//
// hwndParent
// The window to parent the authentication wizard. If NULL, the
// wizard will be parented off the desktop.
//
// hRadio
// A valid local radio handle or NULL. If NULL, then all radios will
// be tired. If any of the radios succeed, then the call will
// succeed.
//
// pbtdi
// BLUETOOTH_DEVICE_INFO record of the device to be authenticated.
//
// pszPasskey
// PIN to be used to authenticate the device. If NULL, then UI is
// displayed and the user steps through the authentication process.
// If not NULL, no UI is shown. The passkey is NOT NULL terminated.
//
// ulPasskeyLength
// Length of szPassKey in bytes. The length must be less than or
// equal to BLUETOOTH_MAX_PASSKEY_SIZE * sizeof(WCHAR).
J'ai fait différents essai qui déclenche tous une violation d'accès dans bthprops.cpl :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| procedure TAddControllerForm.LookForBth;
var
FindParams: BLUETOOTH_FIND_RADIO_PARAMS;
hRadioFind: HBLUETOOTH_RADIO_FIND;
hRadio: THandle;
RadioInfo: BLUETOOTH_RADIO_INFO;
SearchParams: BLUETOOTH_DEVICE_SEARCH_PARAMS;
hDeviceFind: HBLUETOOTH_DEVICE_FIND;
BthDevInfo: BLUETOOTH_DEVICE_INFO;
SearchParamsSize, RadioInfoSize, DevInfoSize: dword;
wcPassKey: PWideChar;
wcPassKeySize: Cardinal;
res: dword;
parentWnd: HWND;
begin
FindParams.dwSize := SizeOf(BLUETOOTH_FIND_RADIO_PARAMS);
hRadioFind:= BluetoothFindFirstRadio(@FindParams, hRadio);
if (hRadioFind <> 0) then
begin
repeat //Parcours des récepteurs Bth du pc
RadioInfo.dwSize:= sizeof(BLUETOOTH_RADIO_INFO);
BluetoothGetRadioInfo(hRadio, RadioInfo);
if ( not BluetoothIsDiscoverable(hRadio) ) then
BluetoothEnableDiscovery(hRadio, true); // Rendre devices Bth détectable
SearchParamsSize:= SizeOf(BLUETOOTH_DEVICE_SEARCH_PARAMS);
FillChar(SearchParams, SearchParamsSize, 0);
SearchParams.dwSize:= SearchParamsSize;
SearchParams.fReturnRemembered := True;
SearchParams.fReturnAuthenticated := True;
SearchParams.fReturnUnknown := True;
SearchParams.fReturnConnected := True;
SearchParams.hRadio:= hRadio;
DevInfoSize:= SizeOf(BLUETOOTH_DEVICE_INFO);
FillChar(BthDevInfo, DevInfoSize, 0);
BthDevInfo.dwSize:= DevInfoSize;
hDeviceFind := BluetoothFindFirstDevice(SearchParams, BthDevInfo);
if (hDeviceFind <> 0) then
begin
repeat //Parcours des devices Bth vues par le récepteur en cours
BluetoothUpdateDeviceRecord(BthDevInfo);
// C'est celui qui doit-être authentifié
if ( string(BthDevInfo.szName) = eCtlBthName.Text ) then
begin
eCtlBthMac.Text:= BTAdrToStr(BthDevInfo.Address);
if (not BthDevInfo.fAuthenticated) then
begin
wcPassKey:= '12345678';
wcPassKeySize:= Length(wcPassKey) * sizeof(WideChar);
parentWnd:= hwnd(nil);
// l'appel ici déclenche toujours une exception "EAccessViolation"
//
// res:= BluetoothAuthenticateDevice(hwnd(nil), hRadio, BthDevInfo, wcPassKey, wcPassKeySize);
// res:= BluetoothAuthenticateDevice(Null, hRadio, BthDevInfo, wcPassKey, wcPassKeySize);
// res:= BluetoothAuthenticateDevice(Self.Handle, hRadio, DevInfo, nil, pkSize);
//
// if ( res = ERROR_SUCCESS ) then ShowMessage('Youpi');
end;
end;
until ( not BluetoothFindNextDevice(hDeviceFind, BthDevInfo) );
BluetoothFindDeviceClose(hDeviceFind);
end;
until ( not BluetoothFindNextRadio(hRadioFind, hRadio) );
BluetoothFindRadioClose(hRadioFind);
end;
end; |
Est-ce mon appel qui est incorrecte, je pense que oui mais je ne vois pas comment le corriger, quelqu'un aurait-il une idée ?
Quand à la seconde partie, j'ai finalement réussi (après avoir fait l'appariement à la main) à relier certaines infos de la BR pour retrouver le port sortant à partir des adresses Mac (récupérer précédemment) de la clé et du module Bluetooth.