BLE蓝牙发现设备后不可以连接问题

qq_25890587 2020-07-14 01:15:23

procedure TForm1.btnFindClick(Sender: TObject);
begin
if btnfind.Caption = '搜索' then
begin
BluetoothLE1.Enabled := true;
BluetoothLE1.DiscoverDevices(10000);
btnfind.Caption := '停止';
end else
begin
bluetoothle1.Enabled := false;
btnfind.Caption := '搜索';
end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
it: TBluetoothLEDevice;
itt: TBluetoothGattServer;
i,j,n: integer;
begin
listbox2.Clear;
//定位listbox1选中的device索引
j := -1;
for i := 0 to listbox1.Count do
begin
if listbox1.Selected[i] then
begin
j := i;
break;
end;
end;
if j < 0 then
begin
tag := -1;
exit;
end;

//停止发现
BluetoothLE1.CancelDiscovery;

//获取device对象元素
it := lst.Items[ strtoint(leftStr(listbox1.Items[j],2))-1];
//showmessage(inttostr(it.Services.Count)); //显示0个服务
//连接设备
if it.Connect then
showmessage('connect ok')
else
showmessage('connnect fail');

end;

//BLE发现事件
procedure TForm1.BluetoothLE1DiscoverLEDevice(const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
var
n,i: integer;
it: TBluetoothLEDevice;
flag: boolean;
begin
//查找是否地址重复
flag := true;
for i := 0 to lst.Count - 1 do
begin
it := lst.Items[i];
if it.Address = aDevice.Address then
begin
flag := false;
break;
end;
end;

//不重复设备加入到lst和listbox1中
if flag then
begin
i := listbox1.Count + 1;
listbox1.Items.Add(format('%.02d',[i]) + ':' + ADevice.DeviceName + '[' + adevice.Address + ']');
lst.Add(Adevice) ;
end;
end


试了很多次,只能搜索到BLE设备,但调用connect立即返回失败。不调用connect直接获取service会报没有配对的错误.
操作系统WIN10,适配器CSR4.0,
...全文
506 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧