Skip to content
Open

Pr 5 #39

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1c7a6e0
fix v android 14
Jul 15, 2024
83f53a4
Merge pull request #1 from tebalink/fix_v14
nasibudesign Aug 13, 2024
d4492ef
capabilities added
Aug 15, 2024
723206a
fixing with lateinit initialization
Aug 15, 2024
75c9893
capabilities
Aug 15, 2024
4723c16
fixed build issue with android with recent flutter.
Aug 15, 2024
4064f31
UPDATE DEPENDENCIES.
marcos930807 Jan 7, 2025
ff14215
Add namespace.
marcos930807 Jan 7, 2025
6d87864
Bump version.
marcos930807 Jan 7, 2025
73972bd
Update README.md
nasibudesign Jan 13, 2025
055661e
fix android 14 sdk 34
Jan 13, 2025
b0b1acc
Merge pull request #2 from nasibudesign/dev
nasibudesign Jan 13, 2025
28fa2d2
fixing name space and
Jan 13, 2025
96c168a
Merge pull request #3 from nasibudesign/dev
nasibudesign Jan 13, 2025
678df61
version: 1.0.6
Jan 13, 2025
46a74c7
name: thermal_printer_plus
Jan 13, 2025
0be785a
name: thermal_printer_plus
Jan 13, 2025
ea23659
updating plugin name changes
Jan 13, 2025
bd0c0b3
fixing ios and adding capabilities
Jan 13, 2025
f89d40f
version: 1.0.7
Jan 13, 2025
e5dbfd1
lowering android minSdkVersion to 21
Jan 13, 2025
9665b52
version: 1.0.8
Jan 13, 2025
1d00e7e
Fix On detach from Engine.
marcos930807 Jan 18, 2025
06e19d2
bumped version
marcos930807 Jan 18, 2025
8c66fb8
Merge branch 'integrando_feature'
marcos930807 Feb 13, 2025
c5580b5
Rename project to `thermal_printer_plus` and update plugin name to `t…
marcos930807 Feb 13, 2025
6ef5fe8
Bump Version.
marcos930807 Feb 13, 2025
4247b18
```
marcos930807 Feb 13, 2025
6761b61
Merge pull request #4 from marcos930807/fixwindows
nasibudesign Feb 13, 2025
1eaf0f0
version: 1.0.11
Feb 13, 2025
c62d5ba
version: 1.0.11 CHANGELOG
Feb 13, 2025
856a2ad
Refactor: Improved Bluetooth and Location Permission Handling
marcos930807 Mar 8, 2025
0540e10
Bump Version.
marcos930807 Mar 8, 2025
f3e385b
Roolback NetworkInfoPlus check https://github.com/fluttercommunity/pl…
marcos930807 Apr 4, 2025
58e24b6
Migrate to new Flutter/Gradle system and other fixes
Jul 16, 2025
3eb2438
Merge branch 'main' of https://github.com/nasibudesign/thermal_printe…
Jul 16, 2025
0aa76ff
Merge branch 'dev' of https://github.com/nasibudesign/thermal_printer…
Jul 16, 2025
f24c70b
Enhance Bluetooth Connection Management
Dec 19, 2025
4b1f350
Update iOS Deployment Target and Permissions Handling
Dec 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## 1.0.11

* fixed windows build

## 1.0.8

* android minSdkVersion to 21

## 1.0.7

* fixed iOS build
* added some capabilities for printers

## 1.0.6
#### FORKED PROJECT
* fixed Bt discovery issue with android 14 version
* fix issue building android version

## 1.0.5

* fixed windows build
Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# thermal_printer
# thermal_printer_plus
As the original [thermal_printer](https://pub.dev/packages/thermal_printer) has not received any update this one is meant to resolve issues by merging every solution contributed.
I love this package, I work with this package but the maintenance of it is suffering from the OG, Any PR are welcome.

[![Pub Version](https://img.shields.io/badge/pub-v1.0.5-green)](https://pub.dev/packages/thermal_printer)
[![Pub Version](https://img.shields.io/badge/pub-v1.0.11-green)](https://pub.dev/packages/thermal_printer_plus)

A library to discover printers, and send printer commands.

Expand Down Expand Up @@ -57,8 +59,8 @@ In build.gradle set
```
defaultConfig {
...
minSdkVersion 19
targetSdkVersion 33
minSdkVersion 24
targetSdkVersion 34
...
```

Expand Down Expand Up @@ -87,6 +89,24 @@ put the following code in AndroidManifest
## iOS
Allow to connect bluetooth (BLE) and network devices

Add in Info.plist
```xml
<key>NSBluetoothAlwaysUsageDescription</key>
<string>We need Bluetooth to connect to devices printer and print receipt.</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>We need Bluetooth to connect and print receipt.</string>
```

To discover network print don't forget to register dartPing at before runApp method

```dart
// Register DartPingIOS
if (Platform.isIOS) {
DartPingIOS.register();
}
```


## Windows
Allow to connect USB and network devices
To network devices is necessary to set ipAddress
Expand Down
9 changes: 5 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.4.0'
classpath 'com.android.tools.build:gradle:8.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -25,7 +25,8 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 33
namespace 'com.codingdevs.thermal_printer'
compileSdkVersion 35

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -41,8 +42,8 @@ android {
}

defaultConfig {
minSdkVersion 19
targetSdkVersion 33
minSdkVersion 21
targetSdkVersion 35
}
}

Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codingdevs.thermal_printer">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature
android:name="android.hardware.bluetooth"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ThermalPrinterPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.Re
private var isBle: Boolean = false
private var isScan: Boolean = false
lateinit var adapter: USBPrinterService
private lateinit var bluetoothService: BluetoothService
lateinit var bluetoothService: BluetoothService


private val usbHandler = object : Handler(Looper.getMainLooper()) {
Expand Down Expand Up @@ -164,8 +164,13 @@ class ThermalPrinterPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.Re
messageChannel = null
messageUSBChannel = null

bluetoothService.setHandler(null)
adapter.setHandler(null)
if (::bluetoothService.isInitialized) {
bluetoothService.setHandler(null)
}

if (::adapter.isInitialized) {
adapter.setHandler(null)
}
}

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
Expand Down Expand Up @@ -368,23 +373,58 @@ class ThermalPrinterPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.Re
result.success(true)
}

/**
* Checks for required Bluetooth/Location permissions. If missing, requests them.
*
* This method also prevents the infinite loop by NOT calling requestPermissions again
* if the user has denied permanently ("Don’t ask again").
*/
private fun checkPermissions(): Boolean {
val permissions = mutableListOf(
val requiredPermissions = mutableListOf(
Manifest.permission.ACCESS_FINE_LOCATION,
// Manifest.permission.BLUETOOTH,
// Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION
)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permissions.add(Manifest.permission.BLUETOOTH_SCAN)
permissions.add(Manifest.permission.BLUETOOTH_CONNECT)
requiredPermissions.add(Manifest.permission.BLUETOOTH_SCAN)
requiredPermissions.add(Manifest.permission.BLUETOOTH_CONNECT)
} else {
requiredPermissions.add(Manifest.permission.BLUETOOTH)
requiredPermissions.add(Manifest.permission.BLUETOOTH_ADMIN)
}

// 1) If all permissions are already granted, return true.
if (hasPermissions(context, *requiredPermissions.toTypedArray())) {
return true
}

if (!hasPermissions(context, *permissions.toTypedArray())) {
ActivityCompat.requestPermissions(currentActivity!!, permissions.toTypedArray(), PERMISSION_ALL)
// 2) Identify which permissions we can request again
val permissionsToRequest = mutableListOf<String>()
for (permission in requiredPermissions) {
val granted = ActivityCompat.checkSelfPermission(context!!, permission) == PackageManager.PERMISSION_GRANTED
val canAskAgain = ActivityCompat.shouldShowRequestPermissionRationale(currentActivity!!, permission)

if (!granted) {
// If not granted, check if we can still ask for it again
if (canAskAgain) {
// We'll add this permission to request
permissionsToRequest.add(permission)
} else {
// The user checked "Don't ask again" or the system policy prohibits asking
Toast.makeText(context, "Permission is permanently denied. Please enable it in Settings.", Toast.LENGTH_LONG).show()
return false
}
}
}

// 3) If there's no permission left to request, it means everything's either granted or permanently denied
if (permissionsToRequest.isEmpty()) {
return false
}
return true

// 4) Request all the missing permissions
ActivityCompat.requestPermissions(currentActivity!!, permissionsToRequest.toTypedArray(), PERMISSION_ALL)
return false
}

private fun hasPermissions(context: Context?, vararg permissions: String?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ class USBPrinterAdapter private constructor() {
fun init(reactContext: Context?) {
mContext = reactContext
mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager
val explicitIntent = Intent(ACTION_USB_PERMISSION);
explicitIntent.setPackage(mContext?.packageName);
mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE)
PendingIntent.getBroadcast(mContext, 0, explicitIntent, PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0)
PendingIntent.getBroadcast(mContext, 0, explicitIntent, 0)
}
val filter = IntentFilter(ACTION_USB_PERMISSION)
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
mContext!!.registerReceiver(mUsbDeviceReceiver, filter)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
mContext!!.registerReceiver(mUsbDeviceReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
} else {
mContext!!.registerReceiver(mUsbDeviceReceiver, filter);
}
Log.v(LOG_TAG, "ESC/POS Printer initialized")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,20 @@ class USBPrinterService private constructor(private var mHandler: Handler?) {
fun init(reactContext: Context?) {
mContext = reactContext
mUSBManager = mContext!!.getSystemService(Context.USB_SERVICE) as UsbManager
val explicitIntent = Intent(ACTION_USB_PERMISSION);
explicitIntent.setPackage(mContext?.packageName);
mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE)
PendingIntent.getBroadcast(mContext, 0, explicitIntent, PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getBroadcast(mContext, 0, Intent(ACTION_USB_PERMISSION), 0)
PendingIntent.getBroadcast(mContext, 0, explicitIntent, 0)
}
val filter = IntentFilter(ACTION_USB_PERMISSION)
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
mContext!!.registerReceiver(mUsbDeviceReceiver, filter)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
mContext!!.registerReceiver(mUsbDeviceReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
} else {
mContext!!.registerReceiver(mUsbDeviceReceiver, filter);
}
Log.v(LOG_TAG, "ESC/POS Printer initialized")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class UsbReceiver : BroadcastReceiver() {
if (UsbManager.ACTION_USB_DEVICE_ATTACHED == action) {

val usbDevice: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)

val explicitIntent = Intent("com.flutter_pos_printer.USB_PERMISSION");
explicitIntent.setPackage(context?.packageName);
val mPermissionIndent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(context, 0, Intent("com.flutter_pos_printer.USB_PERMISSION"), PendingIntent.FLAG_MUTABLE)
PendingIntent.getBroadcast(context, 0, explicitIntent, PendingIntent.FLAG_MUTABLE)
} else {
PendingIntent.getBroadcast(context, 0, Intent("com.flutter_pos_printer.USB_PERMISSION"), 0)
PendingIntent.getBroadcast(context, 0, explicitIntent, 0)
}
val mUSBManager = context?.getSystemService(Context.USB_SERVICE) as UsbManager?
mUSBManager?.requestPermission(usbDevice, mPermissionIndent)
Expand Down
Loading