diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt new file mode 100644 index 0000000..06c30aa --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt @@ -0,0 +1,44 @@ +package com.casic.birmm.inspect.utils + +import java.io.* + + +/** + * 将Log日志写入文件中 + * */ +object LogToFile { + /** + * @param file 待写入的文件 + * @param log 待写入的内容 + */ + fun write(file: File?, log: String?) { + try { + val bufferedWriter = BufferedWriter(FileWriter(file, true)) + bufferedWriter.write(log) + bufferedWriter.newLine() //换行 + bufferedWriter.flush() + bufferedWriter.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun read(file: File?): String? { + val builder: StringBuilder + try { + val bufferedReader = BufferedReader(FileReader(file)) + var line: String? = bufferedReader.readLine() + builder = StringBuilder() + while (line != null) { + builder.append(line) + builder.append("\n") + line = bufferedReader.readLine() + } + bufferedReader.close() + return builder.toString() + } catch (e: IOException) { + e.printStackTrace() + } + return "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt new file mode 100644 index 0000000..06c30aa --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt @@ -0,0 +1,44 @@ +package com.casic.birmm.inspect.utils + +import java.io.* + + +/** + * 将Log日志写入文件中 + * */ +object LogToFile { + /** + * @param file 待写入的文件 + * @param log 待写入的内容 + */ + fun write(file: File?, log: String?) { + try { + val bufferedWriter = BufferedWriter(FileWriter(file, true)) + bufferedWriter.write(log) + bufferedWriter.newLine() //换行 + bufferedWriter.flush() + bufferedWriter.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun read(file: File?): String? { + val builder: StringBuilder + try { + val bufferedReader = BufferedReader(FileReader(file)) + var line: String? = bufferedReader.readLine() + builder = StringBuilder() + while (line != null) { + builder.append(line) + builder.append("\n") + line = bufferedReader.readLine() + } + bufferedReader.close() + return builder.toString() + } catch (e: IOException) { + e.printStackTrace() + } + return "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt index 9cce3ca..b18d01e 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt @@ -12,10 +12,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.AuthenticationHelper -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.google.gson.Gson import com.qmuiteam.qmui.util.QMUIDisplayHelper @@ -71,6 +68,11 @@ userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { if (it.code == 200) { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 startActivity(Intent(this, MainActivity::class.java)) finish() diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt new file mode 100644 index 0000000..06c30aa --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt @@ -0,0 +1,44 @@ +package com.casic.birmm.inspect.utils + +import java.io.* + + +/** + * 将Log日志写入文件中 + * */ +object LogToFile { + /** + * @param file 待写入的文件 + * @param log 待写入的内容 + */ + fun write(file: File?, log: String?) { + try { + val bufferedWriter = BufferedWriter(FileWriter(file, true)) + bufferedWriter.write(log) + bufferedWriter.newLine() //换行 + bufferedWriter.flush() + bufferedWriter.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun read(file: File?): String? { + val builder: StringBuilder + try { + val bufferedReader = BufferedReader(FileReader(file)) + var line: String? = bufferedReader.readLine() + builder = StringBuilder() + while (line != null) { + builder.append(line) + builder.append("\n") + line = bufferedReader.readLine() + } + bufferedReader.close() + return builder.toString() + } catch (e: IOException) { + e.printStackTrace() + } + return "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt index 9cce3ca..b18d01e 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt @@ -12,10 +12,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.AuthenticationHelper -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.google.gson.Gson import com.qmuiteam.qmui.util.QMUIDisplayHelper @@ -71,6 +68,11 @@ userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { if (it.code == 200) { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 startActivity(Intent(this, MainActivity::class.java)) finish() diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt index 2db634f..6273b87 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt @@ -10,10 +10,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues -import com.casic.birmm.inspect.utils.StatusBarColorUtil +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -43,6 +40,11 @@ val userViewModel = ViewModelProvider(this).get(UserViewModel::class.java) userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 }) userViewModel.loadState.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt new file mode 100644 index 0000000..06c30aa --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt @@ -0,0 +1,44 @@ +package com.casic.birmm.inspect.utils + +import java.io.* + + +/** + * 将Log日志写入文件中 + * */ +object LogToFile { + /** + * @param file 待写入的文件 + * @param log 待写入的内容 + */ + fun write(file: File?, log: String?) { + try { + val bufferedWriter = BufferedWriter(FileWriter(file, true)) + bufferedWriter.write(log) + bufferedWriter.newLine() //换行 + bufferedWriter.flush() + bufferedWriter.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun read(file: File?): String? { + val builder: StringBuilder + try { + val bufferedReader = BufferedReader(FileReader(file)) + var line: String? = bufferedReader.readLine() + builder = StringBuilder() + while (line != null) { + builder.append(line) + builder.append("\n") + line = bufferedReader.readLine() + } + bufferedReader.close() + return builder.toString() + } catch (e: IOException) { + e.printStackTrace() + } + return "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt index 9cce3ca..b18d01e 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt @@ -12,10 +12,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.AuthenticationHelper -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.google.gson.Gson import com.qmuiteam.qmui.util.QMUIDisplayHelper @@ -71,6 +68,11 @@ userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { if (it.code == 200) { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 startActivity(Intent(this, MainActivity::class.java)) finish() diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt index 2db634f..6273b87 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt @@ -10,10 +10,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues -import com.casic.birmm.inspect.utils.StatusBarColorUtil +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -43,6 +40,11 @@ val userViewModel = ViewModelProvider(this).get(UserViewModel::class.java) userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 }) userViewModel.loadState.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 28b7649..eb91fad 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -29,8 +29,8 @@ import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList +import com.casic.birmm.inspect.extensions.toDeviceCode import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener @@ -198,15 +198,18 @@ } else { Log.d(Tag, "设备返回值长度异常,无法解析") } - } else { + } else if (firstByte == 51.toByte()) { //解析deviceCode + //51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 if (receiveByteArray.size >= 12) { mapActivity.deviceStatusView.text = - "设备编号: ${receiveByteArray.toAsciiString()}" + "设备编号: ${receiveByteArray.toDeviceCode()}" BLEManager.sendCommand(Constant.OPEN_TRANSFER_COMMAND) } else { Log.d(Tag, "设备返回值长度异常,无法解析") } + } else { + Log.d(Tag, "未知返回值,无法解析") } } Constant.RECEIVE_FAILURE -> { @@ -286,8 +289,8 @@ eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { - if (BLEManager.isEnable()) { - BLEManager.openBluetooth(this, false) + if (!BLEManager.isEnable()) { + BLEManager.openBluetooth(false) } } else { "该设备不支持低功耗蓝牙".show(this) @@ -597,37 +600,45 @@ * * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 * */ - addInspectionViewModel.addInspection( - id = inspectionId, - name = inspectionName, - startTime = inspectionBean.startTime, - endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), - date = inspectionBean.date, - startLng = inspectionBean.startLng, - startLat = inspectionBean.startLat, - endLng = aMapLocation.longitude, - endLat = aMapLocation.latitude, - routes = gson.toJson(routes), - user = userDataModel.name!! - ) - addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> - if (model.code == 200) { - "巡检记录保存成功".show(this@MapActivity) - SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) - } else { - "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) - SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) - } - finish() - }) - addInspectionViewModel.loadState.observe(this@MapActivity, Observer { - when (it) { - is LoadState.Loading -> { - OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + if (OtherUtils.isNetworkConnected(this@MapActivity)) { + addInspectionViewModel.addInspection( + id = inspectionBean.serverMainId, + name = inspectionBean.name, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + //有网但是因为别的情况导致提交失败 + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) } - else -> OtherUtils.dismissLoadingDialog() - } - }) + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } else { + //无网导致提交失败 + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + finish() + } } }) } diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt new file mode 100644 index 0000000..06c30aa --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt @@ -0,0 +1,44 @@ +package com.casic.birmm.inspect.utils + +import java.io.* + + +/** + * 将Log日志写入文件中 + * */ +object LogToFile { + /** + * @param file 待写入的文件 + * @param log 待写入的内容 + */ + fun write(file: File?, log: String?) { + try { + val bufferedWriter = BufferedWriter(FileWriter(file, true)) + bufferedWriter.write(log) + bufferedWriter.newLine() //换行 + bufferedWriter.flush() + bufferedWriter.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun read(file: File?): String? { + val builder: StringBuilder + try { + val bufferedReader = BufferedReader(FileReader(file)) + var line: String? = bufferedReader.readLine() + builder = StringBuilder() + while (line != null) { + builder.append(line) + builder.append("\n") + line = bufferedReader.readLine() + } + bufferedReader.close() + return builder.toString() + } catch (e: IOException) { + e.printStackTrace() + } + return "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt index 9cce3ca..b18d01e 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt @@ -12,10 +12,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.AuthenticationHelper -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.google.gson.Gson import com.qmuiteam.qmui.util.QMUIDisplayHelper @@ -71,6 +68,11 @@ userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { if (it.code == 200) { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 startActivity(Intent(this, MainActivity::class.java)) finish() diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt index 2db634f..6273b87 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt @@ -10,10 +10,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues -import com.casic.birmm.inspect.utils.StatusBarColorUtil +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -43,6 +40,11 @@ val userViewModel = ViewModelProvider(this).get(UserViewModel::class.java) userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 }) userViewModel.loadState.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 28b7649..eb91fad 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -29,8 +29,8 @@ import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList +import com.casic.birmm.inspect.extensions.toDeviceCode import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener @@ -198,15 +198,18 @@ } else { Log.d(Tag, "设备返回值长度异常,无法解析") } - } else { + } else if (firstByte == 51.toByte()) { //解析deviceCode + //51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 if (receiveByteArray.size >= 12) { mapActivity.deviceStatusView.text = - "设备编号: ${receiveByteArray.toAsciiString()}" + "设备编号: ${receiveByteArray.toDeviceCode()}" BLEManager.sendCommand(Constant.OPEN_TRANSFER_COMMAND) } else { Log.d(Tag, "设备返回值长度异常,无法解析") } + } else { + Log.d(Tag, "未知返回值,无法解析") } } Constant.RECEIVE_FAILURE -> { @@ -286,8 +289,8 @@ eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { - if (BLEManager.isEnable()) { - BLEManager.openBluetooth(this, false) + if (!BLEManager.isEnable()) { + BLEManager.openBluetooth(false) } } else { "该设备不支持低功耗蓝牙".show(this) @@ -597,37 +600,45 @@ * * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 * */ - addInspectionViewModel.addInspection( - id = inspectionId, - name = inspectionName, - startTime = inspectionBean.startTime, - endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), - date = inspectionBean.date, - startLng = inspectionBean.startLng, - startLat = inspectionBean.startLat, - endLng = aMapLocation.longitude, - endLat = aMapLocation.latitude, - routes = gson.toJson(routes), - user = userDataModel.name!! - ) - addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> - if (model.code == 200) { - "巡检记录保存成功".show(this@MapActivity) - SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) - } else { - "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) - SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) - } - finish() - }) - addInspectionViewModel.loadState.observe(this@MapActivity, Observer { - when (it) { - is LoadState.Loading -> { - OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + if (OtherUtils.isNetworkConnected(this@MapActivity)) { + addInspectionViewModel.addInspection( + id = inspectionBean.serverMainId, + name = inspectionBean.name, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + //有网但是因为别的情况导致提交失败 + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) } - else -> OtherUtils.dismissLoadingDialog() - } - }) + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } else { + //无网导致提交失败 + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + finish() + } } }) } diff --git a/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt index 65e04d2..9cc3a7a 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt @@ -3,9 +3,12 @@ import androidx.core.content.ContextCompat import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity +import com.casic.birmm.inspect.utils.FileUtils +import com.casic.birmm.inspect.utils.LogToFile import com.casic.birmm.inspect.utils.PageNavigationManager import com.casic.birmm.inspect.utils.StatusBarColorUtil import com.gyf.immersionbar.ImmersionBar +import kotlinx.android.synthetic.main.activity_show_log.* import kotlinx.android.synthetic.main.include_base_title.* class ShowLogActivity : BaseActivity() { @@ -19,6 +22,7 @@ } override fun initData() { + userLoginLogView.text = LogToFile.read(FileUtils.documentFile) } override fun initEvent() { diff --git a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt index 8b2ea6e..e547591 100644 --- a/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt +++ b/app/src/main/java/com/casic/birmm/inspect/extensions/ByteArray.kt @@ -16,8 +16,15 @@ ).toUpperCase(Locale.ROOT) + if (hasSpace) " " else "" } -//ByteArray转ascii码字符串 -fun ByteArray.toAsciiString(): String { +//ByteArray转设备编号 +fun ByteArray.toDeviceCode(): String { + /** + * 51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 + * + * 51是数据标头 + * 13,10是数据结束位 + * 后面的是其他参数,不解析 + * */ val builder = StringBuilder() for (index in 1..12) { builder.append(this[index].toInt().toChar()) @@ -25,6 +32,15 @@ return builder.toString() } +//ByteArray转Char字符串 +fun ByteArray.toCharString(): String { + val builder = StringBuilder() + this.forEach { + builder.append(it.toInt().toChar()) + } + return builder.toString() +} + //ByteArray转十进制字符串集合 fun ByteArray.toDecStringList(): ArrayList { val data: ArrayList = ArrayList() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index df9fb17..6f044ba 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -25,34 +25,28 @@ object BLEManager { private const val Tag = "BLEManager" private const val MAX_CONNECT_TIME = 10000L//连接超时时间10s - private var context: Context? = null - private var bluetoothManager: BluetoothManager? = null + private lateinit var context: Context + private lateinit var serviceUUID: UUID + private lateinit var readUUID: UUID + private lateinit var writeUUID: UUID private var bluetoothAdapter: BluetoothAdapter? = null private val handler = Handler() private var onDeviceSearchListener: OnDeviceSearchListener? = null private var isConnecting = false - private lateinit var curConnectDevice: BluetoothDevice - private lateinit var serviceUUID: UUID - private lateinit var readUUID: UUID - private lateinit var writeUUID: UUID private var onBleConnectListener: OnBleConnectListener? = null private var bluetoothGatt: BluetoothGatt? = null - private var bluetoothGattService: BluetoothGattService? = null private var readCharacteristic: BluetoothGattCharacteristic? = null private var writeCharacteristic: BluetoothGattCharacteristic? = null fun initBle(context: Context): Boolean { this.context = context - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - bluetoothManager = + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - if (bluetoothManager == null) { - return false - } - bluetoothAdapter = bluetoothManager!!.adapter - return bluetoothAdapter != null + bluetoothAdapter = bluetoothManager.adapter + bluetoothAdapter != null } else { - return false + false } } @@ -67,7 +61,7 @@ * 打开蓝牙 * @param isFast true 直接打开蓝牙 false 提示用户打开 */ - fun openBluetooth(context: Context, isFast: Boolean) { + fun openBluetooth(isFast: Boolean) { if (!isEnable()) { if (isFast) { Log.d(Tag, "直接打开手机蓝牙") @@ -147,7 +141,6 @@ Log.d(Tag, "connectBleDevice()-->isConnecting = true") return } - this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) this.readUUID = UUID.fromString(readUUID) this.writeUUID = UUID.fromString(writeUUID) @@ -379,8 +372,7 @@ var notifyCharacteristic: BluetoothGattCharacteristic? = null bluetoothGatt.services.forEach { service -> if (service.uuid == serviceUUID) { - bluetoothGattService = service - bluetoothGattService!!.characteristics.forEach { characteristic -> + service!!.characteristics.forEach { characteristic -> val charaProp = characteristic.properties if (characteristic.uuid == readUUID) { //读特征 readCharacteristic = characteristic @@ -389,7 +381,7 @@ writeCharacteristic = characteristic } if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) { - val notifyServiceUUID = bluetoothGattService!!.uuid + val notifyServiceUUID = service.uuid val notifyCharacteristicUUID = characteristic.uuid Log.d( Tag, @@ -413,18 +405,18 @@ return true } - fun sendCommand(cmd: ByteArray): Boolean { + fun sendCommand(cmd: ByteArray) { if (writeCharacteristic == null) { Log.d(Tag, "sendCommand(ByteArray)-->writeGattCharacteristic == null") - return false + return } if (bluetoothGatt == null) { Log.d(Tag, "sendCommand(ByteArray)-->bluetoothGatt == null") - return false + return } val value = writeCharacteristic!!.setValue(cmd) Log.d(Tag, "写特征设置值结果:$value") - return bluetoothGatt!!.writeCharacteristic(writeCharacteristic) + bluetoothGatt!!.writeCharacteristic(writeCharacteristic) } fun disConnectDevice() { diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt index 8b6d696..50bfc97 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/FileUtils.kt @@ -34,6 +34,39 @@ return imageDir.toString() } + //储存下载文件的目录 + private val downloadFilePath: String + get() { + val downloadDir = + File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") + if (!downloadDir.exists()) { + val mkdir = downloadDir.mkdir() + if (mkdir) { + Log.d(Tag, "创建downloadFilePath文件夹") + } + } + return downloadDir.toString() + } + + val documentFile: File + get() { + val documentDir = File( + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "" + ) + //以天区分名字 + val timeStamp = SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(Date()) + val logFile = + File(documentDir.toString() + File.separator + "Log_" + timeStamp + ".txt") + if (!logFile.exists()) { + try { + logFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + return logFile + } + val waterImageFile: File get() { val waterImageDir = @@ -58,20 +91,6 @@ return imageFile } - //储存下载文件的目录 - private val downloadFilePath: String - get() { - val downloadDir = - File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") - if (!downloadDir.exists()) { - val mkdir = downloadDir.mkdir() - if (mkdir) { - Log.d(Tag, "创建downloadFilePath文件夹") - } - } - return downloadDir.toString() - } - fun downloadFile(url: String, listener: IDownloadListener) { val okHttpClient = OkHttpClient() val request = Request.Builder().get().url(url).build() diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt new file mode 100644 index 0000000..06c30aa --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LogToFile.kt @@ -0,0 +1,44 @@ +package com.casic.birmm.inspect.utils + +import java.io.* + + +/** + * 将Log日志写入文件中 + * */ +object LogToFile { + /** + * @param file 待写入的文件 + * @param log 待写入的内容 + */ + fun write(file: File?, log: String?) { + try { + val bufferedWriter = BufferedWriter(FileWriter(file, true)) + bufferedWriter.write(log) + bufferedWriter.newLine() //换行 + bufferedWriter.flush() + bufferedWriter.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun read(file: File?): String? { + val builder: StringBuilder + try { + val bufferedReader = BufferedReader(FileReader(file)) + var line: String? = bufferedReader.readLine() + builder = StringBuilder() + while (line != null) { + builder.append(line) + builder.append("\n") + line = bufferedReader.readLine() + } + bufferedReader.close() + return builder.toString() + } catch (e: IOException) { + e.printStackTrace() + } + return "" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt index 9cce3ca..b18d01e 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/GuideActivity.kt @@ -12,10 +12,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.AuthenticationHelper -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.google.gson.Gson import com.qmuiteam.qmui.util.QMUIDisplayHelper @@ -71,6 +68,11 @@ userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { if (it.code == 200) { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 startActivity(Intent(this, MainActivity::class.java)) finish() diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt index 2db634f..6273b87 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MainActivity.kt @@ -10,10 +10,7 @@ import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.utils.Constant -import com.casic.birmm.inspect.utils.PageNavigationManager -import com.casic.birmm.inspect.utils.SaveKeyValues -import com.casic.birmm.inspect.utils.StatusBarColorUtil +import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.vm.UserViewModel import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -43,6 +40,11 @@ val userViewModel = ViewModelProvider(this).get(UserViewModel::class.java) userViewModel.getUserInfo() userViewModel.resultModel.observe(this, Observer { + //记录登录log + LogToFile.write( + FileUtils.documentFile, + TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()) + " ${it.data!!.name}登录成功" + ) SaveKeyValues.putValue(Constant.USER_OBJECT, Gson().toJson(it))//保存用户对象 }) userViewModel.loadState.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 28b7649..eb91fad 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -29,8 +29,8 @@ import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show -import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList +import com.casic.birmm.inspect.extensions.toDeviceCode import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener @@ -198,15 +198,18 @@ } else { Log.d(Tag, "设备返回值长度异常,无法解析") } - } else { + } else if (firstByte == 51.toByte()) { //解析deviceCode + //51, 51, 50, 48, 50, 49, 48, 49, 48, 48, 48, 51, 13, 10, -86, 0, 0, 0, 0, 0 if (receiveByteArray.size >= 12) { mapActivity.deviceStatusView.text = - "设备编号: ${receiveByteArray.toAsciiString()}" + "设备编号: ${receiveByteArray.toDeviceCode()}" BLEManager.sendCommand(Constant.OPEN_TRANSFER_COMMAND) } else { Log.d(Tag, "设备返回值长度异常,无法解析") } + } else { + Log.d(Tag, "未知返回值,无法解析") } } Constant.RECEIVE_FAILURE -> { @@ -286,8 +289,8 @@ eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { - if (BLEManager.isEnable()) { - BLEManager.openBluetooth(this, false) + if (!BLEManager.isEnable()) { + BLEManager.openBluetooth(false) } } else { "该设备不支持低功耗蓝牙".show(this) @@ -597,37 +600,45 @@ * * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 * */ - addInspectionViewModel.addInspection( - id = inspectionId, - name = inspectionName, - startTime = inspectionBean.startTime, - endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), - date = inspectionBean.date, - startLng = inspectionBean.startLng, - startLat = inspectionBean.startLat, - endLng = aMapLocation.longitude, - endLat = aMapLocation.latitude, - routes = gson.toJson(routes), - user = userDataModel.name!! - ) - addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> - if (model.code == 200) { - "巡检记录保存成功".show(this@MapActivity) - SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) - } else { - "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) - SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) - } - finish() - }) - addInspectionViewModel.loadState.observe(this@MapActivity, Observer { - when (it) { - is LoadState.Loading -> { - OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + if (OtherUtils.isNetworkConnected(this@MapActivity)) { + addInspectionViewModel.addInspection( + id = inspectionBean.serverMainId, + name = inspectionBean.name, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + //有网但是因为别的情况导致提交失败 + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) } - else -> OtherUtils.dismissLoadingDialog() - } - }) + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } else { + //无网导致提交失败 + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + finish() + } } }) } diff --git a/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt index 65e04d2..9cc3a7a 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/ShowLogActivity.kt @@ -3,9 +3,12 @@ import androidx.core.content.ContextCompat import com.casic.birmm.inspect.R import com.casic.birmm.inspect.base.BaseActivity +import com.casic.birmm.inspect.utils.FileUtils +import com.casic.birmm.inspect.utils.LogToFile import com.casic.birmm.inspect.utils.PageNavigationManager import com.casic.birmm.inspect.utils.StatusBarColorUtil import com.gyf.immersionbar.ImmersionBar +import kotlinx.android.synthetic.main.activity_show_log.* import kotlinx.android.synthetic.main.include_base_title.* class ShowLogActivity : BaseActivity() { @@ -19,6 +22,7 @@ } override fun initData() { + userLoginLogView.text = LogToFile.read(FileUtils.documentFile) } override fun initEvent() { diff --git a/app/src/main/res/layout/activity_show_log.xml b/app/src/main/res/layout/activity_show_log.xml index 82c95a8..d9dec9b 100644 --- a/app/src/main/res/layout/activity_show_log.xml +++ b/app/src/main/res/layout/activity_show_log.xml @@ -5,4 +5,18 @@ android:orientation="vertical"> + + + + + \ No newline at end of file