如何使用Delphi列出蓝牙无线电/设备?

时间:2014-02-01 11:27:43

标签: windows delphi bluetooth

在Delphi下使用蓝牙时,将所有连接的蓝牙设备列入某个蓝牙无线电(主机设备)可能很方便。所以问题是:

如何使用Delphi列出蓝牙无线电/设备?

1 个答案:

答案 0 :(得分:3)

这可以通过JEDI API JwaBluetoothAPIs(在此处找到:http://sourceforge.net/projects/jedi-apilib/)和以下代码段来完成:

uses
  JwaBluetoothAPIs;

procedure ScanBluetoothRadiosDevices;
var
  RadioHandle, DeviceFindHandle: THandle;
  FindHandle: HBLUETOOTH_RADIO_FIND;
  BtFrp: TBlueToothFindRadioParams;
  RadioInfo: BLUETOOTH_RADIO_INFO;
  DeviceInfo: BLUETOOTH_DEVICE_INFO;
  DeviceSearchParams: BLUETOOTH_DEVICE_SEARCH_PARAMS;
  Err : integer;
begin
  // specify record sizes
  BtFrp.dwSize := SizeOf(BtFrp);
  DeviceInfo.dwSize := SizeOf(DeviceInfo);
  RadioInfo.dwSize := SizeOf(RadioInfo);

  FindHandle := BluetoothFindFirstRadio(@BtFrp, RadioHandle);
  if (FindHandle = 0) then
    RaiseLastOSError;

  repeat
    BluetoothEnableDiscovery(RadioHandle, True);
    if BluetoothGetRadioInfo(RadioHandle, RadioInfo) = ERROR_SUCCESS then
      ShowMessage('Radio found: '+ RadioInfo.szName);

    with DeviceSearchParams do
    begin
      dwSize := SizeOf(DeviceSearchParams);
      fReturnUnknown := True;
      fReturnRemembered := True;
      hRadio := RadioHandle;
    end;

    DeviceFindHandle := BluetoothFindFirstDevice(DeviceSearchParams, DeviceInfo);
    if DeviceFindHandle = 0 then
      Continue;

    repeat
      if BluetoothGetDeviceInfo(RadioHandle, DeviceInfo) = ERROR_SUCCESS then
      begin
        BluetoothUpdateDeviceRecord(DeviceInfo);
        if DeviceInfo.fConnected then
          ShowMessageFmt('Device %s is connected', [DeviceInfo.szName])
        else
          ShowMessageFmt('Device %s is not connected', [DeviceInfo.szName]);
      end;
    until not BluetoothFindNextDevice(DeviceFindHandle, DeviceInfo);
    BluetoothFindDeviceClose(DeviceFindHandle)
  until not (BluetoothFindNextRadio(FindHandle, RadioHandle));
  BluetoothFindRadioClose(FindHandle);
end;

从那时起,可以轻松替换ShowMessageFmt(..)调用并将其替换为自定义代码。