Commit abaff8ebd9a295232f585993c7994a34ab1c2b77
1 parent
059986be
删除无用lib
Showing
25 changed files
with
0 additions
and
1048 deletions
project-wl-kuaidiyuan-android/local.properties deleted
100644 → 0
| 1 | -## This file must *NOT* be checked into Version Control Systems, | ||
| 2 | -# as it contains information specific to your local configuration. | ||
| 3 | -# | ||
| 4 | -# Location of the SDK. This is only used by Gradle. | ||
| 5 | -# For customization when using a Version Control System, please read the | ||
| 6 | -# header note. | ||
| 7 | -#Mon Nov 24 16:30:05 CST 2025 | ||
| 8 | -key.password=Dilicd2014 | ||
| 9 | -keystore.password=Dilicd2014 | ||
| 10 | -sdk.dir=D\:\\Work\\android-sdks |
project-wl-kuaidiyuan-android/serialport/.gitignore deleted
100644 → 0
| 1 | -/build |
project-wl-kuaidiyuan-android/serialport/build.gradle deleted
100644 → 0
| 1 | -apply plugin: 'com.android.library' | ||
| 2 | - | ||
| 3 | -android { | ||
| 4 | - compileSdkVersion 29 | ||
| 5 | - buildToolsVersion "29.0.2" | ||
| 6 | - | ||
| 7 | - namespace 'com.dili.serialport' | ||
| 8 | - defaultConfig { | ||
| 9 | - minSdkVersion 17 | ||
| 10 | - targetSdkVersion 29 | ||
| 11 | - versionCode 1 | ||
| 12 | - versionName "1.0" | ||
| 13 | - | ||
| 14 | - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| 15 | - consumerProguardFiles 'consumer-rules.pro' | ||
| 16 | - } | ||
| 17 | - | ||
| 18 | - buildTypes { | ||
| 19 | - release { | ||
| 20 | - minifyEnabled false | ||
| 21 | - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
| 22 | - } | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | -} | ||
| 26 | - | ||
| 27 | -repositories { | ||
| 28 | - flatDir { | ||
| 29 | - dirs 'libs' | ||
| 30 | - } | ||
| 31 | -} | ||
| 32 | - | ||
| 33 | -dependencies { | ||
| 34 | - implementation fileTree(dir: 'libs', include: ['*.jar',"*.aar"]) | ||
| 35 | - | ||
| 36 | - implementation 'androidx.appcompat:appcompat:1.1.0' | ||
| 37 | - testImplementation 'junit:junit:4.12' | ||
| 38 | - androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
| 39 | - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
| 40 | - | ||
| 41 | - //必须添加的依赖 | ||
| 42 | - compileOnly 'com.android.support:recyclerview-v7:27.1.0' | ||
| 43 | - compileOnly 'com.android.support:support-v4:27.1.0' | ||
| 44 | - compileOnly 'com.android.support:appcompat-v7:27.1.0' | ||
| 45 | - compileOnly 'com.alibaba:fastjson:1.1.46.android' | ||
| 46 | - | ||
| 47 | - compileOnly fileTree(include: ['uniapp-v8-release.aar','serialportlib-release.aar'], dir: '../simpleDemo/libs') | ||
| 48 | -} |
project-wl-kuaidiyuan-android/serialport/consumer-rules.pro deleted
100644 → 0
project-wl-kuaidiyuan-android/serialport/libs/serialportlib-release.aar deleted
100644 → 0
No preview for this file type
project-wl-kuaidiyuan-android/serialport/proguard-rules.pro deleted
100644 → 0
| 1 | -# Add project specific ProGuard rules here. | ||
| 2 | -# You can control the set of applied configuration files using the | ||
| 3 | -# proguardFiles setting in build.gradle. | ||
| 4 | -# | ||
| 5 | -# For more details, see | ||
| 6 | -# http://developer.android.com/guide/developing/tools/proguard.html | ||
| 7 | - | ||
| 8 | -# If your project uses WebView with JS, uncomment the following | ||
| 9 | -# and specify the fully qualified class name to the JavaScript interface | ||
| 10 | -# class: | ||
| 11 | -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| 12 | -# public *; | ||
| 13 | -#} | ||
| 14 | - | ||
| 15 | -# Uncomment this to preserve the line number information for | ||
| 16 | -# debugging stack traces. | ||
| 17 | -#-keepattributes SourceFile,LineNumberTable | ||
| 18 | - | ||
| 19 | -# If you keep the line number information, uncomment this to | ||
| 20 | -# hide the original source file name. | ||
| 21 | -#-renamesourcefileattribute SourceFile |
project-wl-kuaidiyuan-android/serialport/src/main/AndroidManifest.xml deleted
100644 → 0
project-wl-kuaidiyuan-android/serialport/src/main/java/com/dili/serialport/UniSerialPort.java deleted
100644 → 0
| 1 | -package com.dili.serialport; | ||
| 2 | - | ||
| 3 | -import android.widget.Toast; | ||
| 4 | - | ||
| 5 | -import com.alibaba.fastjson.JSONObject; | ||
| 6 | -import com.taobao.weex.annotation.JSMethod; | ||
| 7 | -import com.taobao.weex.bridge.JSCallback; | ||
| 8 | -import com.taobao.weex.common.WXModule; | ||
| 9 | - | ||
| 10 | -import java.io.File; | ||
| 11 | -import java.util.List; | ||
| 12 | - | ||
| 13 | -import me.f1reking.serialportlib.SerialPortHelper; | ||
| 14 | -import me.f1reking.serialportlib.entity.Device; | ||
| 15 | -import me.f1reking.serialportlib.listener.IOpenSerialPortListener; | ||
| 16 | -import me.f1reking.serialportlib.listener.ISerialPortDataListener; | ||
| 17 | -import me.f1reking.serialportlib.listener.Status; | ||
| 18 | - | ||
| 19 | -public class UniSerialPort extends WXModule { | ||
| 20 | - | ||
| 21 | - private SerialPortHelper serialPortHelper = new SerialPortHelper(); | ||
| 22 | - | ||
| 23 | - //run ui thread | ||
| 24 | - @JSMethod(uiThread = true) | ||
| 25 | - public void test(){ | ||
| 26 | - Toast.makeText(mWXSDKInstance.getContext(),"d fvv",Toast.LENGTH_LONG).show(); | ||
| 27 | - } | ||
| 28 | - | ||
| 29 | - //设置串口 | ||
| 30 | - //run ui thread | ||
| 31 | - @JSMethod(uiThread = true) | ||
| 32 | - public void setPath(String port){ | ||
| 33 | - this.serialPortHelper.setPort(port); | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - //设置波特率 | ||
| 37 | - //run ui thread | ||
| 38 | - @JSMethod(uiThread = true) | ||
| 39 | - public void setBaudRate(Integer baudRate){ | ||
| 40 | - this.serialPortHelper.setBaudRate(baudRate); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - //设置停止位 | ||
| 44 | - //run ui thread | ||
| 45 | - @JSMethod(uiThread = true) | ||
| 46 | - public void setStopBits(Integer stopBits){ | ||
| 47 | - this.serialPortHelper.setStopBits(stopBits); | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - //设置数据位 | ||
| 51 | - //run ui thread | ||
| 52 | - @JSMethod(uiThread = true) | ||
| 53 | - public void setDataBits(Integer dataBits){ | ||
| 54 | - this.serialPortHelper.setDataBits(dataBits); | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - //设置检验位 | ||
| 58 | - //run ui thread | ||
| 59 | - @JSMethod(uiThread = true) | ||
| 60 | - public void setParity(Integer parity){ | ||
| 61 | - this.serialPortHelper.setParity(parity); | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - //设置流控 | ||
| 65 | - //run ui thread | ||
| 66 | - @JSMethod(uiThread = true) | ||
| 67 | - public void setFlowCon(Integer flowCon){ | ||
| 68 | - this.serialPortHelper.setFlowCon(flowCon); | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - //设置标志 | ||
| 72 | - //run ui thread | ||
| 73 | - @JSMethod(uiThread = true) | ||
| 74 | - public void setFlags(Integer flags){ | ||
| 75 | - this.serialPortHelper.setFlags(flags); | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - //获取所有串口 | ||
| 79 | - //run ui thread | ||
| 80 | - @JSMethod(uiThread = true) | ||
| 81 | - public void getAllDeviceList(JSCallback callback){ | ||
| 82 | - List<Device> list = this.serialPortHelper.getAllDevices(); | ||
| 83 | - if(callback != null){ | ||
| 84 | - callback.invoke(list); | ||
| 85 | - } | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - //获取所有路径 | ||
| 89 | - //run ui thread | ||
| 90 | - @JSMethod(uiThread = true) | ||
| 91 | - public void getAllDevicePath(JSCallback callback){ | ||
| 92 | - String[] list = this.serialPortHelper.getAllDeicesPath(); | ||
| 93 | - if(callback != null){ | ||
| 94 | - callback.invoke(list); | ||
| 95 | - } | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - //打开串口 | ||
| 99 | - //run ui thread | ||
| 100 | - @JSMethod(uiThread = true) | ||
| 101 | - public void open(final JSCallback callback){ | ||
| 102 | - serialPortHelper.setIOpenSerialPortListener(new IOpenSerialPortListener(){ | ||
| 103 | - public void onSuccess(final File device){ | ||
| 104 | - JSONObject data = new JSONObject(); | ||
| 105 | - data.put("status",true); | ||
| 106 | - data.put("msg",""); | ||
| 107 | - callback.invoke(data); | ||
| 108 | - } | ||
| 109 | - public void onFail(final File device, final Status status){ | ||
| 110 | - JSONObject data = new JSONObject(); | ||
| 111 | - data.put("status",false); | ||
| 112 | - data.put("msg",status); | ||
| 113 | - callback.invoke(data); | ||
| 114 | - } | ||
| 115 | - }); | ||
| 116 | - try{ | ||
| 117 | - serialPortHelper.open(); | ||
| 118 | - }catch (Exception e){ | ||
| 119 | - JSONObject data = new JSONObject(); | ||
| 120 | - data.put("status",false); | ||
| 121 | - data.put("msg","NO_READ_WRITE_PERMISSION"); | ||
| 122 | - callback.invoke(data); | ||
| 123 | - } | ||
| 124 | - } | ||
| 125 | - | ||
| 126 | - //消息监听,字节 | ||
| 127 | - //run ui thread | ||
| 128 | - @JSMethod(uiThread = true) | ||
| 129 | - public void onMessage(final JSCallback callback,final JSCallback sendCallback){ | ||
| 130 | - this.serialPortHelper.setISerialPortDataListener(new ISerialPortDataListener() { | ||
| 131 | - @Override | ||
| 132 | - public void onDataReceived(byte[] bytes) { | ||
| 133 | - callback.invokeAndKeepAlive(bytes); | ||
| 134 | - } | ||
| 135 | - | ||
| 136 | - @Override | ||
| 137 | - public void onDataSend(byte[] bytes) { | ||
| 138 | - if(sendCallback != null){ | ||
| 139 | - sendCallback.invokeAndKeepAlive(bytes); | ||
| 140 | - } | ||
| 141 | - } | ||
| 142 | - }); | ||
| 143 | - } | ||
| 144 | - | ||
| 145 | - //消息监听,十六进制 | ||
| 146 | - //run ui thread | ||
| 147 | - @JSMethod(uiThread = true) | ||
| 148 | - public void onMessageHex(final JSCallback callback,final JSCallback sendCallback){ | ||
| 149 | - this.serialPortHelper.setISerialPortDataListener(new ISerialPortDataListener() { | ||
| 150 | - @Override | ||
| 151 | - public void onDataReceived(byte[] bytes) { | ||
| 152 | - callback.invokeAndKeepAlive(byteToHex(bytes)); | ||
| 153 | - } | ||
| 154 | - | ||
| 155 | - @Override | ||
| 156 | - public void onDataSend(byte[] bytes) { | ||
| 157 | - if(sendCallback != null){ | ||
| 158 | - sendCallback.invokeAndKeepAlive(byteToHex(bytes)); | ||
| 159 | - } | ||
| 160 | - } | ||
| 161 | - }); | ||
| 162 | - | ||
| 163 | - } | ||
| 164 | - | ||
| 165 | - //消息监听,字符串 | ||
| 166 | - //run ui thread | ||
| 167 | - @JSMethod(uiThread = true) | ||
| 168 | - public void onMessageASCII(final JSCallback callback,final JSCallback sendCallback){ | ||
| 169 | - this.serialPortHelper.setISerialPortDataListener(new ISerialPortDataListener() { | ||
| 170 | - @Override | ||
| 171 | - public void onDataReceived(byte[] bytes) { | ||
| 172 | - callback.invokeAndKeepAlive(new String(bytes)); | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - @Override | ||
| 176 | - public void onDataSend(byte[] bytes) { | ||
| 177 | - if(sendCallback != null){ | ||
| 178 | - sendCallback.invokeAndKeepAlive(new String(bytes)); | ||
| 179 | - } | ||
| 180 | - } | ||
| 181 | - }); | ||
| 182 | - } | ||
| 183 | - | ||
| 184 | - //字节转16进制 | ||
| 185 | - //run ui thread | ||
| 186 | - @JSMethod(uiThread = false) | ||
| 187 | - public static String byteToHex(byte[] bytes){ | ||
| 188 | - String strHex = ""; | ||
| 189 | - StringBuilder sb = new StringBuilder(""); | ||
| 190 | - for (int n = 0; n < bytes.length; n++) { | ||
| 191 | - strHex = Integer.toHexString(bytes[n] & 0xFF); | ||
| 192 | - sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0 | ||
| 193 | - } | ||
| 194 | - return sb.toString().trim(); | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - //串口状态 | ||
| 198 | - //run ui thread | ||
| 199 | - @JSMethod(uiThread = false) | ||
| 200 | - public boolean isOpen(){ | ||
| 201 | - return this.serialPortHelper.isOpen(); | ||
| 202 | - } | ||
| 203 | - | ||
| 204 | - //关闭串口 | ||
| 205 | - //run ui thread | ||
| 206 | - @JSMethod(uiThread = true) | ||
| 207 | - public void close(){ | ||
| 208 | - this.serialPortHelper.close(); | ||
| 209 | - } | ||
| 210 | - | ||
| 211 | - //发送字节消息 | ||
| 212 | - //run ui thread | ||
| 213 | - @JSMethod(uiThread = true) | ||
| 214 | - public void sendBytes(byte[] bytes){ | ||
| 215 | - this.serialPortHelper.sendBytes(bytes); | ||
| 216 | - } | ||
| 217 | - | ||
| 218 | - //发送十六进制消息 | ||
| 219 | - //run ui thread | ||
| 220 | - @JSMethod(uiThread = true) | ||
| 221 | - public void sendHex(String hex){ | ||
| 222 | - this.serialPortHelper.sendHex(hex); | ||
| 223 | - } | ||
| 224 | - | ||
| 225 | - //发送字符串消息 | ||
| 226 | - //run ui thread | ||
| 227 | - @JSMethod(uiThread = true) | ||
| 228 | - public void sendASCII(String string){ | ||
| 229 | - this.serialPortHelper.sendTxt(string); | ||
| 230 | - } | ||
| 231 | - | ||
| 232 | -} |
project-wl-kuaidiyuan-android/simpleDemo/debug/output-metadata.json deleted
100644 → 0
| 1 | -{ | ||
| 2 | - "version": 3, | ||
| 3 | - "artifactType": { | ||
| 4 | - "type": "APK", | ||
| 5 | - "kind": "Directory" | ||
| 6 | - }, | ||
| 7 | - "applicationId": "com.diligrp.erp", | ||
| 8 | - "variantName": "debug", | ||
| 9 | - "elements": [ | ||
| 10 | - { | ||
| 11 | - "type": "SINGLE", | ||
| 12 | - "filters": [], | ||
| 13 | - "attributes": [], | ||
| 14 | - "versionCode": 100, | ||
| 15 | - "versionName": "1.4", | ||
| 16 | - "outputFile": "simpleDemo-debug.apk" | ||
| 17 | - } | ||
| 18 | - ], | ||
| 19 | - "elementType": "File", | ||
| 20 | - "minSdkVersionForDexing": 21 | ||
| 21 | -} | ||
| 22 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/simpleDemo/debug/simpleDemo-debug.apk deleted
100644 → 0
No preview for this file type
project-wl-kuaidiyuan-android/simpleDemo/release/baselineProfiles/0/simpleDemo-release.dm deleted
100644 → 0
No preview for this file type
project-wl-kuaidiyuan-android/simpleDemo/release/baselineProfiles/1/simpleDemo-release.dm deleted
100644 → 0
No preview for this file type
project-wl-kuaidiyuan-android/simpleDemo/release/output-metadata.json deleted
100644 → 0
| 1 | -{ | ||
| 2 | - "version": 3, | ||
| 3 | - "artifactType": { | ||
| 4 | - "type": "APK", | ||
| 5 | - "kind": "Directory" | ||
| 6 | - }, | ||
| 7 | - "applicationId": "com.diligrp.erp", | ||
| 8 | - "variantName": "release", | ||
| 9 | - "elements": [ | ||
| 10 | - { | ||
| 11 | - "type": "SINGLE", | ||
| 12 | - "filters": [], | ||
| 13 | - "attributes": [], | ||
| 14 | - "versionCode": 100, | ||
| 15 | - "versionName": "1.4", | ||
| 16 | - "outputFile": "simpleDemo-release.apk" | ||
| 17 | - } | ||
| 18 | - ], | ||
| 19 | - "elementType": "File", | ||
| 20 | - "baselineProfiles": [ | ||
| 21 | - { | ||
| 22 | - "minApi": 28, | ||
| 23 | - "maxApi": 30, | ||
| 24 | - "baselineProfiles": [ | ||
| 25 | - "baselineProfiles/1/simpleDemo-release.dm" | ||
| 26 | - ] | ||
| 27 | - }, | ||
| 28 | - { | ||
| 29 | - "minApi": 31, | ||
| 30 | - "maxApi": 2147483647, | ||
| 31 | - "baselineProfiles": [ | ||
| 32 | - "baselineProfiles/0/simpleDemo-release.dm" | ||
| 33 | - ] | ||
| 34 | - } | ||
| 35 | - ], | ||
| 36 | - "minSdkVersionForDexing": 21 | ||
| 37 | -} | ||
| 38 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/simpleDemo/release/simpleDemo-release.apk deleted
100644 → 0
No preview for this file type
project-wl-kuaidiyuan-android/usbPrinter/.gitignore deleted
100644 → 0
project-wl-kuaidiyuan-android/usbPrinter/build.gradle deleted
100644 → 0
| 1 | -plugins { | ||
| 2 | - id 'com.android.library' | ||
| 3 | - id 'org.jetbrains.kotlin.android' | ||
| 4 | -} | ||
| 5 | - | ||
| 6 | -android { | ||
| 7 | - namespace 'com.diligrp.printer' | ||
| 8 | - compileSdk 35 | ||
| 9 | - | ||
| 10 | - defaultConfig { | ||
| 11 | - minSdk 21 | ||
| 12 | - | ||
| 13 | - consumerProguardFiles "consumer-rules.pro" | ||
| 14 | - } | ||
| 15 | - | ||
| 16 | - buildTypes { | ||
| 17 | - debug { | ||
| 18 | - minifyEnabled false | ||
| 19 | - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
| 20 | - } | ||
| 21 | - release { | ||
| 22 | - minifyEnabled false | ||
| 23 | - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | - compileOptions { | ||
| 27 | - sourceCompatibility JavaVersion.VERSION_11 | ||
| 28 | - targetCompatibility JavaVersion.VERSION_11 | ||
| 29 | - } | ||
| 30 | - kotlinOptions { | ||
| 31 | - jvmTarget = '11' | ||
| 32 | - } | ||
| 33 | -} | ||
| 34 | - | ||
| 35 | -dependencies { | ||
| 36 | -// implementation fileTree(dir: 'libs', include: ['*.jar', "*.aar"]) | ||
| 37 | - | ||
| 38 | - implementation 'androidx.core:core-ktx:1.16.0' | ||
| 39 | - compileOnly 'com.google.android.material:material:1.10.0' | ||
| 40 | - compileOnly 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' | ||
| 41 | - compileOnly 'androidx.core:core:1.1.0' | ||
| 42 | - compileOnly 'androidx.fragment:fragment:1.1.0' | ||
| 43 | - compileOnly 'androidx.appcompat:appcompat:1.1.0' | ||
| 44 | - compileOnly 'androidx.recyclerview:recyclerview:1.1.0' | ||
| 45 | - compileOnly 'com.alibaba:fastjson:1.2.83' | ||
| 46 | - | ||
| 47 | - compileOnly fileTree(include: ['uniapp-v8-release.aar'], dir: '../simpleDemo/libs') | ||
| 48 | - compileOnly fileTree(include: ['androidprintsdk.jar'], dir: '../simpleDemo/libs') | ||
| 49 | -} | ||
| 50 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/usbPrinter/consumer-rules.pro deleted
100644 → 0
project-wl-kuaidiyuan-android/usbPrinter/libs/androidprintsdk.jar deleted
100644 → 0
No preview for this file type
project-wl-kuaidiyuan-android/usbPrinter/proguard-rules.pro deleted
100644 → 0
| 1 | -# Add project specific ProGuard rules here. | ||
| 2 | -# You can control the set of applied configuration files using the | ||
| 3 | -# proguardFiles setting in build.gradle. | ||
| 4 | -# | ||
| 5 | -# For more details, see | ||
| 6 | -# http://developer.android.com/guide/developing/tools/proguard.html | ||
| 7 | - | ||
| 8 | -# If your project uses WebView with JS, uncomment the following | ||
| 9 | -# and specify the fully qualified class name to the JavaScript interface | ||
| 10 | -# class: | ||
| 11 | -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| 12 | -# public *; | ||
| 13 | -#} | ||
| 14 | - | ||
| 15 | -# Uncomment this to preserve the line number information for | ||
| 16 | -# debugging stack traces. | ||
| 17 | -#-keepattributes SourceFile,LineNumberTable | ||
| 18 | - | ||
| 19 | -# If you keep the line number information, uncomment this to | ||
| 20 | -# hide the original source file name. | ||
| 21 | -#-renamesourcefileattribute SourceFile | ||
| 22 | --keep public class * extends io.dcloud.feature.uniapp.common.UniModule{*;} | ||
| 23 | --keep class com.diligrp.printer.** {*;} | ||
| 24 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/usbPrinter/src/main/AndroidManifest.xml deleted
100644 → 0
| 1 | -<?xml version="1.0" encoding="utf-8"?> | ||
| 2 | -<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
| 3 | - | ||
| 4 | - <uses-permission android:name="android.hardware.usb.UsbAccessory" /> | ||
| 5 | - <uses-feature | ||
| 6 | - android:name="android.hardware.usb.host" | ||
| 7 | - android:required="true" /> | ||
| 8 | -</manifest> | ||
| 9 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/usbPrinter/src/main/java/com/diligrp/printer/UsbPrinterClient.kt deleted
100644 → 0
| 1 | -package com.diligrp.printer | ||
| 2 | - | ||
| 3 | -import android.util.Log | ||
| 4 | -import com.alibaba.fastjson.JSONObject | ||
| 5 | - | ||
| 6 | -import io.dcloud.feature.uniapp.annotation.UniJSMethod | ||
| 7 | -import io.dcloud.feature.uniapp.bridge.UniJSCallback | ||
| 8 | -import io.dcloud.feature.uniapp.common.UniModule | ||
| 9 | - | ||
| 10 | -class UsbPrinterClient : UniModule() { | ||
| 11 | - private var mPrinter: UsbPrinterUtils? = UsbPrinterUtils() | ||
| 12 | - | ||
| 13 | - @UniJSMethod(uiThread = true) | ||
| 14 | - fun initDevice() { | ||
| 15 | - mPrinter?.initDevice(mUniSDKInstance.context) | ||
| 16 | - } | ||
| 17 | - | ||
| 18 | - @UniJSMethod(uiThread = true) | ||
| 19 | - fun prepare() { | ||
| 20 | - mPrinter?.prepare() | ||
| 21 | - } | ||
| 22 | - | ||
| 23 | - @UniJSMethod(uiThread = true) | ||
| 24 | - fun printTextLeft(str: String?) { | ||
| 25 | - mPrinter?.printTextLeft(str) | ||
| 26 | - } | ||
| 27 | - | ||
| 28 | - @UniJSMethod(uiThread = true) | ||
| 29 | - fun printTextCenter(str: String?) { | ||
| 30 | - Log.d("Test", "printTextCenter $mPrinter ---------------> $str") | ||
| 31 | - mPrinter?.printTextCenter(str) | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - @UniJSMethod(uiThread = true) | ||
| 35 | - fun printTextRight(str: String?) { | ||
| 36 | - mPrinter?.printTextRight(str) | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - @UniJSMethod(uiThread = true) | ||
| 40 | - fun printEmptyLine(count: Int) { | ||
| 41 | - mPrinter?.printEmptyLine(count) | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - @UniJSMethod(uiThread = true) | ||
| 45 | - fun printGapLine() { | ||
| 46 | - mPrinter?.printGapLine() | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - @UniJSMethod(uiThread = true) | ||
| 50 | - fun onRelease() { | ||
| 51 | - mPrinter?.onRelease() | ||
| 52 | - mPrinter = null | ||
| 53 | - } | ||
| 54 | -} | ||
| 55 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/usbPrinter/src/main/java/com/diligrp/printer/UsbPrinterUtils.kt deleted
100644 → 0
| 1 | -package com.diligrp.printer | ||
| 2 | - | ||
| 3 | -import android.annotation.SuppressLint | ||
| 4 | -import android.content.Context | ||
| 5 | -import android.hardware.usb.UsbManager | ||
| 6 | -import android.os.Handler | ||
| 7 | -import android.os.Message | ||
| 8 | -import android.util.Log | ||
| 9 | -import android.widget.Toast | ||
| 10 | -import com.android.print.sdk.PrinterConstants | ||
| 11 | -import com.android.print.sdk.PrinterInstance | ||
| 12 | -import com.diligrp.printer.utils.IPrinterOpertion | ||
| 13 | -import com.diligrp.printer.utils.UsbOperation | ||
| 14 | - | ||
| 15 | -/** | ||
| 16 | - * UsbPrinterUtils | ||
| 17 | - * | ||
| 18 | - * @Description: | ||
| 19 | - * @Author: | ||
| 20 | - * @CreateDate: 2025/8/11 12:38 | ||
| 21 | - */ | ||
| 22 | -class UsbPrinterUtils { | ||
| 23 | - private var mPrinterOpertion: IPrinterOpertion? = null | ||
| 24 | - private var mPrinter: PrinterInstance? = null | ||
| 25 | - private var isConnected = false //是否已经建立了连接 | ||
| 26 | - private var mContext: Context? = null | ||
| 27 | - | ||
| 28 | - companion object { | ||
| 29 | - const val LEFT = 0 | ||
| 30 | - const val RIGHT = 2 | ||
| 31 | - const val CENTER = 1 | ||
| 32 | - | ||
| 33 | - const val LINE_BREAK = "\n" | ||
| 34 | - const val GAP_LINE = "--------------------------------${LINE_BREAK}" | ||
| 35 | - | ||
| 36 | - @SuppressLint("StaticFieldLeak") | ||
| 37 | - private var _instance: UsbPrinterUtils? = null | ||
| 38 | - | ||
| 39 | - fun getInstance(): UsbPrinterUtils { | ||
| 40 | - if (_instance == null) { | ||
| 41 | - _instance = UsbPrinterUtils() | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - return _instance!! | ||
| 45 | - } | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - fun initDevice(context: Context?) { | ||
| 49 | - if (context == null) { | ||
| 50 | - return | ||
| 51 | - } | ||
| 52 | - mContext = context | ||
| 53 | - mPrinterOpertion = UsbOperation(mContext, mHandler) | ||
| 54 | - val manager = mContext?.getSystemService(Context.USB_SERVICE) as UsbManager | ||
| 55 | - mPrinterOpertion!!.usbAutoConn(manager); | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - fun prepare() { | ||
| 59 | - if (mPrinter != null && isConnected) { | ||
| 60 | - mPrinter?.init() | ||
| 61 | - } | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - fun printTextLeft(str: String?) { | ||
| 65 | - if (mPrinter != null && isConnected && !str.isNullOrEmpty()) { | ||
| 66 | - mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, LEFT) | ||
| 67 | - mPrinter?.printText(str) | ||
| 68 | - } | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - fun printTextCenter(str: String?) { | ||
| 72 | - Log.d("Test", "printTextCenter $mPrinter ------- $isConnected --------> $str") | ||
| 73 | - if (mPrinter != null && isConnected && !str.isNullOrEmpty()) { | ||
| 74 | - mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, CENTER) | ||
| 75 | - mPrinter?.printText(str) | ||
| 76 | - } | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - fun printTextRight(str: String?) { | ||
| 80 | - if (mPrinter != null && isConnected && !str.isNullOrEmpty()) { | ||
| 81 | - mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, RIGHT) | ||
| 82 | - mPrinter?.printText(str) | ||
| 83 | - } | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - fun printEmptyLine(count: Int) { | ||
| 87 | - if (mPrinter != null && isConnected) { | ||
| 88 | - mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, LEFT) | ||
| 89 | - mPrinter?.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, count) | ||
| 90 | - } | ||
| 91 | - } | ||
| 92 | - | ||
| 93 | - fun printGapLine() { | ||
| 94 | - if (mPrinter != null && isConnected) { | ||
| 95 | - mPrinter?.setPrinter(PrinterConstants.Command.ALIGN, LEFT) | ||
| 96 | - mPrinter?.printText(GAP_LINE) | ||
| 97 | - } | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - fun onRelease() { | ||
| 101 | - mHandler.removeCallbacksAndMessages(null) | ||
| 102 | - isConnected = false | ||
| 103 | - mPrinterOpertion?.close() | ||
| 104 | - mPrinterOpertion = null | ||
| 105 | - mPrinter = null | ||
| 106 | - mContext = null | ||
| 107 | - _instance = null | ||
| 108 | - } | ||
| 109 | - | ||
| 110 | - //用于接受连接状态消息的 Handler | ||
| 111 | - private val mHandler: Handler by lazy { | ||
| 112 | - @SuppressLint("HandlerLeak") | ||
| 113 | - object : Handler() { | ||
| 114 | - | ||
| 115 | - @SuppressLint("HandlerLeak") | ||
| 116 | - override fun handleMessage(msg: Message) { | ||
| 117 | - super.handleMessage(msg) | ||
| 118 | - when (msg.what) { | ||
| 119 | - PrinterConstants.Connect.SUCCESS -> { | ||
| 120 | - isConnected = true; | ||
| 121 | - mPrinter = mPrinterOpertion?.getPrinter() | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | - PrinterConstants.Connect.FAILED -> { | ||
| 125 | - showToast("打印机连接失败") | ||
| 126 | - } | ||
| 127 | - | ||
| 128 | - PrinterConstants.Connect.CLOSED -> { | ||
| 129 | - isConnected = false; | ||
| 130 | -// showToast("打印机已关闭") | ||
| 131 | - } | ||
| 132 | - | ||
| 133 | - PrinterConstants.Connect.NODEVICE -> { | ||
| 134 | - isConnected = false; | ||
| 135 | - showToast("未检测到打印机") | ||
| 136 | - } | ||
| 137 | - } | ||
| 138 | - } | ||
| 139 | - } | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | - private fun showToast(msg: String?) { | ||
| 143 | - if (msg.isNullOrEmpty() || mContext == null) { | ||
| 144 | - return | ||
| 145 | - } | ||
| 146 | - Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show() | ||
| 147 | - } | ||
| 148 | -} | ||
| 149 | \ No newline at end of file | 0 | \ No newline at end of file |
project-wl-kuaidiyuan-android/usbPrinter/src/main/java/com/diligrp/printer/utils/IPrinterOpertion.java deleted
100644 → 0
| 1 | -package com.diligrp.printer.utils; | ||
| 2 | - | ||
| 3 | - | ||
| 4 | -import android.content.Context; | ||
| 5 | -import android.content.Intent; | ||
| 6 | -import android.hardware.usb.UsbManager; | ||
| 7 | -import android.os.Handler; | ||
| 8 | - | ||
| 9 | -import com.android.print.sdk.PrinterInstance; | ||
| 10 | - | ||
| 11 | - | ||
| 12 | -public interface IPrinterOpertion { | ||
| 13 | - public void open(Intent data); | ||
| 14 | - | ||
| 15 | - public void close(); | ||
| 16 | - | ||
| 17 | - public void chooseDevice(); | ||
| 18 | - | ||
| 19 | - public PrinterInstance getPrinter(); | ||
| 20 | - | ||
| 21 | - public void usbAutoConn(UsbManager manager); | ||
| 22 | - | ||
| 23 | - public void btAutoConn(Context context, Handler mHandler); | ||
| 24 | - | ||
| 25 | - | ||
| 26 | -} |
project-wl-kuaidiyuan-android/usbPrinter/src/main/java/com/diligrp/printer/utils/USBPortService.java deleted
100644 → 0
| 1 | -package com.diligrp.printer.utils; | ||
| 2 | - | ||
| 3 | -import android.annotation.SuppressLint; | ||
| 4 | -import android.annotation.TargetApi; | ||
| 5 | -import android.app.PendingIntent; | ||
| 6 | -import android.content.BroadcastReceiver; | ||
| 7 | -import android.content.Context; | ||
| 8 | -import android.content.Intent; | ||
| 9 | -import android.content.IntentFilter; | ||
| 10 | -import android.hardware.usb.UsbConstants; | ||
| 11 | -import android.hardware.usb.UsbDevice; | ||
| 12 | -import android.hardware.usb.UsbDeviceConnection; | ||
| 13 | -import android.hardware.usb.UsbEndpoint; | ||
| 14 | -import android.hardware.usb.UsbInterface; | ||
| 15 | -import android.hardware.usb.UsbManager; | ||
| 16 | -import android.os.Handler; | ||
| 17 | -import android.util.Log; | ||
| 18 | - | ||
| 19 | -import com.android.print.sdk.IPrinterPort; | ||
| 20 | -import com.android.print.sdk.util.Utils; | ||
| 21 | - | ||
| 22 | -import java.io.ByteArrayInputStream; | ||
| 23 | -import java.io.OutputStream; | ||
| 24 | - | ||
| 25 | -@TargetApi(12) | ||
| 26 | -public class USBPortService implements IPrinterPort { | ||
| 27 | - private static final String TAG = "USBPrinter"; | ||
| 28 | - private UsbManager mUsbManager; | ||
| 29 | - private UsbDevice mUsbDevice; | ||
| 30 | - private UsbDeviceConnection connection; | ||
| 31 | - private UsbInterface usbInterface; | ||
| 32 | - private UsbEndpoint inEndpoint; | ||
| 33 | - private UsbEndpoint outEndpoint; | ||
| 34 | - private static OutputStream outputStream; | ||
| 35 | - private boolean isOldUSB; | ||
| 36 | - private Handler mHandler; | ||
| 37 | - private int mState; | ||
| 38 | - private Context mContext; | ||
| 39 | - private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION"; | ||
| 40 | - private ConnectThread mConnectThread; | ||
| 41 | - private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { | ||
| 42 | - public void onReceive(Context context, Intent intent) { | ||
| 43 | - String action = intent.getAction(); | ||
| 44 | - Log.w("USBPrinter", "receiver action: " + action); | ||
| 45 | - if ("com.android.usb.USB_PERMISSION".equals(action)) { | ||
| 46 | - synchronized (this) { | ||
| 47 | - USBPortService.this.mContext.unregisterReceiver(USBPortService.this.mUsbReceiver); | ||
| 48 | - UsbDevice device = (UsbDevice) intent.getParcelableExtra("device"); | ||
| 49 | - if (intent.getBooleanExtra("permission", false) && USBPortService.this.mUsbDevice.equals(device)) { | ||
| 50 | - USBPortService.this.connect(); | ||
| 51 | - } else { | ||
| 52 | - USBPortService.this.setState(102); | ||
| 53 | - Log.e("USBPrinter", "permission denied for device " + device); | ||
| 54 | - } | ||
| 55 | - } | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - } | ||
| 59 | - }; | ||
| 60 | - | ||
| 61 | - @SuppressLint("WrongConstant") | ||
| 62 | - public USBPortService(Context context, UsbDevice usbDevice, Handler handler) { | ||
| 63 | - this.mContext = context; | ||
| 64 | - this.mUsbManager = (UsbManager) this.mContext.getSystemService("usb"); | ||
| 65 | - this.mUsbDevice = usbDevice; | ||
| 66 | - this.mHandler = handler; | ||
| 67 | - this.mState = 103; | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - public void open() { | ||
| 71 | - Log.d("USBPrinter", "connect to: " + this.mUsbDevice.getDeviceName()); | ||
| 72 | - if (this.mState != 103) { | ||
| 73 | - this.close(); | ||
| 74 | - } | ||
| 75 | - | ||
| 76 | - if (isUsbPrinter(this.mUsbDevice)) { | ||
| 77 | - if (this.mUsbManager.hasPermission(this.mUsbDevice)) { | ||
| 78 | - this.connect(); | ||
| 79 | - } else { | ||
| 80 | - PendingIntent pendingIntent = PendingIntent.getBroadcast(this.mContext, 0, new Intent("com.android.usb.USB_PERMISSION"), 0); | ||
| 81 | - IntentFilter filter = new IntentFilter("com.android.usb.USB_PERMISSION"); | ||
| 82 | - this.mContext.registerReceiver(this.mUsbReceiver, filter); | ||
| 83 | - this.mUsbManager.requestPermission(this.mUsbDevice, pendingIntent); | ||
| 84 | - } | ||
| 85 | - } else { | ||
| 86 | - this.setState(102); | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - } | ||
| 90 | - | ||
| 91 | - private void connect() { | ||
| 92 | - this.mConnectThread = new ConnectThread((ConnectThread) null); | ||
| 93 | - this.mConnectThread.start(); | ||
| 94 | - } | ||
| 95 | - | ||
| 96 | - public void close() { | ||
| 97 | - Utils.Log("USBPrinter", "close()"); | ||
| 98 | - if (this.connection != null) { | ||
| 99 | - this.connection.releaseInterface(this.usbInterface); | ||
| 100 | - this.connection.close(); | ||
| 101 | - this.connection = null; | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - this.mConnectThread = null; | ||
| 105 | - if (this.mState != 102) { | ||
| 106 | - this.setState(103); | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | - } | ||
| 110 | - | ||
| 111 | - public int write(byte[] data) { | ||
| 112 | - | ||
| 113 | - if (this.connection == null) { | ||
| 114 | - return -1; | ||
| 115 | - } else if (data.length < 64) { | ||
| 116 | - return this.connection.bulkTransfer(this.outEndpoint, data, data.length, 60000); | ||
| 117 | - } else { | ||
| 118 | - int sentLength = 0; | ||
| 119 | - | ||
| 120 | - try { | ||
| 121 | - byte[] buff = new byte[64]; | ||
| 122 | - ByteArrayInputStream bis = new ByteArrayInputStream(data); | ||
| 123 | - | ||
| 124 | - byte[] realData; | ||
| 125 | - int length; | ||
| 126 | - for (sentLength = 0; (length = bis.read(buff)) != -1; sentLength += this.connection.bulkTransfer(this.outEndpoint, realData, realData.length, 60000)) { | ||
| 127 | - realData = new byte[length]; | ||
| 128 | - System.arraycopy(buff, 0, realData, 0, length); | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - bis.close(); | ||
| 132 | - } catch (Exception var7) { | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - return sentLength; | ||
| 136 | - } | ||
| 137 | - } | ||
| 138 | - | ||
| 139 | - public byte[] read() { | ||
| 140 | - if (this.connection != null) { | ||
| 141 | - byte[] retData = new byte[64]; | ||
| 142 | - int readLen = this.connection.bulkTransfer(this.inEndpoint, retData, retData.length, 60000); | ||
| 143 | - Log.w("USBPrinter", "read length:" + readLen); | ||
| 144 | - if (readLen > 0) { | ||
| 145 | - if (readLen == 64) { | ||
| 146 | - return retData; | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | - byte[] realData = new byte[readLen]; | ||
| 150 | - System.arraycopy(retData, 0, realData, 0, readLen); | ||
| 151 | - return realData; | ||
| 152 | - } | ||
| 153 | - } | ||
| 154 | - | ||
| 155 | - return null; | ||
| 156 | - } | ||
| 157 | - | ||
| 158 | - public boolean isOldUSB() { | ||
| 159 | - return this.isOldUSB; | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | - public static boolean isUsbPrinter(UsbDevice device) { | ||
| 163 | - if (device == null) { | ||
| 164 | - return false; | ||
| 165 | - } | ||
| 166 | - if (device.getInterfaceCount() == 0) { | ||
| 167 | - return false; | ||
| 168 | - } | ||
| 169 | - | ||
| 170 | - for (int i = 0; i < device.getInterfaceCount(); i++) { | ||
| 171 | - UsbInterface usbInterface = device.getInterface(i); | ||
| 172 | - | ||
| 173 | - if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER) { | ||
| 174 | - Utils.Log("USBPrinter", "device name: " + device.getDeviceName()); | ||
| 175 | - Utils.Log("USBPrinter", "vid:" + device.getVendorId() + " pid:" + device.getProductId()); | ||
| 176 | - return true; | ||
| 177 | - } | ||
| 178 | - } | ||
| 179 | - return false; | ||
| 180 | - } | ||
| 181 | - | ||
| 182 | - private synchronized void setState(int state) { | ||
| 183 | - Utils.Log("USBPrinter", "setState() " + this.mState + " -> " + state); | ||
| 184 | - if (this.mState != state) { | ||
| 185 | - this.mState = state; | ||
| 186 | - if (this.mHandler != null) { | ||
| 187 | - this.mHandler.obtainMessage(this.mState).sendToTarget(); | ||
| 188 | - } | ||
| 189 | - } | ||
| 190 | - | ||
| 191 | - } | ||
| 192 | - | ||
| 193 | - public int getState() { | ||
| 194 | - return this.mState; | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - private class ConnectThread extends Thread { | ||
| 198 | - private ConnectThread(ConnectThread connectThread) { | ||
| 199 | - } | ||
| 200 | - | ||
| 201 | - public void run() { | ||
| 202 | - boolean hasError = true; | ||
| 203 | - if (USBPortService.this.mUsbManager.hasPermission(USBPortService.this.mUsbDevice)) { | ||
| 204 | - try { | ||
| 205 | - USBPortService.this.usbInterface = USBPortService.this.mUsbDevice.getInterface(0); | ||
| 206 | - | ||
| 207 | - for (int i = 0; i < USBPortService.this.usbInterface.getEndpointCount(); ++i) { | ||
| 208 | - UsbEndpoint ep = USBPortService.this.usbInterface.getEndpoint(i); | ||
| 209 | - if (ep.getType() == 2) { | ||
| 210 | - if (ep.getDirection() == 0) { | ||
| 211 | - USBPortService.this.outEndpoint = ep; | ||
| 212 | - } else { | ||
| 213 | - USBPortService.this.inEndpoint = ep; | ||
| 214 | - } | ||
| 215 | - } | ||
| 216 | - } | ||
| 217 | - | ||
| 218 | - USBPortService.this.connection = USBPortService.this.mUsbManager.openDevice(USBPortService.this.mUsbDevice); | ||
| 219 | - if (USBPortService.this.connection != null && USBPortService.this.connection.claimInterface(USBPortService.this.usbInterface, true)) { | ||
| 220 | - hasError = false; | ||
| 221 | - } | ||
| 222 | - } catch (Exception var5) { | ||
| 223 | - var5.printStackTrace(); | ||
| 224 | - } | ||
| 225 | - } | ||
| 226 | - | ||
| 227 | - synchronized (this) { | ||
| 228 | - USBPortService.this.mConnectThread = null; | ||
| 229 | - } | ||
| 230 | - | ||
| 231 | - if (hasError) { | ||
| 232 | - USBPortService.this.setState(102); | ||
| 233 | - USBPortService.this.close(); | ||
| 234 | - } else { | ||
| 235 | - USBPortService.this.setState(101); | ||
| 236 | - } | ||
| 237 | - | ||
| 238 | - } | ||
| 239 | - } | ||
| 240 | -} | ||
| 241 | - |
project-wl-kuaidiyuan-android/usbPrinter/src/main/java/com/diligrp/printer/utils/UsbOperation.java deleted
100644 → 0
| 1 | -package com.diligrp.printer.utils; | ||
| 2 | - | ||
| 3 | -import android.annotation.TargetApi; | ||
| 4 | -import android.content.BroadcastReceiver; | ||
| 5 | -import android.content.Context; | ||
| 6 | -import android.content.Intent; | ||
| 7 | -import android.content.IntentFilter; | ||
| 8 | -import android.hardware.usb.UsbDevice; | ||
| 9 | -import android.hardware.usb.UsbManager; | ||
| 10 | -import android.os.Build; | ||
| 11 | -import android.os.Handler; | ||
| 12 | -import android.util.Log; | ||
| 13 | -import android.widget.Toast; | ||
| 14 | - | ||
| 15 | -import com.android.print.sdk.PrinterInstance; | ||
| 16 | - | ||
| 17 | -import java.util.ArrayList; | ||
| 18 | -import java.util.HashMap; | ||
| 19 | -import java.util.List; | ||
| 20 | - | ||
| 21 | -@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) | ||
| 22 | -public class UsbOperation implements IPrinterOpertion { | ||
| 23 | - private final static String TAG = "UsbOpertion"; | ||
| 24 | - private Context mContext; | ||
| 25 | - private Handler mHandler; | ||
| 26 | - private PrinterInstance mPrinter; | ||
| 27 | - private UsbDevice mDevice; | ||
| 28 | - private boolean hasRegDisconnectReceiver; | ||
| 29 | - private IntentFilter filter; | ||
| 30 | - | ||
| 31 | - private List<UsbDevice> deviceList; | ||
| 32 | - | ||
| 33 | - public UsbOperation(Context context, Handler handler) { | ||
| 34 | - mContext = context; | ||
| 35 | - mHandler = handler; | ||
| 36 | - hasRegDisconnectReceiver = false; | ||
| 37 | - | ||
| 38 | - filter = new IntentFilter(); | ||
| 39 | - filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); | ||
| 40 | - filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - public void open(Intent data) { | ||
| 44 | - mDevice = data.getParcelableExtra(UsbManager.EXTRA_DEVICE); | ||
| 45 | - mPrinter = new PrinterInstance(mContext, mDevice, mHandler); | ||
| 46 | - // default is gbk... | ||
| 47 | - // mPrinter.setEncoding("gbk"); | ||
| 48 | - mPrinter.openConnection(); | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | - public void close() { | ||
| 52 | - if (mPrinter != null) { | ||
| 53 | - mPrinter.closeConnection(); | ||
| 54 | - mPrinter = null; | ||
| 55 | - } | ||
| 56 | - if (hasRegDisconnectReceiver) { | ||
| 57 | - mContext.unregisterReceiver(myReceiver); | ||
| 58 | - hasRegDisconnectReceiver = false; | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - private final BroadcastReceiver myReceiver = new BroadcastReceiver() { | ||
| 63 | - public void onReceive(Context context, Intent intent) { | ||
| 64 | - String action = intent.getAction(); | ||
| 65 | - Log.i(TAG, "receiver is: " + action); | ||
| 66 | - if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { | ||
| 67 | - // xxxxx | ||
| 68 | - } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { | ||
| 69 | - UsbDevice device = (UsbDevice) intent | ||
| 70 | - .getParcelableExtra(UsbManager.EXTRA_DEVICE); | ||
| 71 | - if (device != null && mPrinter != null | ||
| 72 | - && mPrinter.isConnected() && device.equals(mDevice)) { | ||
| 73 | - close(); | ||
| 74 | - } | ||
| 75 | - } | ||
| 76 | - } | ||
| 77 | - }; | ||
| 78 | - | ||
| 79 | - public PrinterInstance getPrinter() { | ||
| 80 | - if (mPrinter != null && mPrinter.isConnected()) { | ||
| 81 | - if (!hasRegDisconnectReceiver) { | ||
| 82 | - mContext.registerReceiver(myReceiver, filter); | ||
| 83 | - hasRegDisconnectReceiver = true; | ||
| 84 | - } | ||
| 85 | - } | ||
| 86 | - return mPrinter; | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - @Override | ||
| 90 | - public void chooseDevice() { | ||
| 91 | - | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - public void usbAutoConn(UsbManager manager) { | ||
| 95 | - | ||
| 96 | - doDiscovery(manager); | ||
| 97 | - | ||
| 98 | - if (!deviceList.isEmpty()) { | ||
| 99 | - mDevice = deviceList.get(0); | ||
| 100 | - } | ||
| 101 | - if (mDevice != null) { | ||
| 102 | - mPrinter = new PrinterInstance(mContext, mDevice, mHandler); | ||
| 103 | - mPrinter.openConnection(); | ||
| 104 | - } else { | ||
| 105 | - Toast.makeText(mContext, "opened failed!", Toast.LENGTH_SHORT).show(); | ||
| 106 | - } | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | - private void doDiscovery(UsbManager manager) { | ||
| 110 | - HashMap<String, UsbDevice> devices = manager.getDeviceList(); | ||
| 111 | - deviceList = new ArrayList<UsbDevice>(); | ||
| 112 | - for (UsbDevice device : devices.values()) { | ||
| 113 | - if (USBPortService.isUsbPrinter(device)) { | ||
| 114 | - deviceList.add(device); | ||
| 115 | - } | ||
| 116 | - } | ||
| 117 | - } | ||
| 118 | - | ||
| 119 | - @Override | ||
| 120 | - public void btAutoConn(Context context, Handler mHandler) { | ||
| 121 | - } | ||
| 122 | - | ||
| 123 | - | ||
| 124 | -} |