We connect KKM ATOL to AndroidStudio (update to FZ-54)

    Good afternoon, Habr. Last summer, I needed to connect KKM ATOL to a project in AndroidStudio. Having successfully completed the task, I published a post on Habré, in order to facilitate the way for those who go my own way: We connect KKM ATOL to AndroidStudio .

    In the light of updating legislation (FZ-54), updated drivers were released for KKM ATOL, which require a slightly different approach to connecting than described previously.



    Under the cut you will see what exactly I did. But I’ll say right away that the pros of Android development will probably not find anything interesting for themselves, but for beginners like me it will make the development path a little easier.

    To connect the updated drivers, the following steps were taken:

    1) Removed the old module from the project: File - Project Structure - select the old module from the Modules section, and click on the red minus;
    2) Added a new module: File - New - Import Module - look for the folder (in the provided folder, I have: Trade_drivers_drivers / android / jar) FptrLibRes and add it to the project;
    3) Next, as in the previous article, add to jniLibs (path: app / src / main), the armeabi and armeabi-v7a folders;
    4) Add to the libs folder (path: app / src / main) - jar-files: fptrlib.jar, fptrproxylib.jar, paycardlib.jar, usblib.jar Right-click on the jar-files, and select “Add As Library ";
    5) We make changes to gradle (I give an example from a test project that was created for the last article):

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile project(':fptrRes')
        compile files('src/main/libs/fptrproxylib.jar')
        compile files('src/main/libs/paycardlib.jar')
        compile files('src/main/libs/usblib.jar')
        compile files('src/main/libs/fptrlib.jar')
    }

    6) From the manifest, delete the lines (if they were, as in the previous test project):


    7) Errors will appear in the code. To fix it, change:

    fptr = new IFptr();
    на
    fptr = new Fptr();

    It will also throw an error on statements containing the words "Reciept". They need to be changed to “Receipt”.

    Sample test application:

    AndroidManifest File


    Gradle file
    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
        defaultConfig {
            applicationId "ru.kkm_test"
            minSdkVersion 14
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile project(':fptrRes')
        compile files('src/main/libs/fptrproxylib.jar')
        compile files('src/main/libs/paycardlib.jar')
        compile files('src/main/libs/usblib.jar')
        compile files('src/main/libs/fptrlib.jar')
    }


    MainActivity File
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Toast;
    import com.atol.drivers.fptr.Fptr;
    import com.atol.drivers.fptr.IFptr;
    import com.atol.drivers.fptr.settings.SettingsActivity;
    public class MainActivity extends AppCompatActivity {
        IFptr fptr = null;
        public String TAG = "atol";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            try{
                fptr = new Fptr();
                fptr.create(this);
            } catch (NullPointerException ex){
                fptr = null;
            }
        }
        public void onClick(View view){
            switch (view.getId()){
                case R.id.button:
                    Intent intent = new Intent(this, SettingsActivity.class);
                    intent.putExtra(SettingsActivity.DEVICE_SETTINGS, fptr.get_DeviceSettings());
                    startActivityForResult(intent, 1);
                    break;
            }
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode == 1){
                if(data!=null && data.getExtras()!=null){
                    String settings  = data.getExtras().getString(SettingsActivity.DEVICE_SETTINGS);
                    Toast.makeText(this, settings, Toast.LENGTH_LONG).show();
                    printSlip(settings);
                }
            }
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            fptr.destroy();
        }
        public void printSlip(String settings){
            if(fptr == null) {
                try {
                    fptr = new Fptr();
                    fptr.create(this);
                } catch (NullPointerException ex) {
                    fptr = null;
                }
            }
            fptr.put_DeviceSettings(settings);
            fptr.put_DeviceEnabled(true);
            fptr.Beep();
            Log.d(TAG, fptr.GetStatus()+" status");
            fptr.put_UserPassword("30");
            fptr.put_Mode(1);
            if(fptr.SetMode()<0){
                Log.d(TAG, "Ошибка: "+ fptr.get_ResultCode());
            }
            fptr.BeginDocument();
            fptr.put_Caption("ТЕСТ.ТЕСТ.ТЕСТ.");
            fptr.put_ReceiptLinespacing(255);
            fptr.put_ReceiptBrightness(15);
            fptr.put_Alignment(2);
            fptr.PrintString();
            fptr.EndDocument();
            fptr.put_Mode(2);
            fptr.SetMode();
            fptr.PrintFooter();
        }
    }


    Next, we work according to the official guidelines.

    Then let me take my leave. I hope someone will save time and nerves. If someone has comments and tips, I will listen to them with great pleasure.

    PS: recently I got the opportunity to test my application on a device with API23. When creating and reading a file, I constantly caught an exception. So I came across a new security policy regarding permissions. A wonderful article from OneeL : Android runtime permissions helped me figure this out . Why, why, and how .

    Having estimated that the capabilities of API23 in my application seem to be useless so far, I lowered targetSdkVersion to 22. In this case, everything started working fine.

    UPD:
    Links for downloading new drivers (actual on 01/30/2017):
    Download Center
    Archive with the drivers used

    Also popular now: