diff --git a/BluetoothSerialExample/.project b/BluetoothSerialExample/.project new file mode 100644 index 0000000..4aedbdf --- /dev/null +++ b/BluetoothSerialExample/.project @@ -0,0 +1,17 @@ + + + BluetoothSerialExample + Project BluetoothSerialExample created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/BluetoothSerialExample/.settings/org.eclipse.buildship.core.prefs b/BluetoothSerialExample/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..e889521 --- /dev/null +++ b/BluetoothSerialExample/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir= +eclipse.preferences.version=1 diff --git a/android/.project b/android/.project new file mode 100644 index 0000000..3964dd3 --- /dev/null +++ b/android/.project @@ -0,0 +1,17 @@ + + + android + Project android created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/android/.settings/org.eclipse.buildship.core.prefs b/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..e889521 --- /dev/null +++ b/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir= +eclipse.preferences.version=1 diff --git a/android/build.gradle b/android/build.gradle index 385ae9e..2f8d182 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -35,4 +35,5 @@ repositories { dependencies { compile 'com.facebook.react:react-native:+' + compile files('libs/btsdk.jar') } \ No newline at end of file diff --git a/android/libs/btsdk.jar b/android/libs/btsdk.jar new file mode 100644 index 0000000..f58afa5 Binary files /dev/null and b/android/libs/btsdk.jar differ diff --git a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java index be90cf3..14b6d2b 100644 --- a/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java +++ b/android/src/main/java/com/rusel/RCTBluetoothSerial/RCTBluetoothSerialModule.java @@ -15,6 +15,12 @@ import android.util.Log; import android.util.Base64; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; + +import java.io.ByteArrayOutputStream; +import zj.com.customize.sdk.Other; + import com.facebook.react.bridge.ActivityEventListener; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; @@ -24,6 +30,7 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.Promise; +import com.facebook.react.bridge.Callback; import com.facebook.react.modules.core.DeviceEventManagerModule; import static com.rusel.RCTBluetoothSerial.RCTBluetoothSerialPackage.TAG; @@ -91,6 +98,49 @@ public String getName() { return "RCTBluetoothSerial"; } + private Bitmap base64ToBitmap(String b64) { + byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); + return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); + } + + private String bitmapToBase64(Bitmap bitmap) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); + byte[] byteArray = byteArrayOutputStream .toByteArray(); + return Base64.encodeToString(byteArray, Base64.DEFAULT); + } + + @ReactMethod + /** + * Write to device over serial port + */ + public void write_img(String imgB64, Promise promise) { + String encodeIMG = imgB64; + String base64Image = encodeIMG.split(",")[1]; + Bitmap bmp = base64ToBitmap(base64Image); + String encodedImage = bitmapToBase64(bmp); + //--------------------------------------------------- + byte[] data = POS_PrintBMP(bmp, 384, 0); // --- PRINT IMG FROM BITMAP + //--------------------------------------------------- + mBluetoothService.write(data); + promise.resolve(true); + // callback.invoke(true); + } + + public static byte[] POS_PrintBMP(Bitmap mBitmap, int nWidth, int nMode) { + int width = (nWidth + 7) / 8 * 8; + int height = mBitmap.getHeight() * width / mBitmap.getWidth(); + height = (height + 7) / 8 * 8; + Bitmap rszBitmap = mBitmap; + if(mBitmap.getWidth() != width) { + rszBitmap = Other.resizeImage(mBitmap, width, height); + } + Bitmap grayBitmap = Other.toGrayscale(rszBitmap); + byte[] dithered = Other.thresholdToBWPic(grayBitmap); + byte[] data = Other.eachLinePixToCmd(dithered, width, nMode); + return data; + } + @Override public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) { if (D) Log.d(TAG, "On activity result request: " + requestCode + ", result: " + resultCode);