delphi xe5 android 控制蓝牙

Posted on 2013-09-21 13:41  至今  阅读(2584)  评论(1编辑  收藏  举报

本文部分内容摘自: 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 

 

Copyright © 2024 至今
Powered by .NET 9.0 on Kubernetes