Android实例-红外线操作(XE10.2+小米5)
相关资料:
http://blog.csdn.net/qq_21752153/article/details/50244717
https://developer.xamarin.com/api/type/Android.Hardware.ConsumerIrManager/
https://forums.embarcadero.com/thread.jspa?messageID=876851
结果:
1.在取出来的支持Hz范围不知道为什么出错,哪位大神了解的,可以指点一下。谢了。
2.注意一定要增加红外线权限。
3.发送的信息最多2秒时间。
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, 8 FMX.Controls.Presentation, FMX.StdCtrls, 9 Androidapi.Helpers,//需要引入 10 AndroidAPI.jni.OS,//需要引入 11 Androidapi.JNIBridge,//需要引入 12 Androidapi.JNI.GraphicsContentViewText,//需要引入 13 Androidapi.JNI.JavaTypes,//需要引入 14 FMX.Helpers.Android;//需要引入 15 16 type 17 TForm1 = class(TForm) 18 Button1: TButton; 19 procedure Button1Click(Sender: TObject); 20 private 21 { Private declarations } 22 public 23 { Public declarations } 24 end; 25 26 //外国大神写的 27 type 28 JConsumerIrManager_CarrierFrequencyRange = interface; 29 30 JConsumerIrManager_CarrierFrequencyRangeClass = interface(JObjectClass) 31 [\'{191A7F08-B93B-40D4-9B6E-3D908B37578E}\'] 32 {class} function init(min, max: Integer): JConsumerIrManager_CarrierFrequencyRange; cdecl; 33 end; 34 35 [JavaSignature(\'android/hardware/ConsumerIrManager$CarrierFrequencyRange\')] 36 JConsumerIrManager_CarrierFrequencyRange = interface(JObject) 37 [\'{5AC0C73D-9DA2-49B0-A072-55EA03FE3B78}\'] 38 function getMaxFrequency: Integer; cdecl; 39 function getMinFrequency: Integer; cdecl; 40 end; 41 TJConsumerIrManager_CarrierFrequencyRange = class(TJavaGenericImport<JConsumerIrManager_CarrierFrequencyRangeClass, JConsumerIrManager_CarrierFrequencyRange>) end; 42 43 JConsumerIrManagerClass = interface(JObjectClass) 44 [\'{C8AB7EEA-046F-429C-8585-EF69EBB53718}\'] 45 end; 46 47 [JavaSignature(\'android/hardware/ConsumerIrManager\')] 48 JConsumerIrManager = interface(JObject) 49 [\'{FF469731-5B57-46AB-9C83-4FB51CA6F2BA}\'] 50 function getCarrierFrequencies: TJavaArray<JConsumerIrManager_CarrierFrequencyRange>; cdecl; 51 function hasIrEmitter: Boolean; cdecl; 52 procedure transmit(carrierFrequency: Integer; pattern: TJavaArray<Integer>); cdecl; 53 end; 54 TJConsumerIrManager = class(TJavaGenericImport<JConsumerIrManagerClass, JConsumerIrManager>) end; 55 56 57 var 58 Form1: TForm1; 59 60 implementation 61 62 {$R *.fmx} 63 64 function MakeJavaIntArray(arr: array of Integer): TJavaArray<Integer>; 65 var 66 I: Integer; 67 begin 68 Result := TJavaArray<Integer>.Create(Length(arr)); 69 for I := Low(arr) to High(arr) do 70 Result.Items[I] := arr[I]; 71 end; 72 73 74 procedure TForm1.Button1Click(Sender: TObject); 75 var 76 mCIR: JConsumerIrManager; 77 mRanges: TJavaArray<JConsumerIrManager_CarrierFrequencyRange>; 78 mRange: JConsumerIrManager_CarrierFrequencyRange; 79 I, DesiredFrequency: Integer; 80 HasDesiredFrequency: Boolean; 81 begin 82 //得到红外服务 83 mCIR := TJConsumerIrManager.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.CONSUMER_IR_SERVICE) 84 as ILocalObject).GetObjectID); 85 //查看手机是否支持红外功能 86 if mCIR.hasIrEmitter then 87 begin 88 //根据需要使用红外线 89 DesiredFrequency := 3800;//一般的遥控板都是38KHz 90 HasDesiredFrequency := False; 91 //mRanges := mCIR.getCarrierFrequencies;//获得支持的Hz范围(不知道为什么我得到一个范围,取里面的值时出错了,我只能注掉) 92 //for I := 0 to mRanges.Length-1 do 93 //begin 94 //mRange := mRanges.Items[I]; 95 //if (DesiredFrequency >= mRange.getMinFrequency) and (DesiredFrequency <= mRange.getMaxFrequency) then 96 //begin 97 //HasDesiredFrequency := True; 98 //Break; 99 //end; 100 //end; 101 // 102 //if HasDesiredFrequency then 103 //发送红外信号内容 104 mCIR.transmit(DesiredFrequency, MakeJavaIntArray([8950,4350,650,500,600,500,550,600,550,550,600,500,550,550,600,550,600,500,550,1650,600,1600,650,1600,550,1700,600,1600,600,1600,600,1650,600,1600,650,1600,550,1650,650,1600,600,550,550,550,550,550,550,550,600,500,600,550,550,550,550,550,550,1650,650,1600,600,1600,600,1650,600,1600,600])); //pattern values here 105 end; 106 end; 107 108 end.
实例配置:
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- BEGIN_INCLUDE(manifest) --> 3 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 4 package="%package%" 5 android:versionCode="%versionCode%" 6 android:versionName="%versionName%" 7 android:installLocation="%installLocation%"> 8 9 <!-- This is the platform API where NativeActivity was introduced. --> 10 <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" /> 11 <%uses-permission%> 12 <uses-feature android:glEsVersion="0x00020000" android:required="True"/> 13 <application android:persistent="%persistent%" 14 android:restoreAnyVersion="%restoreAnyVersion%" 15 android:label="%label%" 16 android:debuggable="%debuggable%" 17 android:largeHeap="%largeHeap%" 18 android:icon="%icon%" 19 android:theme="%theme%" 20 android:hardwareAccelerated="%hardwareAccelerated%"> 21 22 <%application-meta-data%> 23 <%services%> 24 <!-- Our activity is a subclass of the built-in NativeActivity framework class. 25 This will take care of integrating with our NDK code. --> 26 <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" 27 android:label="%activityLabel%" 28 android:configChanges="orientation|keyboard|keyboardHidden|screenSize" 29 android:launchMode="singleTask"> 30 <!-- Tell NativeActivity the name of our .so --> 31 <meta-data android:name="android.app.lib_name" 32 android:value="%libNameValue%" /> 33 <intent-filter> 34 <action android:name="android.intent.action.MAIN" /> 35 <category android:name="android.intent.category.LAUNCHER" /> 36 </intent-filter> 37 </activity> 38 <%activity%> 39 <%receivers%> 40 </application> 41 <uses-permission android:name="android.permission.TRANSMIT_IR" /> 42 <uses-feature android:name="android.hardware.consumerir" android:required="false"/> 43 </manifest> 44 <!-- END_INCLUDE(manifest) -->
请发表评论