Application on TSD and communication with 1C: Enterprise 8.3 through HTTP-Service. Part 3 (BroadcastReceiver Getting the Barcode)

1. The choice of exchange method. API Description.



2. Implementation of the API on the side of 1C.



3. BroadcastReceiver. We receive barcode data on the example of ATOL Smart.Lite



4. OnKeyUp. Get a barcode from a scanner with keyboard emulation



In this part, I would like to focus on the details. In all the tutorials that I came across, the functions are simply described and approximately where to add it.





Let's write our application, which listens to broadcast messages and displays them in a pop-up window. Create a project with Empty Activity



.



Package name



specify " com.domain.barcodeTest



" A bunch of textbooks. Now in the project we will create a package



. For myself, I called it utils, because I don’t know where else to take it. It does not work with the network, with the database either. It doesn’t look like a model.

Therefore, like that.



image



The image highlights where to create the package. Inside utils



create a class (Kotlin File/Class



). We select that it is a class, and we call it CustomBroadcastReceiver



. Full file code:



CustomBroadcastReceiver.kt
 package com.domain.barcodeTest.utils import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.widget.Toast class CustomBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { //          val type = intent.getStringExtra("EXTRA_BARCODE_DECODING_SYMBOLE") val barcode = intent.getStringExtra("EXTRA_BARCODE_DECODING_DATA") //  .      . val sb = StringBuilder() sb.append("Type: $type, Barcode:$barcode\n") //    .    . Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show() // "data class",         . //      ,    . val bc = Barcode(type, barcode) } } }
      
      







We have created our class for receiving broadcast messages, inheriting everything from BroadcastReceiver. In it, we redefined the behavior of the function to receive a message ( onReceive



). At the top level " com.domain.barcodeTest



" we will create another package models



, and in it we will create a class " Barcode



". Full file code:



barcode.kt
 package org.innova_it.mws.models data class Barcode( val type: String?, val value: String? )
      
      







Thus, we created the Barcode class and created a constructor for it. All the same, Kotlin is good. Now we can freely use it in the CustomBroadcastReceiver



class, the error should disappear. As a result, we have the following structure.



image



In packages we store similar objects. If translated into 1C, then in the models package we store a description of 1C data. Directories, documents with details and subordinate objects. We also store the description of the database tables and the format for receiving data over the network. Looking ahead. Here is a typical example of the model from the previous part for Nomenclature. It does not apply to our current project. But it’s easier to understand with an example.



models / wares.kt
 data class PayLoadWares( val quantity: Int, val wares: List<Ware> ) //      sqlLite(Room) //@SerializedName -  Retrofit      json @Entity(tableName = "wares_table") data class Ware( @PrimaryKey @NonNull @SerializedName("code") @Expose val code: String, @SerializedName("article") @Expose val article: String, @NonNull @SerializedName("name") @Expose val name: String, @SerializedName("fullName") @Expose val fullName: String, @NonNull @SerializedName("unit") @Expose val unit: String ) data class WareResponse( val result : Result, val payload: PayLoadWares )
      
      







If we imagine the project as a 1C structure, then each package would contain classes: WaresModel (Model of the directory of goods, WaresManager (Manager of the directory of goods), WaresObject (Object of the directory), WaresUI (Module of the form), WaresActivity (Form). But in unlike 1C, we can describe common properties, methods for all directories in the model, and then inherit from them.In 1C, the platform does this and we don’t have to think about it.

How to describe interfaces in 1C, I don’t even know. This must be taught before full enlightenment. Further without this it will be impossible.



Let's get back to our application. The next thing we need to do is sign the application for the events we need. We will sign it dynamically, without using AndroidManifest.xml



.



To do this, we need to add a variable and override two procedures in MainActivity



.



Add variable



 class MainActivity : AppCompatActivity() { //   MainActivity   .      "CustomBroadcastReceiver",    "BroadcastReceiver" private val customBroadcastReceiver = CustomBroadcastReceiver() ...
      
      





Subscribe to the events. Redefine the behavior of two methods in MainActivity







 override fun onResume() { super.onResume() registerReceiver( customBroadcastReceiver, IntentFilter ("com.xcheng.scanner.action.BARCODE_DECODING_BROADCAST") ) } override fun onStop() { super.onStop() unregisterReceiver(customBroadcastReceiver) }
      
      





I think everything is quite transparent here. We sign the application for receiving messages with a filter. In 1C language When a message is (onReceive)



is (onReceive)



, where the Source is "com.xcheng.scanner.action.BARCODE_DECODING_BROADCAST"



. This is how the internal utility for working with the scanner on ATOL Smart.Lite calls itself. And the data we have is





And actually the message is being processed. We compile run. We check. Have a question? Ask in the comments. Everything. Now we are developers for Android. :)



This part is suitable for obtaining the desired result. But after it, you have to go and learn the basics of java. And only then the basics of kotlin.



PS Here I would like to contact the Android developers. I have a strange situation. I used to sign an application in OnCreate (), and unsubscribe in onStop (). But after the application went to onPause (), and when resuming onResume (), the application crashed with an error when it received the message. What could be causing this behavior?



All Articles