Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions BluetoothSerialExample/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BluetoothSerialExample</name>
<comment>Project BluetoothSerialExample created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=
eclipse.preferences.version=1
17 changes: 17 additions & 0 deletions android/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>android</name>
<comment>Project android created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=
eclipse.preferences.version=1
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ repositories {

dependencies {
compile 'com.facebook.react:react-native:+'
compile files('libs/btsdk.jar')
}
Binary file added android/libs/btsdk.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down