Skip to content

Commit

Permalink
Merge pull request #59 from AbobosSoftware/feature/android-12-bluetoo…
Browse files Browse the repository at this point in the history
…th-printing-support

Support for bluetooth on Android 12 and above
  • Loading branch information
arcadius authored Nov 30, 2023
2 parents faa054a + 5bcb237 commit f320343
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/android/BrotherPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class BrotherPrinter extends CordovaPlugin {
private ImageFilePrint mFilePrint;

private final static int PERMISSION_WRITE_EXTERNAL_STORAGE = 1;
private static final int PERMISSION_BLUETOOTH_12_REQUEST_CODE = 2;
private static final String[] PERMISSION_BLUETOOTH_12 = {Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN};

private boolean isPermitWriteStorage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Expand Down Expand Up @@ -352,6 +354,10 @@ private void findBluetoothPrinters(final CallbackContext callbackctx) {
@Override
public void run() {
try {
if(!ensureAndroid12BtPermissions()){
return;
}

List<DiscoveredPrinter> discoveredPrinters = enumerateBluetoothPrinters();
sendDiscoveredPrinters(callbackctx, discoveredPrinters);
} catch (Throwable t) {
Expand Down Expand Up @@ -447,6 +453,11 @@ public void run() {
}

if (PrinterInfo.Port.BLUETOOTH.toString().equals(port)) {

if (!ensureAndroid12BtPermissions()) {
return;
}

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
PluginResult result = new PluginResult(PluginResult.Status.ERROR, "This device does not have a bluetooth adapter.");
Expand Down Expand Up @@ -622,4 +633,21 @@ private void copyBinFile(String filename, String targetPath) {

}

private boolean ensureAndroid12BtPermissions() {

boolean alreadyHasPermission = true;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {//Andro 12 and above

if (!cordova.hasPermission(Manifest.permission.BLUETOOTH_CONNECT) ||
!cordova.hasPermission(Manifest.permission.BLUETOOTH_SCAN)) {
cordova.requestPermissions(this, PERMISSION_BLUETOOTH_12_REQUEST_CODE, PERMISSION_BLUETOOTH_12);
alreadyHasPermission = false;
}

}

return alreadyHasPermission;
}

}

0 comments on commit f320343

Please sign in to comment.