Android实例-红外线操作(XE10.2+小米5)

风流不在谈锋胜,袖手无言味最长。这篇文章主要讲述Android实例-红外线操作(XE10.2+小米5)相关的知识,希望能为你提供帮助。
 
 
问题现象:在老外网站上找到的,生成的APP,运行后,取服务就报错。不知哪位大神可以帮助看一下代码出错在哪里呀。跪求!
【Android实例-红外线操作(XE10.2+小米5)】 
 
 

Android实例-红外线操作(XE10.2+小米5)

文章图片
Android实例-红外线操作(XE10.2+小米5)

文章图片
1 unit Unit1; 2 3 interface 4 5 uses 6System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, 8FMX.Controls.Presentation, FMX.StdCtrls, 9androidapi.Helpers,//需要引入 10AndroidAPI.jni.OS,//需要引入 11Androidapi.JNIBridge, // 12Androidapi.JNI.GraphicsContentViewText, 13Androidapi.JNI.javaTypes, 14FMX.Helpers.Android; // 15 16 type 17TForm1 = class(TForm) 18Button1: TButton; 19procedure Button1Click(Sender: TObject); 20private 21{ Private declarations } 22public 23{ Public declarations } 24end; 25 type 26JConsumerIrManager_CarrierFrequencyRange = interface; 27 28JConsumerIrManager_CarrierFrequencyRangeClass = interface(JObjectClass) 29[‘{191A7F08-B93B-40D4-9B6E-3D908B37578E}‘] 30{class} function init(min, max: Integer): JConsumerIrManager_CarrierFrequencyRange; cdecl; 31end; 32 33[JavaSignature(‘android/hardware/ConsumerIrManager$CarrierFrequencyRange‘)] 34JConsumerIrManager_CarrierFrequencyRange = interface(JObject) 35[‘{5AC0C73D-9DA2-49B0-A072-55EA03FE3B78}‘] 36function getMaxFrequency: Integer; cdecl; 37function getMinFrequency: Integer; cdecl; 38end; 39TJConsumerIrManager_CarrierFrequencyRange = class(TJavaGenericImport< JConsumerIrManager_CarrierFrequencyRangeClass, JConsumerIrManager_CarrierFrequencyRange> ) end; 40 41JConsumerIrManagerClass = interface(JObjectClass) 42[‘{C8AB7EEA-046F-429C-8585-EF69EBB53718}‘] 43end; 44 45[JavaSignature(‘android/hardware/ConsumerIrManager‘)] 46JConsumerIrManager = interface(JObject) 47[‘{FF469731-5B57-46AB-9C83-4FB51CA6F2BA}‘] 48function getCarrierFrequencies: TJavaArray< JConsumerIrManager_CarrierFrequencyRange> ; cdecl; 49function hasIrEmitter: Boolean; cdecl; 50procedure transmit(carrierFrequency: Integer; pattern: TJavaArray< Integer> ); cdecl; 51end; 52TJConsumerIrManager = class(TJavaGenericImport< JConsumerIrManagerClass, JConsumerIrManager> ) end; 53 54 55 var 56Form1: TForm1; 57 58 implementation 59 60 {$R *.fmx} 61 function MakeJavaIntArray(arr: array of Integer): TJavaArray< Integer> ; 62 var 63I: Integer; 64 begin 65Result := TJavaArray< Integer> .Create(Length(arr)); 66for I := Low(arr) to High(arr) do 67Result.Items[I] := arr[I]; 68 end; 69 70 71 procedure TForm1.Button1Click(Sender: TObject); 72 var 73mCIR: JConsumerIrManager; 74mRanges: TJavaArray< JConsumerIrManager_CarrierFrequencyRange> ; 75mRange: JConsumerIrManager_CarrierFrequencyRange; 76I, DesiredFrequency: Integer; 77HasDesiredFrequency: Boolean; 78 begin 79mCIR := TJConsumerIrManager.Wrap((SharedActivityContext.getSystemService(TJContext.JavaClass.CONSUMER_IR_SERVICE) 80as ILocalObject).GetObjectID); 81if mCIR.hasIrEmitter then 82begin 83// use mCIR as needed... 84 85DesiredFrequency := 3800; 86HasDesiredFrequency := False; 87 88mRanges := mCIR.getCarrierFrequencies; 89for I := 0 to mRanges.Length-1 do 90begin 91mRange := mRanges.Items[I]; 92if (DesiredFrequency > = mRange.getMinFrequency) and (DesiredFrequency < = mRange.getMaxFrequency) then 93begin 94HasDesiredFrequency := True; 95Break; 96end; 97end; 98 99if HasDesiredFrequency then 100mCIR.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 101end; 102 end; 103 104 end.

View Code 

    推荐阅读