You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi Tethering连接异常求助:无法显示对方应用句柄

解决Tethering配对成功后无法获取对方App句柄的问题

Hey,我看了你遇到的问题:App1和App2已经显示配对成功,但就是拿不到对方的句柄对吧?这大概率是因为你只处理了配对逻辑,没监听远程应用配置(App Profile)的发现事件——这才是获取对方App句柄的关键。下面是具体的排查和修复步骤:

1. 先确认App Profile的基础配置

你已经加了tetheringappprofile组件,但得确保这两点:

  • 两个App的TetheringAppProfile都设置了唯一的ProfileIdentifier(比如App1设为App1_MainProfile,App2设为App2_MainProfile,别重名)
  • 每个App的TetheringManager的Profiles属性已经绑定了对应的TetheringAppProfile——可以在设计时通过属性编辑器直接添加,也可以在FormCreate里用代码绑定:
// App1的FormCreate里补充
TetheringManager1.Profiles.Add(TetheringAppProfile1);

2. 添加远程Profile发现的事件处理

配对成功后,Tethering不会自动把对方的Profile推给你,得监听OnRemoteProfileAdded事件来获取对方的句柄。给两个App的TetheringManager都加上这个事件:

App1补充代码:

procedure TForm1.TetheringManager1RemoteProfileAdded(const Sender: TObject;
  const AManagerInfo: TTetheringManagerInfo; const AProfileInfo: TTetheringProfileInfo);
begin
  // 根据标识符找到远程Profile的句柄
  var RemoteAppProfile := TetheringManager1.RemoteProfiles.FindProfile(AManagerInfo.ManagerIdentifier, AProfileInfo.ProfileIdentifier);
  if Assigned(RemoteAppProfile) then
  begin
    Label2.Text := Format('找到App2的Profile:%s', [AProfileInfo.ProfileIdentifier]);
    // 这里就可以用RemoteAppProfile操作对方的共享属性、方法了
  end;
end;

App2补充代码:

procedure TForm1.TetheringManager1RemoteProfileAdded(const Sender: TObject;
  const AManagerInfo: TTetheringManagerInfo; const AProfileInfo: TTetheringProfileInfo);
begin
  var RemoteAppProfile := TetheringManager1.RemoteProfiles.FindProfile(AManagerInfo.ManagerIdentifier, AProfileInfo.ProfileIdentifier);
  if Assigned(RemoteAppProfile) then
  begin
    Label2.Text := Format('找到App1的Profile:%s', [AProfileInfo.ProfileIdentifier]);
  end;
end;

3. 优化配对后的Profile同步

有时候配对成功后,Profile不会立刻同步,你可以在配对完成的事件里手动触发刷新:

比如App1的OnPairedToRemote事件里加一行:

procedure TForm1.TetheringManager1PairedToRemote(const Sender: TObject; const AManagerInfo: TTetheringManagerInfo);
begin
  Label1.Text := Format('已连接 : %s %s', [AManagerInfo.ManagerIdentifier, AManagerInfo.ManagerName]);
  // 手动刷新远程Profile列表
  TetheringManager1.RefreshRemoteProfiles;
end;

App2的OnPairedFromLocal事件同理:

procedure TForm1.TetheringManager1PairedFromLocal(const Sender: TObject; const AManagerInfo: TTetheringManagerInfo);
begin
  Label1.Text := Format('已连接 : %s %s',[AManagerInfo.ManagerIdentifier, AManagerInfo.ManagerName]);
  TetheringManager1.RefreshRemoteProfiles;
end;

4. 额外排查点

  • 确认TetheringManager的Enabled属性在调用AutoConnect前已经设为True(设计时勾上或者FormCreate里设置)
  • 如果是跨设备连接,确保两台设备在同一局域网,防火墙没挡Delphi Tethering的默认端口(UDP 2202、TCP 2203)
  • 可以给TetheringManager加个OnError事件,看看有没有具体的错误提示,方便定位问题

按这些步骤调整后,应该就能正常获取到对方App的句柄了。如果还有问题,随时补充细节~

内容的提问来源于stack exchange,提问作者sounobre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:43:13