gpt4 book ai didi

android - 如何使用 delphi XE5 通过蓝牙发送字符串或字符?

转载 作者:行者123 更新时间:2023-11-29 00:17:54 28 4
gpt4 key购买 nike

我正在尝试制作一个与 Arduino 配合使用的应用程序。我只想发送一个字符或一个简单的字符串,例如“a”。

这是我当前的代码:

unit TabbedFormwithNavigation;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Graphics, FMX.Forms, FMX.Dialogs, FMX.TabControl, FMX.StdCtrls,
System.Actions, FMX.ActnList, FMX.Gestures, FMX.ListView.Types, FMX.ListView;
Androidapi.JNI.BluetoothAdapter,
Androidapi.JNI.JavaTypes,
Androidapi.JNIBridge,
FMX.ListBox, FMX.Layouts, FMX.Memo, FMX.Edit, FMX.Objects, FMX.ListView.Types,
FMX.ListView, System.Rtti, FMX.Grid, Data.Bind.GenData,
System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.EngExt,
Fmx.Bind.DBEngExt, Data.Bind.Components, Data.Bind.ObjectScope;

//------------------------------------------------------------------------------

type
TTabbedwithNavigationForm = class(TForm)
TabControl1: TTabControl;
TabItem1: TTabItem;
TabItem2: TTabItem;
TabItem3: TTabItem;
TabItem4: TTabItem;
ActionList1: TActionList;
TabControl2: TTabControl;
TabItem5: TTabItem;
TabItem6: TTabItem;
ChangeTabAction1: TChangeTabAction;
ToolBar1: TToolBar;
lblTitle1: TLabel;
ToolBar2: TToolBar;
lblTitle2: TLabel;
ToolBar3: TToolBar;
lblTitle3: TLabel;
ToolBar4: TToolBar;
lblTitle4: TLabel;
ToolBar5: TToolBar;
lblTitle5: TLabel;
ChangeTabAction2: TChangeTabAction;
btnBack: TSpeedButton;
btnNext: TSpeedButton;
GestureManager1: TGestureManager;
ListView1: TListView;
procedure FormCreate(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
Shift: TShiftState);
procedure TabControl1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
procedure FormShow(Sender: TObject);
procedure ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
private
{ Private declarations }
public
{ Public declarations }
targetMACAddress:string; // Endereço MAC do dispositivo selevionado
ostream:JOutputStream;
istream:JInputstream;
uid:JUUID; // UUID para tráfego SPP
Sock:JBluetoothSocket;
Adapter:JBluetoothAdapter; // Adaptador BLUETOOTH local
remoteDevice:JBluetoothDevice;
end;

//------------------------------------------------------------------------------

var
TabbedwithNavigationForm: TTabbedwithNavigationForm;

implementation

{$R *.fmx}

procedure TTabbedwithNavigationForm.FormCreate(Sender: TObject);
begin
{ This defines the default active tab at runtime }
TabControl1.ActiveTab := TabItem4;
{$IFDEF ANDROID}
{ This hides the toolbar back button on Android }
btnBack.Visible := True;
{$ENDIF}
end;

//------------------------------------------------------------------------------

procedure TTabbedwithNavigationForm.FormKeyUp(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
if Key = vkHardwareBack then
begin
if (TabControl1.ActiveTab = TabItem1) and (TabControl2.ActiveTab = TabItem6) then
begin
ChangeTabAction2.Tab := TabItem5;
ChangeTabAction2.ExecuteTarget(Self);
ChangeTabAction2.Tab := TabItem6;
Key := 0;
end;
end;
end;

//------------------------------------------------------------------------------

procedure TTabbedwithNavigationForm.FormShow(Sender: TObject);
var
s:string;
i:integer;
list:TStringList;
begin
list:=TStringList.Create;
s:=checkBluetooth; // Verifica se o bluetooth está habilitado
if pos('disabled',s)<>0 then begin
ShowMessage('Por favor, ligue o bluetooth e tente novamente!');
exit
end;

// Esta é a melhor conexão SPP UUID para conectar com um dispositivo serial Bluetooth
uid:=TJUUID.JavaClass.fromString(stringtojstring('00001101-0000-1000-8000-00805F9B34FB'));

list.Clear;
list.AddStrings(getbonded); // Faz uma lista com dispositivos pareados

listview1.Items.Clear; // Lampa a lista e a reconstrói
listview1.BeginUpdate;
for i := 0 to list.Count-1 do begin
listview1.Items.Add;
listview1.Items.Item[i].Text:=list[i].Split(['='])[0];
listview1.Items.Item[i].Detail:=list[i].Split(['='])[1];
end;
listview1.EndUpdate
end;

//------------------------------------------------------------------------------

procedure TTabbedwithNavigationForm.ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
begin

ShowMessage('Você selecionou: '+Aitem.Text);

// Dependendo do dispositivo bluetooth selecionado - fazer alguma coisa com ele
targetMACAddress:=Aitem.Detail;
if trim(targetMACAddress)='' then exit;

Adapter:=TJBluetoothAdapter.JavaClass.getDefaultAdapter;
remoteDevice:=Adapter.getRemoteDevice(stringtojstring(targetMACAddress));
sock:=remoteDevice.createRfcommSocketToServiceRecord(UID);
try
sock.connect;
except
ShowMessage('Não pôde conectar-se com o dispositivo bluetooth');
end;
if not sock.isConnected then
begin
ShowMessage('Falha ao tentar conectar novamene, tente de novo...');
exit;
end;
Toast('Conectado');
TabControl1.ActiveTab := TabItem1;
end;

end;

//------------------------------------------------------------------------------

procedure TTabbedwithNavigationForm.TabControl1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
{$IFDEF ANDROID}
case EventInfo.GestureID of
sgiLeft:
begin
if TabControl1.ActiveTab <> TabControl1.Tabs[TabControl1.TabCount-1] then
TabControl1.ActiveTab := TabControl1.Tabs[TabControl1.TabIndex+1];
Handled := True;
end;

sgiRight:
begin
if TabControl1.ActiveTab <> TabControl1.Tabs[0] then
TabControl1.ActiveTab := TabControl1.Tabs[TabControl1.TabIndex-1];
Handled := True;
end;
end;
{$ENDIF}
end;

end.

//------------------------------------------------------------------------------

function StringToJA(Data: String): TJavaArray<Byte>;
var
Arr: TBytes;
len: integer;
begin
Arr := TEncoding.Default.GetBytes(Data);
len := Length(Arr);
Result := TJavaArray<Byte>.Create(len);
if len > 0 then Move(Arr[0], Result.Data^, len);
end;

//------------------------------------------------------------------------------

我只是想知道当我点击按钮时如何发送信息,我究竟该怎么做。我知道我必须使用以下功能:

function StringToJA(Data: String): TJavaArray<Byte>;
var
Arr: TBytes;
len: integer;
begin
Arr := TEncoding.Default.GetBytes(Data);
len := Length(Arr);
Result := TJavaArray<Byte>.Create(len);
if len > 0 then Move(Arr[0], Result.Data^, len);
end;

PS.: 如果你知道如何将这段代码转换成 C++ 将对我有所帮助,因为 C++ 比 delphi 对我来说更容易。

从现在开始,非常感谢。

最佳答案

BluetoothSocket 连接到配对设备后,调用套接字的 getInputStream()getOutputStream()分别获取可用于读取和写入的流的方法。

关于android - 如何使用 delphi XE5 通过蓝牙发送字符串或字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266485/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com