项目作者: yimankaing

项目描述 :
print text and image from react native
高级语言: Java
项目地址: git://github.com/yimankaing/react-native-bluetooth-printer.git


react_native_bluetooth_printer (Android, iOS = future)

{

I’m sorry this is not a react-native package. You have to copy some files and modified or add some line to native project.

Android project

const androidApp = android/app/src/main/java/com/native-module;

const resources = android/app/src/main/res;

FILE TO BE ADDED:

  • androidApp/bluetooth_printer/ (3 files)
    • BluetoothService.java
    • DeviceListActivity.java
    • PrintPicture.java
  • androidApp/Constants.java (I use Khmer language)
  • androidApp/PrinterManager.java
  • androidApp/PrinterPackage.java
  • resources/values/* (3 files)
  • resources/layout/ (2 files)
    • device_list.xml
    • device_name.xml
  • resources/anim/* (2 files)

FILE TO BE MODIFIED:

  • AndroidManifest.xml
    • permissions
      1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
      2. <uses-permission android:name="android.permission.BLUETOOTH" ></uses-permission>
      3. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" ></uses-permission>
    • device list activity
      1. <application ...>
      2. <activity
      3. android:name=".bluetooth_printer.DeviceListActivity"
      4. android:theme="@style/DeviceListTheme" ></activity>
      5. </application>
  • MainApplication.java
    1. @Override
    2. protected List<ReactPackage> getPackages() {
    3. return Arrays.<ReactPackage>asList(
    4. new MainReactPackage(),
    5. ...
    6. new PrinterPackage()
    7. );
    8. }
  • MainActivity.java

    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3. ...
    4. super.onCreate(savedInstanceState);
    5. /*INIT BLUETOOTH SERVICE*/
    6. PrinterManager.mService = new BluetoothService(getApplicationContext(), mHandler);
    7. ...
    8. }
    9. @Override
    10. public void onNewIntent(Intent intent) {
    11. super.onNewIntent(intent);
    12. setIntent(intent);
    13. }
    14. /*MESSAGE HANDLER BLUETOOTH STATE*/
    15. @SuppressLint("HandlerLeak")
    16. private final Handler mHandler = new Handler() {
    17. @Override
    18. public void handleMessage(Message msg) {
    19. switch (msg.what) {
    20. case BluetoothService.MESSAGE_STATE_CHANGE:
    21. switch (msg.arg1) {
    22. case BluetoothService.STATE_CONNECTED:
    23. Toast.makeText(getApplicationContext(), Constants.connected, Toast.LENGTH_SHORT).show();
    24. break;
    25. case BluetoothService.STATE_CONNECTING:
    26. Toast.makeText(getApplicationContext(), Constants.connecting, Toast.LENGTH_SHORT).show();
    27. break;
    28. case BluetoothService.STATE_LISTEN:
    29. case BluetoothService.STATE_NONE:
    30. break;
    31. }
    32. break;
    33. case BluetoothService.MESSAGE_CONNECTION_LOST:
    34. Toast.makeText(getApplicationContext(), Constants.disconnected, Toast.LENGTH_SHORT).show();
    35. break;
    36. case BluetoothService.MESSAGE_UNABLE_CONNECT:
    37. Toast.makeText(getApplicationContext(), Constants.unable_to_connect, Toast.LENGTH_SHORT).show();
    38. break;
    39. }
    40. }
    41. };
    42. @Override
    43. public void onStart() {
    44. super.onStart();
    45. }
    46. /*Destroy*/
    47. @Override
    48. protected void onDestroy() {
    49. super.onDestroy();
    50. if (PrinterManager.mService != null)
    51. PrinterManager.mService.stop();
    52. PrinterManager.mService = null;
    53. }

React native project

  1. import React, { Component } from 'react';
  2. import {
  3. Platform,
  4. StyleSheet,
  5. Text,
  6. View,
  7. Button,
  8. NativeModules,
  9. } from 'react-native';
  10. import ViewShot from "react-native-view-shot";
  11. import RNFS from "react-native-fs";
  12. const PrinterManager = NativeModules.PrinterManager;
  13. const imageType = "png";
  14. const imagePath = `${RNFS.ExternalDirectoryPath}/image.${imageType}`;
  15. export default class App extends React.Component {
  16. constructor(props) {
  17. super(props);
  18. }
  19. captureView = async () => {
  20. this.refs.viewShot.capture().then(uri => {
  21. RNFS.moveFile(uri, imagePath)
  22. .then((success) => {
  23. console.log('FILE MOVED!');
  24. //this.printViewShot(imagePath)
  25. })
  26. .catch((err) => {
  27. console.log(err.message);
  28. });
  29. });
  30. };
  31. printViewShot = (imagePath) => {
  32. PrinterManager.printImage(imagePath, (res) => {
  33. console.log(res)
  34. if (res === 'connected') {
  35. //do something
  36. } else {
  37. //do something
  38. }
  39. });
  40. };
  41. render() {
  42. return (
  43. <View style={styles.container}>
  44. <ViewShot ref="viewShot"
  45. options={{ format: "jpg", quality: 0.9 }}>
  46. <Button title="Connect"
  47. onPress={() => PrinterManager.connect()} />
  48. <Button title="Print Text"
  49. onPress={() => PrinterManager.printText("Hello")} />
  50. <Button title="Print view shot"
  51. onPress={() => this.printViewShot(imagePath)} />
  52. <Button title="Disconnect"
  53. onPress={() => PrinterManager.disconnect()} />
  54. <Button title="Capture"
  55. onPress={() => this.captureView()} />
  56. </ViewShot>
  57. </View>
  58. );
  59. }
  60. }
  61. const styles = StyleSheet.create({
  62. container: {
  63. flex: 1,
  64. justifyContent: 'center',
  65. alignItems: 'center',
  66. backgroundColor: '#F5FCFF',
  67. },
  68. });