• xe5 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里给蓝牙的权限

    效果图:

        

     http://pan.baidu.com/s/1pBciU

  • 相关阅读:
    HTML5画的简单时钟
    JS操作网页中的iframe
    JS垂直落体回弹原理
    JS回弹原理高级
    待续...
    JS拖拽面向对象拖拽继承
    JS简单回弹原理
    c++快速排序(从大到小greater/从小到大less)
    搜索素数环
    搜索组合数
  • 原文地址:https://www.cnblogs.com/key-ok/p/3341119.html
Copyright © 2020-2023  润新知