Hola, de nuevo, amigos del foro.
Nunca había usado hilos en mis aplicaciones. Quizá no fuera necesario.
Después de
este problema y gracias a
dani36652 que me ayudó a resolverlo con un hilo, he visto la utilidad de los mismos.
Como digo en el enunciado, me veo en la necesidad de modificar alguna
variable que no está declarada en el hilo.
Tengo el problema de que no se actualiza al terminar el hilo.
Al insistir una o dos veces más termina tomando su correcto valor. Pero esto no es operativo.
Se trata de
comprobar si una impresora conectada por BlueTooth está operativa o no.
Os participo el código que estoy utilizando, por si alguien puede ayudarme.
(Conste que antes de solicictar vuestra ayuda llevo 2 dias buscando en internet sin éxito)
Código Delphi
[-]
-----------------------------------------------------------------
procedure ImpresoraConectada;
procedure ImpresoraLista;
private
Impresora : TBluetoothDevice;
FSocket: TBluetoothSocket;
BluetoothAdapter: TBluetoothAdapter;
FBluetoothManager : TBluetoothManager;
ServiceGUI : String;
Guid : TGUID;
ServiceName : String;
ResultadoImpresora : Boolean;
public
end;
var
Presu: TPresu;
implementation
----------------------------------------------------------------------
procedure TPresu.ImpresoraLista;
var
Thread: TThread;
begin
Thread:= TThread.CreateAnonymousThread(
procedure
begin
TThread.ForceQueue(TThread.CurrentThread,
ImpresoraConectada) end);
Thread.FreeOnTerminate:= True;
Thread.Start;
end;
procedure TPresu.ImpresoraConectada;
var
Msg: string;
I, B: Integer;
ListaVinculados: TBluetoothDeviceList;
LServices: TBluetoothServiceList;
begin
ResultadoImpresora := False;
try
FBluetoothManager := TBluetoothManager.Current;
if FBluetoothManager = nil
then begin
Mensaje(Form1.NoCodi(ST.Strings[79]), 0);
Exit;
end;
FBluetoothManager.SocketTimeout := 100;
BluetoothAdapter := FBluetoothManager.CurrentAdapter;
if BluetoothAdapter = nil
then begin
Mensaje(Form1.NoCodi(ST.Strings[80]), 0);
Exit;
end;
ListaVinculados := FBluetoothManager.GetPairedDevices(BluetoothAdapter);
if ListaVinculados = nil
then begin
Mensaje(Form1.NoCodi(ST.Strings[81]), 0);
Exit;
end;
Impresora := ListaVinculados[DeviceElegido] as TBluetoothDevice;
if Impresora.IsPaired then
begin
LServices := Impresora.GetServices;
for B := 0 to LServices.Count - 1 do
begin
ServiceGUI := GUIDToString(LServices[b].UUID);
Guid := LServices[b].UUID;
ServiceName := LServices[b].Name;
end;
end
else begin
Mensaje(Form1.NoCodi(ST.Strings[82]), 0);
Exit;
end;
except
on E: Exception do
begin
Msg := E.Message;
Mensaje('Error al Conectar el Software de la Impresora: ' + Msg, 0);
Exit;
end;
end;
try
FSocket := Impresora.CreateClientSocket(Guid, true);
if (Assigned(Impresora)) And (Assigned(FSocket))
then Begin
FSocket.Connect; FSocket.Close; ResultadoImpresora := True;
End;
except
on E: Exception do
begin
ResultadoImpresora := False; Exit;
end;
end;
end;
-------------------------------------------------------------------
ImpresoraLista;
if not ResultadoImpresora then begin
end
else begin
end;
Esperando que, si el problema tiene solución y alguien lo sabe, la comparta conmigo.
Gracias de antemano.
Saludos cordiales.