Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Cross-Platform-Entwicklung (https://www.delphipraxis.net/91-cross-platform-entwicklung/)
-   -   Komponente Android Daten USB senden/empfangen (https://www.delphipraxis.net/187843-komponente-android-daten-usb-senden-empfangen.html)

Bierwut-Tom 8. Jan 2016 08:21

Komponente Android Daten USB senden/empfangen
 
Hallo Zusammen!

Ich soll Daten vom Android über USB senden und empfangen....

Eine Leidensgeschichte - oder die nächste Pappnase in meiner Sammlung ;-)
Mit Delphi2006 haben ich vor Jahren einige Anwendungen für Windows geschrieben, die Daten, die von unseren Geräten über die serielle Schnittstelle gesendet wurden, empgfangen und verarbeitet haben. Dazu benutze ich eine externe Komponete 'Comport' - alles prima.

Heute muss alles mit dem Tablet gemacht werden und man erinnerte sich an mich.
Da ich mich nicht schnell genug verstecken konnte, soll nun mal eben 'ne neue Sofware machen. Da ist sie nun, die Pappnase in meiner Sammlung ;-)

Geld spielt keine Rolex!
Also schnell Delphi Seattle und das Mobile Addon Pack bestellt, installiert und völlig problemlos auf einem Samsung Tablet meine erste 'Hallo Welt'-Software zum Laufen gebracht. Ich habe mich gewundert, wie problemlos das alles geklappt hat, da ich vorher die Horrorgeschichten in den verschiedenen Foren gelesen habe. Soweit so gut.
Jetzt der Haken... Wie früher ist nix für Seriell oder USB dabei - zumindest habe ich nichts gefunden.
Gibt es eine Komponente mit der man nun von Android über USB senden und empfangen kann? Die Firemonkey USB-Library habe ich natürlich gefunden. Da steht aber:
'In any event this looks like the fastest way to get cross platform USB support in Firemonkey on Windows and Mac OSX.'
Tja leider nix von Android.

Hat jemand Erfahrung mit der Library?
Kennt jemand eine Komponente die funktioniert?

Ich würde mich über jede Anregung freuen !
Gruß Tom!

Union 8. Jan 2016 08:56

AW: Komponente Android Daten USB senden/empfangen
 
Du musst die Android USB-Klassen verwenden, wie in diesem Beispiel.

mkinzler 8. Jan 2016 09:02

AW: Komponente Android Daten USB senden/empfangen
 
COM über USB ist nun mal eine schwierige Sache, nicht nur unter Android.

Um welche Geräte handelt es sich? Haben diese direkt USB? Wird ein USB Converter eingesetzt?

http://www.winsoftxe.com/products.htm

Bierwut-Tom 8. Jan 2016 13:34

AW: Komponente Android Daten USB senden/empfangen
 
Vielen Dank für die schnellen Antworten!

@Union: Das werde ich mir mal aif der Zunge zergehen lassen.:shock:

@mkinzler:

Das ist ein von uns entwickeltes Board mit einmem FTDI FT312D

Schönes Wochenende!

Gruß
Tom

mkinzler 8. Jan 2016 13:39

AW: Komponente Android Daten USB senden/empfangen
 
Dann könnte http://www.winsoftxe.com/acpft31xd.htm ja etwas für Dich sein. Es gibt eine Testversion zum Download.

Bierwut-Tom 11. Jan 2016 07:47

AW: Komponente Android Daten USB senden/empfangen
 
Hallo Markus!

Danke für den Tip. Ich werde es mal damit versuchen.

Gruß
Tom

wschrabi 20. Mär 2016 18:40

AW: Komponente Android Daten USB senden/empfangen
 
Hallo
habe da gleiche Problem. Mein USB Device ist mittles HID Class an PC gekoppelt und klappt mit Delphi VCL sehr gut.
DOch jetzt soll es an ein Android Tablett gehen.

Hat jemand einen TIP für DEHLPIE XE6 und Android USB communication via HID class Drivers ?
DANKE
Walter

PS: Hab von UNION das Github project gesaugt und compilieren wollen: Doch bekomme ich undeclared ID bei SharedActivityContext

JavaObject := SharedActivityContext.getSystemService(TJContext.J avaClass.USB_SERVICE);

Was kann ich tun ?

hab das hier gefunden: FMX.Helpers.Android muss in uses...

Also nach editieren des MAnifest konnte ich das ding compilieren.... und deplyen.
doch leider tut sich bei der MAUS am Android Tablet bei INFO nix.

Muss man da VENDOR und PRODUCTID noch einstellen?`
HABE USB_DEVICE attached einen Filter XML gefunden. Dort die VEID und PID eingetragen doch immer noch nix.

hat damit wer Erfahrung?

Im JAP BUCH steht das anscheinend ausfühlich drin:

https://books.google.at/books?id=aGl...201240&f=false

Bitte um Rat von jenden die das Ausprobiert haben .. danke

wschrabi 21. Mär 2016 02:36

AW: Komponente Android Daten USB senden/empfangen
 
Hallo, wie ich gesehen habe muss man das AndroidManifest.xml verwenden, das dem GITHUB Project beigefügt war.
Doch leider bekomme ich dann einen DEPLAY Error:
AndroidManisfest.xml :45 : Error parsing XML: not well formed (invalid token)

Kann mir jemand sagen, was im Manifesttemplate da falsch ist und wie ich es deployen kann?
DANKE


HIer das Manifest:
Delphi-Quellcode:
<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="%package%"
        android:versionCode="%versionCode%"
        android:versionName="%versionName%">

    <!-- This is the platform API where NativeActivity was introduced. -->
    <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
    <uses-feature android:name="android.hardware.usb.host" />
    <uses-permission android:name="android.permission.USB_PERMISSION" />
    <permissions>
       <feature name="android.hardware.usb.host"/>
    </permissions>
<%uses-permission%>
    <application android:persistent="%persistent%"
        android:restoreAnyVersion="%restoreAnyVersion%"
        android:label="%label%"
        android:installLocation="%installLocation%"
        android:debuggable="%debuggable%"
        android:largeHeap="%largeHeap%"
        android:icon="%icon%"
        android:theme="%theme%"
        android:hardwareAccelerated="%hardwareAccelerated%">
        <!-- Our activity is a subclass of the built-in NativeActivity framework class.
             This will take care of integrating with our NDK code. -->
        <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
                android:label="%activityLabel%"
                android:configChanges="orientation|keyboardHidden"
                android:launchMode="singleTask">
            <!-- Tell NativeActivity the name of our .so -->
            <meta-data android:name="android.app.lib_name"
                android:value="%libNameValue%" />
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>
        </activity>
        <%activity%>
        <receiver android:name="com.embarcadero.firemonkey.notifications.FMXNotificationAlarm" />
        <%receivers%>
    </application>
</manifest>
<!-- END_INCLUDE(manifest) -->

hoika 21. Mär 2016 05:53

AW: Komponente Android Daten USB senden/empfangen
 
Hallo,
damit kenne ich mich nicht aus.
Aber:
Müssen die %-Einträge nicht durch richtige Werte ersetzt werden?


Heiko

DonAlfredo 21. Mär 2016 08:13

AW: Komponente Android Daten USB senden/empfangen
 
Here the author.
What Delphi version are you using ?
If I check with XE8 and XE10, all is ok !

Edit:

Sorry, I see: XE6.

Unfortunately, I do not have XE6.

But I will be interested to know what is needed to get things going under XE6 !

wschrabi 21. Mär 2016 14:38

AW: Komponente Android Daten USB senden/empfangen
 
The problem was that I had the FIle in UTF8 format.
I had to convert it to ANSI then I could deploy it.
Thanks for your help
Walter

wschrabi 22. Mär 2016 08:24

AW: Komponente Android Daten USB senden/empfangen
 
HI
also ich hab die USb componenten von https://github.com/LongDirtyAnimAlf/...ndroid-USB-HID installiert. Klar kann man eine SERIAL vom HID USB Device auslesen, doch fehlt die GET_REPORT Methode. bei den JVCL Libraries https://github.com/project-jedi/jvclgibt es auch eine HIDCOntroller componete, aber hier gibt es die GetINputReport Methode.

ich hätte noch eine FRAGE:
Bieten Sie auch eine Delphi Componente die auf ANDROID API wi unter https://github.com/LongDirtyAnimAlf/...ndroid-USB-HID auf baut an?
Leider finde ich in dieser github lib nicht die Methode GetINputReport.
Die WIndows API Componente https://github.com/project-jedi/jvcl , die unter WIndows HID Devices zu steueren erlaubt, hat diese Methode GetINputReport drin.
Zwar hat Android API eine ReadWrite Methode, mit der man zb die Device Serial Number auslesen kann.
Meine Zweite Frage: Das Commando zum Auslesen der Serial ist lt. Program
> type
> TCommands = (
> CMD_get_serial=100,
> CMD_set_serial=101,
> );

wissen Sie vielleicht, ob Get_report auch so eine Bitfolge als Commando hat?
Vielleicht kann man einfach das Command anpassen und bekommt dann den Report?
Danke für Ihre INfo.

Kann wer helfen?
Danke

English:
In the USB componentes at https://github.com/LongDirtyAnimAlf/...ndroid-USB-HID there is the GET_REPORT Method missing. In the JVCL Library https://github.com/project-jedi/jvcl there is also a HIDController componente included. But there there is the GetINputReport Methode included. Can anyone here give my an advice?

DonAlfredo 22. Mär 2016 11:27

AW: Komponente Android Daten USB senden/empfangen
 
Nop !
No GET_REPORT.

I wrote this library to communicate with some Microchip USB mcu's.
HID interrupt only. No reports. Yet.

This:
TCommands = (
CMD_get_serial=100,
CMD_set_serial=101,
);
are just internal commands for my mcu's.
They slipped into the demo not seen by me.

At the moment I am very busy.
If time is available again, I will look into it.

But you could give it a try to do it yourself.
Because the basic code is just a copy of the original JVCL code.
So, perhaps, a copy of the original GET_REPORT code will also work in this library.

Later, I will look into it !.

wschrabi 22. Mär 2016 14:59

AW: Komponente Android Daten USB senden/empfangen
 
Zitat:

Zitat von DonAlfredo (Beitrag 1333423)
Here the author.
What Delphi version are you using ?
If I check with XE8 and XE10, all is ok !

Edit:

Sorry, I see: XE6.

Unfortunately, I do not have XE6.

But I will be interested to know what is needed to get things going under XE6 !

Dear Sir,
I just found the problem:
The token <%receivers%> will not work in XE6. WIll it work in X10 and what ´does it do? WIth out I can deplay, but I have problems with my HID USB Device.
I try to get the Vendor name from the descriptor. But alway a blank line. But I can get the PID and VID.
Can you help me please?
Thanks

DonAlfredo 22. Mär 2016 16:18

AW: Komponente Android Daten USB senden/empfangen
 
Just remove it .... its a placeholder for some standard receivers that are used by Delphi:
Code:
<receiver android:name="com.embarcadero.firemonkey.notifications.FMXNotificationAlarm" />
<receiver android:name="com.embarcadero.rtl.notifications.NotificationAlarm" />
I guess that these are of no importance for USB processing.

wschrabi 22. Mär 2016 17:09

AW: Komponente Android Daten USB senden/empfangen
 
Thanks a lot, for this great advice.
Best regards
Walter

wschrabi 22. Mär 2016 17:14

AW: Komponente Android Daten USB senden/empfangen
 
Dear Don,
thank you very very much that I could contact you.
I always get READ and Write Error with your tool.
My HID Device works like a mouse. It so no interrupt as mouse do make no interupt transfer.
You use READWRITE with your could. Can I use this also or is it bulk transfer.

I really need the GET REPORT. I also can not read the VENDOR / Product Name of the Device.
It works fine with the WIN JCVL LIbrary.

I would really be thankful if you could help me a little.
Thanks a lot!

Walter

wschrabi 22. Mär 2016 17:18

AW: Komponente Android Daten USB senden/empfangen
 
Zitat:

Zitat von DonAlfredo (Beitrag 1333526)
Nop !

Because the basic code is just a copy of the original JVCL code.
So, perhaps, a copy of the original GET_REPORT code will also work in this library.

Later, I will look into it !.

Don, I have analysed the WIN JVCL code and they use the Get_Report methode via the DLL from windwos.
you will find in HID.pas of the JVCL LIb:

Zitat:

{$IFDEF WINXP}

// (rom) new XP functions

function HidD_GetInputReport(HidDeviceObject: THandle;
Buffer: Pointer; BufferLength: ULONG): LongBool; stdcall;

function HidD_SetOutputReport(HidDeviceObject: THandle;
Buffer: Pointer; BufferLength: ULONG): LongBool; stdcall;

{$ENDIF WINXP}

function HidP_GetCaps(PreparsedData: PHIDPPreparsedData;
var Capabilities: THIDPCaps): NTSTATUS; stdcall;
So far as I can say, I am not sure if I can adapt the JVCL LIb with the ANDROID API as the ANDROID API have less commands.
Thanks a lot for your reply.

Walter

DonAlfredo 23. Mär 2016 06:22

AW: Komponente Android Daten USB senden/empfangen
 
Yep. It will need (a lot) more work to implement reports.
And if that is the case, you will have to have a little patience.
Sorry for that. I will report back.

DonAlfredo 23. Mär 2016 08:16

AW: Komponente Android Daten USB senden/empfangen
 
I just had a quick look on how to send/get a report.
You need to do a controltransfer.
I you look at: function TJvHidDevice.GetDeviceString(Idx: Byte): string;
You will find an example on how to do a controltransfer.
It could give you a start.

wschrabi 23. Mär 2016 08:36

AW: Komponente Android Daten USB senden/empfangen
 
Zitat:

Zitat von DonAlfredo (Beitrag 1333606)
Yep. It will need (a lot) more work to implement reports.
And if that is the case, you will have to have a little patience.
Sorry for that. I will report back.

thanks don, I am not soo sophisticated than you are, and now it is clear that I need not to break my minds.
But generally, it could be implementated with Android API to make REPORTs, right?
If so, I need someone how makes it. :oops:

Moreover, I have done the firmware for my device. Its like a mouse. Maybe it is easier to change the
mouse firmware (which is not interruped) into a HID with interupes.
Do you know what I have generally to change ? The mouse firmware I have done step by step according a how-to manual. It transfers only 12 BYTES when I hit one DIP switch.

In My WIN Prg I poll by getting reports the device and get the 12 Bytes which are interpreted than.

If it would be done by interuppes, I just could use the OnData Handler and it would be quite easy with
the JVCL Lib.

Thanks for help
Walter

wschrabi 23. Mär 2016 08:38

AW: Komponente Android Daten USB senden/empfangen
 
Zitat:

Zitat von DonAlfredo (Beitrag 1333620)
I just had a quick look on how to send/get a report.
You need to do a controltransfer.
I you look at: function TJvHidDevice.GetDeviceString(Idx: Byte): string;
You will find an example on how to do a controltransfer.
It could give you a start.

thanks , that is a help. I will investigate.
I would be thankful if I can ask you (many) more.
thanks

PS: One more question:
Would this be a way to make the prg debugable?
My Device is connected to the ANdroid usb OTG. So I can not debug via Delphi and the device is connected.
So My idea:
Wo make the same way with WIN API with the control transfer, as my device can be connected to WINDOWS and so
I can debug the prg too.
When It works on win I copy it to my Android program.
Would this a way to start the investigation?

I tried: But I do not get an aquivalent function in the WIN API (Hid.dll) for
Code:
 FUsbDeviceConnection.controlTransfer
.
How can I make a controltransfer with win api wihtout using the ready made GEt_Report.
Any ideas?

Thanks

DonAlfredo 23. Mär 2016 09:09

AW: Komponente Android Daten USB senden/empfangen
 
Ok.
I have added a quick and dirty feature report (see GitHub).
Not tested. Just use it as a start. Adapt for your needs.
Now I have to go back to normal business.

wschrabi 23. Mär 2016 09:40

AW: Komponente Android Daten USB senden/empfangen
 
Liste der Anhänge anzeigen (Anzahl: 1)
I have tried it this way.
Code:
function TJvHidDevice.GetFeatureReport: string;
const
  STD_USB_REQUEST_RECIPIENT = $01; // Interface
  STD_USB_REQUEST_GET_REPORT = $01; //HID GET_REPORT
  STD_USB_REQUEST_SET_REPORT = $09; //HID SET_REPORT
  LIBUSB_FEATURE_REPORT = $0301; //Feature report ($0300), ID = 1 ($01)
  LIBUSB_FEATURE_REPORT_LENGTH = $FF;

  (* my try
  //STD_USB_REQUEST_GET_DESCRIPTOR = $06;
  STD_USB_REQUEST_GET_REPORT = $01;
  STD_USB_REQUEST_TYPE_GET_REPORT = $A1;  
  STD_USB_REQUEST_VALUE_GET_REPORT = $100;  
  LIBUSB_DT_STRING = $03;
  *)
 
var
  i,rdo:integer;
  buffer : TJavaArray<Byte>;
  S : String;
begin

  if Openfile then
  begin

    buffer := TJavaArray<Byte>.Create(255);

    rdo := FUsbDeviceConnection.controlTransfer(
           (TJUsbConstantsUSB_DIR_IN OR TJUsbConstantsUSB_TYPE_CLASS OR STD_USB_REQUEST_RECIPIENT),
           STD_USB_REQUEST_GET_REPORT,
           LIBUSB_FEATURE_REPORT,
           0,
           buffer,
           LIBUSB_FEATURE_REPORT_LENGTH,
           2000);
    (* my try
    rdo := FUsbDeviceConnection.controlTransfer(
           STD_USB_REQUEST_TYPE_GET_REPORT,
           STD_USB_REQUEST_GET_REPORT,
           STD_USB_REQUEST_VALUE_GET_REPORT,  
           0,
           buffer,
           $0B,
           0);
    *)
           

    if rdo<0 then
    begin
      buffer.Free;
      result:='';
      exit;
    end;

    if rdo>255 then rdo:=255;

    S:='';
    for i:=0 to rdo do
    begin
      S:=S+Char(buffer.Items[i]);
    end;
    result:=S;
    buffer.Free;
  end
  else result:='';
end;
Accoring to my sniffer snappshot of my working windows prg.
(See attached screeshoot)

wschrabi 23. Mär 2016 09:41

AW: Komponente Android Daten USB senden/empfangen
 
Zitat:

Zitat von DonAlfredo (Beitrag 1333623)
Ok.
I have added a quick and dirty feature report (see GitHub).
Not tested. Just use it as a start. Adapt for your needs.
Now I have to go back to normal business.

Thanks, I will try your solution:

The ShardeActivity... is not recognized in your file.

In the new Github file, you forgot this file in the uses clauses.
Code:
  FMX.Helpers.Android

wschrabi 23. Mär 2016 10:49

AW: Komponente Android Daten USB senden/empfangen
 
Dear Don,
one furhter question:

My Firmware has no Serial Number. COuld this be a problem, that I can not open a file.
As If OpenFile is always false.

I can re_flash my firmware with a serial number, but how can I enumerate the usb device
without one.

Thanks
walter

DonAlfredo 23. Mär 2016 12:43

AW: Komponente Android Daten USB senden/empfangen
 
I think you are referring to usb.pas ?!

If so, this usb.pas is tailor made for my application.

It is a measurement system with sometimes over 25 mcu's. All connected by USB.
All mcu's have a serial, so that the app can distinguisch them.
On initial connect, a serial is generated by the main system, and stored in firmware of the mcu.

If you do not connect multiple identical USB HID devices onto a single system, then you do not need the whole serial selection protocol.

Just use:
Code:
HidCtl.OnArrival:= DeviceArrival;
HidCtl.OnRemoval:= DeviceRemoval;
Also adjust
Code:
TReport = packed record
  ReportID: byte;
  Data:   array [0..15] of byte; // adjust 15 to fit your datapacket lenghth (often 64 bits so 63 is a good guess!!)
end;

wschrabi 23. Mär 2016 12:50

AW: Komponente Android Daten USB senden/empfangen
 
Zitat:

Zitat von DonAlfredo (Beitrag 1333652)
On initial connect, a serial is generated by the main system, and stored in firmware of the mcu.

If you do not connect multiple identical USB HID devices onto a single system, then you do not need the whole serial selection protocol.

Just use:
Code:
HidCtl.OnArrival:= DeviceArrival;
HidCtl.OnRemoval:= DeviceRemoval;
Also adjust
Code:
TReport = packed record
  ReportID: byte;
  Data:   array [0..15] of byte; // adjust 15 to fit your datapacket lenghth (often 64 bits so 63 is a good guess!!)
end;

thanks Don, I only have one USB Device, but no EP-OUT defined in the Firmware, so writing a serial to the device is not possible.
I have only one IN EP, like a mouse.

BTW: For what is that:
{.$define WITHPERMISSION}

I see that you use this here:
Code:
...
    {$ifdef WITHPERMISSION}
    FPermissionIntent : JPendingIntent;
    {$endif}
...
        {$ifdef WITHPERMISSION}
        FMyController.UsbManager.requestPermission(FUsbDevice,FMyController.PermissionIntent);
        {$endif}
Do I need that on my Tablet?

One more I think to change:
I have a NON INterupted HID Device and in your code there is:
Code:
 // Get HID endpoint 1
      if FUsbInterface.getEndpointCount>1 then
      begin
        FUsbEP := FUsbInterface.getEndpoint(1);
        if FUsbEP.getType=TJUsbConstantsUSB_ENDPOINT_XFER_INT then
        begin
          if FUsbEP.getDirection = TJUsbConstantsUSB_DIR_OUT then
          begin
            FEpOut := FUsbEP;
          end
          else if FUsbEP.getDirection = TJUsbConstantsUSB_DIR_IN then
          begin
            FEpIn := FUsbEP;
          end;
        end;
      end;
I think the TJUsbConstantsUSB_ENDPOINT_XFER_INT must also change. RIght?

Thank you very much!

DonAlfredo 23. Mär 2016 15:02

AW: Komponente Android Daten USB senden/empfangen
 
I think you do not need the permission part.
As long as you apply the device filter (device_filter.xml ; with your values).

AFAIK, you do not use the interrupt endpoints, so they are not important for your programme.

wschrabi 23. Mär 2016 17:26

AW: Komponente Android Daten USB senden/empfangen
 
Alfred, yes that was an issue, I need the permission define.
Now I can get my 12 Bytes from the Device by GET_REPORT Request.
Thank you very very much for your help.
Best regards
:-D Walter

DonAlfredo 24. Mär 2016 05:44

AW: Komponente Android Daten USB senden/empfangen
 
You're welcome. And thanks for your feedback.
Together, we solved this more quickly than I expected !


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz