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,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/9621.html
標籤:VCL組件開發及應用
上一篇:請問如何將一個PDF檔案由非PDF/A格式轉換成PDF/A格式?
下一篇:SQL如何查詢到最大連續的資料?
