Ver Mensaje Individual
  #4  
Antiguo 09-02-2021
Manu_Rx Manu_Rx is offline
Registrado
 
Registrado: jun 2015
Posts: 8
Reputación: 0
Manu_Rx Va por buen camino
Solucionado

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;
//------------------------------------------------------------------------------
// ************** Procedimiento que Escanea Dispositivos Bluetooth *************
//------------------------------------------------------------------------------
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;
//------------------------------------------------------------------------------
// ************** Gestion de la selección de elementos *************************
//------------------------------------------------------------------------------
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  // BotonCogido esta en verde que significa que está activo
        EjecutaExterno(iUID, '110B', '-r'); // Desactivo los servicios
        EjecutaExterno(iUID, '110E', '-r');
        DiscoveryD; // Al parar los servicios, debo volver a leer los dispositivos emparejados, ya que cambia el orden
        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 // Si el color no es BotonCogido Activo los servicios
        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;
 
//------------------------------------------------------------------------------
// ************** Ejecuta BTCOM para gestionar el BLUETOOTH ********************
//------------------------------------------------------------------------------
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'); // Hago un bucle esperando que termine el programa Btcom
    while h <> 0 do Begin
      Sleep(3000);
      h := FindWindow(nil, 'BtCom');
    End;
end;
 
 
//------------------------------------------------------------------------------
// ************** Calcula el componente BLUETOOTH según su dirección MAC *******
//------------------------------------------------------------------------------
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
Responder Con Cita