f7a13682
“wangming”
项目初始化
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
package com.printer.sdk.usb
import android.annotation.SuppressLint
import android.app.Activity
import android.bluetooth.BluetoothDevice
import android.content.Context
import android.hardware.usb.UsbDevice
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import etelligens.com.foodsafety.PrintLabel.PrintClasses.DiscoveryCallback
import etelligens.com.foodsafety.R
import java.io.File
class ScanningActivity : AppCompatActivity() {
private lateinit var usbPort: UsbPort
private var devices = ArrayList<UsbDevice>()
var toolbar: Toolbar? = null
var printers: ListView? = null
// var refreshLayout: SwipeRefreshLayout? = null
private lateinit var usbAdapter: UsbDevicesAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scanning)
toolbar = findViewById(R.id.my_toolbar) as Toolbar?
printers = findViewById(R.id.printers)
usbAdapter = UsbDevicesAdapter(this)
usbPort = UsbPort(this)
setup()
}
private fun setup() {
initViews()
initListeners()
initDeviceCallback()
}
private fun initDeviceCallback() {
usbPort.setDiscoveryCallback(object : DiscoveryCallback {
override fun onDiscoveryStarted() {
}
override fun onDiscoveryFinished() {
}
override fun onDeviceFound(device: BluetoothDevice) {
}
override fun onDevicePaired(device: BluetoothDevice) {
// FinalPrint.setPrinter(device.name, device.address)
// Toast.makeText(this@ScanningActivity, "Device Paired", Toast.LENGTH_SHORT).show()
// bluetoothAdapter.notifyDataSetChanged()
// setResult(Activity.RESULT_OK)
// this@ScanningActivity.finish()
}
override fun onDeviceUnpaired(device: BluetoothDevice) {
// Toast.makeText(this@ScanningActivity, "Device unpaired", Toast.LENGTH_SHORT).show()
// val pairedPrinter = FinalPrint.getPairedPrinter()
// if (pairedPrinter != null && pairedPrinter.address == device.address)
// FinalPrint.removeCurrentPrinter()
// devices.remove(device)
// bluetoothAdapter.notifyDataSetChanged()
// bluetooth.startScanning()
}
override fun onError(message: String) {
}
override fun onSerialDiscoveryStarted() {
TODO("Not yet implemented")
}
override fun onSerialDiscoveryFinished() {
TODO("Not yet implemented")
}
override fun onSerialDeviceFound(device: File) {
TODO("Not yet implemented")
}
override fun onSerialDevicePaired(device: File) {
TODO("Not yet implemented")
}
override fun onSerialDeviceUnpaired(device: File) {
TODO("Not yet implemented")
}
override fun onSerialError(message: String) {
TODO("Not yet implemented")
}
override fun onUsbDiscoveryStarted() {
// refreshLayout?.isRefreshing = true
toolbar?.title = "Scanning.."
devices.clear()
// devices.addAll(bluetooth.pairedDevices)
// Log.e("devices===", devices.toString())
usbAdapter.notifyDataSetChanged()
}
override fun onUsbDiscoveryFinished() {
toolbar?.title = if (devices.isNotEmpty()) "Select a Printer" else "No devices"
// refreshLayout?.isRefreshing = false
}
override fun onUsbDeviceFound(device: UsbDevice) {
if (!devices.contains(device)) {
devices.add(device)
usbAdapter.notifyDataSetChanged()
}
}
override fun onUsbError(message: String) {
Toast.makeText(this@ScanningActivity, "Error while pairing", Toast.LENGTH_SHORT).show()
usbAdapter.notifyDataSetChanged()
}
})
}
private fun initListeners() {
// refreshLayout?.setOnRefreshListener { bluetooth.startScanning() }
printers?.setOnItemClickListener { _, _, i, _ ->
val device = devices[i]
val vid=device.vendorId;
val pid=device.productId;
FinalPrint.setPrinter(device.deviceName,"vid:$vid\npid:$pid");
setResult(Activity.RESULT_OK)
this@ScanningActivity.finish()
usbAdapter.notifyDataSetChanged()
}
}
private fun initViews() {
printers?.adapter = usbAdapter
}
override fun onStart() {
super.onStart()
usbPort.onStart()
Handler().postDelayed({
usbPort.startScanning()
}, 1000)
}
override fun onStop() {
super.onStop()
usbPort.onStop()
}
companion object {
const val SCANNING_FOR_PRINTER = 115
}
/* inner class BluetoothDevicesAdapter(private val devices: List<BluetoothDevice>) : RecyclerView.Adapter<BluetoothDevicesAdapter.ViewHolder>() {
// create new views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// inflates the card_view_design view
// that is used to hold list item
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.bluetooth_device_row, parent, false)
return ViewHolder(view)
}
// binds the list items to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val BluetoothDevice = devices[position]
if (devices[position].name.isNullOrEmpty()){
holder.name.setText(devices[position].address)
}else{
holder.name.setText(devices[position].name)
}
when (devices[position].bondState) {
BluetoothDevice.BOND_BONDED -> "Paired"
BluetoothDevice.BOND_BONDING -> "Pairing.."
// sets the image to the imageview from our itemHolder class
// holder.imageView.setImageResource(ItemsViewModel.image)
// sets the text to the textview from our itemHolder class
// holder.textView.text = ItemsViewModel.text
}
// return the number of the items in the list
override fun getItemCount(): Int {
return devices.size
}
// Holds the views for adding it to image and text
inner class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
val name: TextView = itemView.findViewById(R.id.name)
val deviceStatus: TextView = itemView.findViewById(R.id.pairStatus)
}
}
*/
/* inner class BluetoothDevicesAdapter(private val context: Context, private val device: ArrayList<BluetoothDevice>) : BaseAdapter() {
override fun getCount(): Int {
Log.e("size==", device.size.toString())
return device.size
}
override fun getItem(position: Int): Any {
return position
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
return LayoutInflater.from(context)
.inflate(R.layout.bluetooth_device_row, parent, false).apply {
findViewById<TextView>(R.id.name).text = if (device[position].name.isNullOrEmpty()) device[position].address else device[position].name
findViewById<TextView>(R.id.pairStatus).visibility = if (device[position].bondState != BluetoothDevice.BOND_NONE) View.VISIBLE else View.INVISIBLE
findViewById<TextView>(R.id.pairStatus).text = when (device[position].bondState) {
BluetoothDevice.BOND_BONDED -> "Paired"
BluetoothDevice.BOND_BONDING -> "Pairing.."
else -> ""
}
}
}
}*/
inner class UsbDevicesAdapter(context: Context) : ArrayAdapter<UsbDevice>(context, android.R.layout.simple_list_item_1) {
override fun getCount(): Int {
return devices.size
}
@SuppressLint("ViewHolder", "CutPasteId")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val device = devices[position];
val name=device.deviceName;
val vid=device.vendorId;
val pid=device.productId;
val textShowStr="$name\nvid:$vid,pid:$pid";
return LayoutInflater.from(context)
.inflate(R.layout.bluetooth_device_row, parent, false).apply {
findViewById<TextView>(R.id.name).text = textShowStr;
findViewById<TextView>(R.id.pairStatus).visibility = View.INVISIBLE
// findViewById<TextView>(R.id.pairStatus).text = when (devices[position].bondState) {
// BluetoothDevice.BOND_BONDED -> "Paired"
// BluetoothDevice.BOND_BONDING -> "Pairing.."
// else -> ""
// }
findViewById<ImageView>(R.id.pairedPrinter).visibility = View.GONE
}
}
}
}
|