• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphixe5android控制蓝牙

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文部分内容摘自: http://www.pclviewer.com/android/
用以下代码中的接口实现控制蓝牙的开、关及详细信息

 

unit Androidapi.JNI.BluetoothAdapter;

// (c) RedTitan Technology 2013
// JNI BLUETOOTH import demo
// Note - REQUIRES - PROJECT OPTIONS - USES PERMISSIONS - BLUETOOTH

interface

uses
  Androidapi.JNIBridge,
  Androidapi.JNI.JavaTypes, Classes;

type
  /////////////////////////// BluetoothDevice //////////////////////////////////
  JBluetoothDevice = interface;
  JBluetoothDeviceClass = interface(JObjectClass)
  ['{FB8F9E83-CF98-4737-B7B0-64EFB07EBE2A}']
  end;

  [JavaSignature('android/bluetooth/BluetoothDevice')]
  JBluetoothDevice = interface(JObject)
  ['{F2560E3D-D26E-4DD8-B384-43070B02FF04}']
    function getName:JString; cdecl;
    function getAddress:JString; cdecl;
  end;

  TJBluetoothDevice = class(TJavaGenericImport<JBluetoothDeviceClass, JBluetoothDevice>) end;

  /////////////////////////// BluetoothAdapter /////////////////////////////////

  JBluetoothAdapter = interface;

  JBluetoothAdapterClass = interface(JObjectClass)
  ['{05CE5465-2953-4CC3-9D9B-01D71E90F412}']
    function getDefaultAdapter: JBluetoothAdapter; cdecl;
  end;

  [JavaSignature('android/bluetooth/BluetoothAdapter')]
  JBluetoothAdapter = interface(JObject)
  ['{D5C8CAD6-E717-4B95-89F1-A6FD0861B7DB}']

    function isEnabled:Boolean; cdecl;
    function getName:JString; cdecl;
    function cancelDiscovery:Boolean;
    function checkBluetoothAddress(Address:JString):Boolean;  cdecl;
    function disable:Boolean;  cdecl;
    function enable:Boolean;  cdecl;
    function getAddress:JString;  cdecl;
    procedure closeProfileProxy(profile:Integer;BluetoothProfile:Jobject); cdecl;
    function getBondedDevices:JSet; cdecl;
  end;

  TJBluetoothAdapter = class(TJavaGenericImport<JBluetoothAdapterClass, JBluetoothAdapter>) end;

// get local Bluetooth device name and check if it is enabled
function checkBluetooth:string;

// List bonded devices
function getBonded:TStringList;

implementation

uses
  FMX.Helpers.Android;

function checkBluetooth:string;
var
  x:JBluetoothAdapter;
  s:String;
  addr:string;
begin
  x:=TJBluetoothAdapter.JavaClass.getDefaultAdapter;
  s:=jstringtostring(x.getName);
  if x.isEnabled then result:=s+'(enabled) '+jstringtostring(x.getAddress)
    else result:=s+'(disabled)';
end;

function getBonded:TStringList;
var
  x:JBluetoothAdapter;
  externalDevices:JSet;
  bonded:Tjavaobjectarray<Jobject>;
  it:Jiterator;
  o:JBluetoothDevice;
begin
  result:=TStringlist.Create;
  x:=TJBluetoothAdapter.JavaClass.getDefaultAdapter;
  externalDevices:=x.getBondedDevices;

  it:=externalDevices.iterator;

  while it.hasNext do
  begin
    o:=TJBluetoothDevice.Wrap((it.next as ILocalObject).GetObjectID);
    result.Add(jstringtostring(o.getName)+','+jstringtostring(o.getAddress))
  end;

end;

end.
蓝牙接口单元代码

 

procedure TForm1.Button1Click(Sender: TObject);

var
  x:JBluetoothAdapter;
  s:String;
  addr:string;
begin
  x:=TJBluetoothAdapter.JavaClass.getDefaultAdapter;
  s:=jstringtostring(x.getName);
  if x.isEnabled then showmessage(s+'(enabled) '+jstringtostring(x.getAddress))
    else showmessage('(disabled)');
end;
测试代码

当然你需要在project->option里给蓝牙的权限

 

效果图:

   

 

代码下载地址:https://files.cnblogs.com/nywh2008/bluetooth.rar 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DelphiUnicode学习发布时间:2022-07-18
下一篇:
Delphi实现Ping命令发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap