@@ -13,12 +13,15 @@ import android.content.IntentFilter
13
13
import android.content.pm.PackageManager
14
14
import android.graphics.Paint
15
15
import android.graphics.Typeface
16
+ import android.os.Build
17
+ import androidx.annotation.RequiresApi
16
18
import androidx.core.app.ActivityCompat
17
19
import com.facebook.react.bridge.Promise
18
20
import com.facebook.react.bridge.ReactApplicationContext
19
21
import com.facebook.react.bridge.ReactContext.RCTDeviceEventEmitter
20
22
import com.facebook.react.bridge.ReactContextBaseJavaModule
21
23
import com.facebook.react.bridge.ReactMethod
24
+ import com.facebook.react.bridge.ReadableArray
22
25
import org.json.JSONObject
23
26
import java.io.OutputStream
24
27
import java.util.Collections
@@ -35,6 +38,7 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
35
38
private val EVENT_FOUND_DEVICES = " EVENT_FOUND_DEVICES"
36
39
private val EVENT_PAIRED_DEVICES = " EVENT_PAIRED_DEVICES"
37
40
private val EVENT_DISCOVERY_FINISHED = " EVENT_DISCOVERY_FINISHED"
41
+ private var promiseScan: Promise ? = null
38
42
39
43
private val uuid = UUID .fromString(" 00001101-0000-1000-8000-00805F9B34FB" )
40
44
@@ -59,7 +63,6 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
59
63
return bleAdapter
60
64
}
61
65
62
-
63
66
@ReactMethod
64
67
fun bluetoothIsEnabled (promise : Promise ) {
65
68
val adapter = getAdapter()
@@ -114,6 +117,8 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
114
117
}
115
118
}
116
119
120
+ @SuppressLint(" MissingPermission" )
121
+ @RequiresApi(Build .VERSION_CODES .ECLAIR )
117
122
@ReactMethod
118
123
fun scanDevices (promise : Promise ) {
119
124
val checkBluetoothScanPermission = ActivityCompat .checkSelfPermission(
@@ -135,10 +140,7 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
135
140
136
141
val bleAdapter = getAdapter()
137
142
try {
138
- if (bleAdapter != null && bleAdapter.isDiscovering) {
139
- bleAdapter.cancelDiscovery()
140
- return
141
- }
143
+ stopScan()
142
144
143
145
144
146
val pairedDevices = bleAdapter?.bondedDevices
@@ -151,11 +153,22 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
151
153
152
154
devices.clear()
153
155
bleAdapter?.startDiscovery()
156
+ promiseScan = promise
154
157
} catch (e: Exception ) {
155
158
promise.reject(" SCAN" , e.message)
156
159
}
157
160
}
158
161
162
+ @RequiresApi(Build .VERSION_CODES .ECLAIR )
163
+ @SuppressLint(" MissingPermission" )
164
+ fun stopScan () {
165
+ val bleAdapter = getAdapter()
166
+ if (bleAdapter != null && bleAdapter.isDiscovering) {
167
+ bleAdapter.cancelDiscovery()
168
+ return
169
+ }
170
+ }
171
+
159
172
@ReactMethod
160
173
fun printText (
161
174
text : String ,
@@ -176,10 +189,11 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
176
189
}
177
190
178
191
@ReactMethod
179
- fun printUnderline ( promise : Promise ) {
192
+ fun printStroke ( strokeHeight : Int = 20, strokeWidth : Float = 5f , strokeDash : ReadableArray ? = null, promise : Promise ) {
180
193
val stream = getStream()
181
194
try {
182
- stream?.write(Utils ().line())
195
+ val dash = if (strokeDash != null ) FloatArray (strokeDash.size()) { strokeDash.getInt(it).toFloat() } else null
196
+ stream?.write(Utils ().createStyledStrokeBitmap(strokeHeight, strokeWidth, dash))
183
197
promise.resolve(" Print Underline" )
184
198
} catch (e: Exception ) {
185
199
promise.reject(" PrintError" , e.message)
@@ -198,13 +212,11 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
198
212
}
199
213
200
214
@ReactMethod
201
- fun printLines (lines : Int , promise : Promise ) {
215
+ fun printSpace (lines : Int , promise : Promise ) {
202
216
val stream = getStream()
203
217
try {
204
- var n = 1 ;
205
- while (n < lines) {
206
- n++
207
- stream?.write(byteArrayOf(0x0A ))
218
+ for (i in 0 .. lines) {
219
+ stream?.write(" \n " .toByteArray())
208
220
}
209
221
210
222
promise.resolve(" PRINTED LINES" )
@@ -213,32 +225,34 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
213
225
}
214
226
}
215
227
216
-
217
228
@ReactMethod
218
- fun printColumns (
229
+ fun printTwoColumns (
219
230
leftText : String ,
220
231
rightText : String ,
221
232
bold : Boolean = false,
222
233
size : Float = 24f,
223
234
promise : Promise
224
235
) {
225
- val paint = Paint ().apply {
226
- textSize = size // Tamanho da fonte
227
- typeface = if (bold) Typeface .DEFAULT_BOLD else Typeface .DEFAULT // Fonte padrão
228
- }
229
- val leftWidth = paint.measureText(leftText)
230
- val rightWidth = paint.measureText(rightText)
231
-
232
- // Calcula o espaço disponível entre os textos
233
- val totalSpace = 374 - (leftWidth + rightWidth)
234
-
235
- // Adiciona espaços entre os textos
236
- val spaces = " " .repeat((totalSpace / paint.measureText(" " )).toInt())
237
- val line = " $leftText$spaces$rightText "
236
+ val text = Utils ().twoColumns(leftText, rightText, bold, size)
237
+ printText(text, bold = bold, size = size, promise = promise)
238
+ }
238
239
239
- printText(line, bold = bold, size = size, promise = promise)
240
+ @ReactMethod
241
+ fun printColumns (texts : ReadableArray , columnWidths : ReadableArray , alignments : ReadableArray , bold : Boolean = false, textSize : Float = 24f, promise : Promise ) {
242
+ val textList = Array (texts.size()) { texts.getString(it) ? : " " }
243
+ val columnWidthList = Array (columnWidths.size()) { columnWidths.getInt(it) }
244
+ val alignmentList = Array (alignments.size()) { alignments.getString(it) ? : " LEFT" }
245
+ val columnBitmap = Utils ().createColumnTextBitmap(textList, columnWidthList, alignmentList, bold, textSize)
246
+ val stream = getStream()
247
+ try {
248
+ stream?.write(columnBitmap)
249
+ promise.resolve(" Printed Text" )
250
+ } catch (e: Exception ) {
251
+ promise.resolve(e.message)
252
+ }
240
253
}
241
254
255
+
242
256
private val receiver = object : BroadcastReceiver () {
243
257
244
258
@SuppressLint(" MissingPermission" )
@@ -264,6 +278,7 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
264
278
}
265
279
266
280
BluetoothAdapter .ACTION_DISCOVERY_FINISHED -> {
281
+ promiseScan?.resolve(" SCAN" )
267
282
reactEmitEvent(EVENT_DISCOVERY_FINISHED )
268
283
}
269
284
}
0 commit comments