En esta dirección
itectec.com/superuser/scripting-connecting-disconnecting-a-paired-bluetooth-device/
está la solución en el programa btcom.exe.
El tratamiento más o menos es:
Código Delphi
[-]Uses System.Bluetooth, ShellApi, Winapi.Messages, AdvPanel;
procedure TForm1.DiscoveryD;
Var i: Integer;
begin
FBluetoothManager := TBluetoothManager.Current;
FAdapter := FBluetoothManager.CurrentAdapter;
if FBluetoothManager.ConnectionState = TBluetoothConnectionState.Connected then Begin
FPairedDevices := FBluetoothManager.GetPairedDevices;
end;
end;
procedure TForm1.b01click(Sender: TObject);
Var i, n, w: Integer; iUID: String;
begin
If BotonAltavoz.Down Then Begin
i := (Sender as TAdvPanel).Tag;
if FBluetoothManager.ConnectionState = TBluetoothConnectionState.Connected then Begin
iUID:= FPairedDevices[i].Address;
n:= 0;
if (Sender As TAdvPanel).ColorTo = BotonCogido then Begin EjecutaExterno(iUID, '110B', '-r'); EjecutaExterno(iUID, '110E', '-r');
DiscoveryD; w:= Damei(iUID);
while (FPairedDevices[w].State = TBluetoothDeviceState.Connected) AND (n < 15) do begin
Sleep(2000);
DiscoveryD;
w:= Damei(iUID);
Inc(n);
end;
End Else Begin EjecutaExterno(iUID, '110E', '-c');
EjecutaExterno(iUID, '110B', '-c');
DiscoveryD;
w:= Damei(iUID);
while (FPairedDevices[w].State = TBluetoothDeviceState.Paired) AND (n < 15) do begin
Sleep(2000);
DiscoveryD;
w:= Damei(iUID);
Inc(n);
end;
End;
if NOT (N < 15) then Begin
PanelMensaje.Caption:= '***** ERROR GESTIONANDO BLUETOOTH *****';
Sleep(5000);
End;
End;
procedure TForm1.EjecutaExterno(DEVICE_ADDRESS, SERVICE_UUID, BTCOM_TIPE: STring);
var h: HWND; Folder, Comando, Parametros: String;
begin
Folder := ExtractFilePath(Application.ExeName);
Comando:= 'BtCom.exe';
Parametros:= BTCOM_TIPE + ' -b"' + DEVICE_ADDRESS + '" -s' + SERVICE_UUID;
ShellExecute(Handle, 'Open', PWideChar(Comando), PWideChar(Parametros), PWideChar(Folder), SW_HIDE);
sleep(3000);
h := FindWindow(nil, 'BtCom'); while h <> 0 do Begin
Sleep(3000);
h := FindWindow(nil, 'BtCom');
End;
end;
function TForm1.Damei(Id: String): Integer;
VAr i: Integer;
Begin
Result:= -1;
for I := 0 to FPairedDevices.Count -1 do Begin
if FPairedDevices.Items[i].address = Id then Begin
Result:= I;
Exit;
End;
End;
End;
Y esto me permite conectar y desconectar los diferentes altavoces inalámbricos que tengo emparejados.
i := (Sender as TAdvPanel).Tag; TAdvPanel corresponde a TMSSoftware