diff --git a/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt index d8b726f..1cb4f6c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt @@ -41,6 +41,7 @@ import com.casic.birmm.inspect.vm.RouteViewModel import com.clj.fastble.BleManager import com.clj.fastble.callback.BleGattCallback +import com.clj.fastble.callback.BleMtuChangedCallback import com.clj.fastble.callback.BleNotifyCallback import com.clj.fastble.callback.BleScanCallback import com.clj.fastble.callback.BleWriteCallback @@ -312,6 +313,9 @@ //关闭数据传送指令 bleManager.disconnect(connectedDevice) + latLngs.clear() + aMap.clear()//清除原来的路线 + SaveKeyValues.removeKey(LocaleConstant.DEVICE_CODE) withContext(Dispatchers.Main) { binding.currentValueView.text = "--" binding.settingsValueView.text = "--" @@ -319,12 +323,6 @@ binding.deviceModelView.text = "产品型号:未连接" binding.deviceCodeView.text = "设备编号:未连接" "设备已断开连接".show(requireContext()) - - //清除缓存 - bluetoothDevices.clear() - latLngs.clear() - aMap.clear()//清除原来的路线 - SaveKeyValues.removeKey(LocaleConstant.DEVICE_CODE) } } } @@ -396,6 +394,8 @@ } private fun startConnectDevice(device: BleDevice) { + //只要一点击设备列表,就清空蓝牙设备列表,不然会有重复 + bluetoothDevices.clear() // 当前蓝牙设备 bleManager.connect(device, object : BleGattCallback() { override fun onStartConnect() { @@ -406,13 +406,21 @@ LoadingDialogHub.dismiss() } - override fun onConnectSuccess( - bleDevice: BleDevice, - gatt: BluetoothGatt, - status: Int - ) { + override fun onConnectSuccess(bleDevice: BleDevice, gatt: BluetoothGatt, status: Int) { connectedDevice = bleDevice - notifyDeviceService(bleDevice, gatt) + + //修改蓝牙每包数据长度 + bleManager.setMtu(bleDevice, 32, object : BleMtuChangedCallback() { + override fun onMtuChanged(mtu: Int) { + Log.d(kTag, "onMtuChanged: $mtu") + notifyDeviceService(bleDevice, gatt) + } + + override fun onSetMTUFailure(exception: BleException?) { + Log.d(kTag, "onSetMTUFailure: ${exception?.description}") + notifyDeviceService(bleDevice, gatt) + } + }) } override fun onDisConnected( @@ -473,7 +481,7 @@ } override fun onCharacteristicChanged(data: ByteArray) { - // 打开通知后,设备发过来的数据 + Log.d(kTag, data.contentToString()) if (data.first() == 51.toByte() && data.size >= 14) { //[51, 57, 50, 48, 50, 52, 48, 49, 48, 48, 48, 54, 32, 0] lifecycleScope.launch(Dispatchers.IO) { @@ -496,12 +504,39 @@ //发送数据传送指令 sendCommand(LocaleConstant.OPEN_TRANSFER_COMMAND) } - } else if (data.first() == (-86).toByte() && data.size >= 14) { - //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1] - handleMethaneData(data) + } else if (data.first() == (-86).toByte() && data.size % 14 == 0) { + //处理粘包 + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10] + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10] + for (i in data.indices step 14) { + val bytes = ByteArray(14) + System.arraycopy(data, 0, bytes, 0, 14) + handleMethaneData(bytes) + } } else { - //如果收到错误数据,就保存设备编号 - Log.d(kTag, data.contentToString()) + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86] + val indexArray = ArrayList() + data.forEachIndexed { index, byte -> + if (byte == (-86).toByte()) { + indexArray.add(index) + } + } + + //去掉最后一个index,数据肯定不满一帧 + for (i in 0 until indexArray.size - 1) { + if (indexArray[i] % 14 == 0) { + val bytes = ByteArray(14) + System.arraycopy(data, indexArray[i], bytes, 0, 14) + handleMethaneData(bytes) + } else { + //不是14倍数的index+14要小于整体数据长度,不然会越界 + if (indexArray[i] + 14 < data.size) { + val bytes = ByteArray(14) + System.arraycopy(data, indexArray[i], bytes, 0, 14) + handleMethaneData(bytes) + } + } + } } } }) diff --git a/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt index d8b726f..1cb4f6c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/birmm/inspect/fragment/HomePageFragment.kt @@ -41,6 +41,7 @@ import com.casic.birmm.inspect.vm.RouteViewModel import com.clj.fastble.BleManager import com.clj.fastble.callback.BleGattCallback +import com.clj.fastble.callback.BleMtuChangedCallback import com.clj.fastble.callback.BleNotifyCallback import com.clj.fastble.callback.BleScanCallback import com.clj.fastble.callback.BleWriteCallback @@ -312,6 +313,9 @@ //关闭数据传送指令 bleManager.disconnect(connectedDevice) + latLngs.clear() + aMap.clear()//清除原来的路线 + SaveKeyValues.removeKey(LocaleConstant.DEVICE_CODE) withContext(Dispatchers.Main) { binding.currentValueView.text = "--" binding.settingsValueView.text = "--" @@ -319,12 +323,6 @@ binding.deviceModelView.text = "产品型号:未连接" binding.deviceCodeView.text = "设备编号:未连接" "设备已断开连接".show(requireContext()) - - //清除缓存 - bluetoothDevices.clear() - latLngs.clear() - aMap.clear()//清除原来的路线 - SaveKeyValues.removeKey(LocaleConstant.DEVICE_CODE) } } } @@ -396,6 +394,8 @@ } private fun startConnectDevice(device: BleDevice) { + //只要一点击设备列表,就清空蓝牙设备列表,不然会有重复 + bluetoothDevices.clear() // 当前蓝牙设备 bleManager.connect(device, object : BleGattCallback() { override fun onStartConnect() { @@ -406,13 +406,21 @@ LoadingDialogHub.dismiss() } - override fun onConnectSuccess( - bleDevice: BleDevice, - gatt: BluetoothGatt, - status: Int - ) { + override fun onConnectSuccess(bleDevice: BleDevice, gatt: BluetoothGatt, status: Int) { connectedDevice = bleDevice - notifyDeviceService(bleDevice, gatt) + + //修改蓝牙每包数据长度 + bleManager.setMtu(bleDevice, 32, object : BleMtuChangedCallback() { + override fun onMtuChanged(mtu: Int) { + Log.d(kTag, "onMtuChanged: $mtu") + notifyDeviceService(bleDevice, gatt) + } + + override fun onSetMTUFailure(exception: BleException?) { + Log.d(kTag, "onSetMTUFailure: ${exception?.description}") + notifyDeviceService(bleDevice, gatt) + } + }) } override fun onDisConnected( @@ -473,7 +481,7 @@ } override fun onCharacteristicChanged(data: ByteArray) { - // 打开通知后,设备发过来的数据 + Log.d(kTag, data.contentToString()) if (data.first() == 51.toByte() && data.size >= 14) { //[51, 57, 50, 48, 50, 52, 48, 49, 48, 48, 48, 54, 32, 0] lifecycleScope.launch(Dispatchers.IO) { @@ -496,12 +504,39 @@ //发送数据传送指令 sendCommand(LocaleConstant.OPEN_TRANSFER_COMMAND) } - } else if (data.first() == (-86).toByte() && data.size >= 14) { - //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1] - handleMethaneData(data) + } else if (data.first() == (-86).toByte() && data.size % 14 == 0) { + //处理粘包 + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10] + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10] + for (i in data.indices step 14) { + val bytes = ByteArray(14) + System.arraycopy(data, 0, bytes, 0, 14) + handleMethaneData(bytes) + } } else { - //如果收到错误数据,就保存设备编号 - Log.d(kTag, data.contentToString()) + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86] + val indexArray = ArrayList() + data.forEachIndexed { index, byte -> + if (byte == (-86).toByte()) { + indexArray.add(index) + } + } + + //去掉最后一个index,数据肯定不满一帧 + for (i in 0 until indexArray.size - 1) { + if (indexArray[i] % 14 == 0) { + val bytes = ByteArray(14) + System.arraycopy(data, indexArray[i], bytes, 0, 14) + handleMethaneData(bytes) + } else { + //不是14倍数的index+14要小于整体数据长度,不然会越界 + if (indexArray[i] + 14 < data.size) { + val bytes = ByteArray(14) + System.arraycopy(data, indexArray[i], bytes, 0, 14) + handleMethaneData(bytes) + } + } + } } } }) diff --git a/app/src/main/java/com/casic/birmm/inspect/single/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/birmm/inspect/single/fragment/HomePageFragment.kt index 378954b..5747c69 100644 --- a/app/src/main/java/com/casic/birmm/inspect/single/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/birmm/inspect/single/fragment/HomePageFragment.kt @@ -36,6 +36,7 @@ import com.casic.birmm.inspect.utils.SoundPoolHelper import com.clj.fastble.BleManager import com.clj.fastble.callback.BleGattCallback +import com.clj.fastble.callback.BleMtuChangedCallback import com.clj.fastble.callback.BleNotifyCallback import com.clj.fastble.callback.BleScanCallback import com.clj.fastble.callback.BleWriteCallback @@ -94,7 +95,7 @@ override fun initOnCreate(savedInstanceState: Bundle?) { vibrator = requireContext().getSystemService(Context.VIBRATOR_SERVICE) as Vibrator - bleManager.enableLog(true).setSplitWriteNum(16).init(BaseApplication.get()) + bleManager.enableLog(true).init(BaseApplication.get()) binding.mapView.onCreate(savedInstanceState) aMap = binding.mapView.map @@ -268,6 +269,9 @@ //关闭数据传送指令 bleManager.disconnect(connectedDevice) + latLngs.clear() + aMap.clear()//清除原来的路线 + SaveKeyValues.removeKey(LocaleConstant.DEVICE_CODE) withContext(Dispatchers.Main) { binding.currentValueView.text = "--" binding.settingsValueView.text = "--" @@ -275,12 +279,6 @@ binding.deviceModelView.text = "产品型号:未连接" binding.deviceCodeView.text = "设备编号:未连接" "设备已断开连接".show(requireContext()) - - //清除缓存 - bluetoothDevices.clear() - latLngs.clear() - aMap.clear()//清除原来的路线 - SaveKeyValues.removeKey(LocaleConstant.DEVICE_CODE) } } } @@ -352,6 +350,8 @@ } private fun startConnectDevice(device: BleDevice) { + //只要一点击设备列表,就清空蓝牙设备列表,不然会有重复 + bluetoothDevices.clear() // 当前蓝牙设备 bleManager.connect(device, object : BleGattCallback() { override fun onStartConnect() { @@ -364,7 +364,19 @@ override fun onConnectSuccess(bleDevice: BleDevice, gatt: BluetoothGatt, status: Int) { connectedDevice = bleDevice - notifyDeviceService(bleDevice, gatt) + + //修改蓝牙每包数据长度 + bleManager.setMtu(bleDevice, 32, object : BleMtuChangedCallback() { + override fun onMtuChanged(mtu: Int) { + Log.d(kTag, "onMtuChanged: $mtu") + notifyDeviceService(bleDevice, gatt) + } + + override fun onSetMTUFailure(exception: BleException?) { + Log.d(kTag, "onSetMTUFailure: ${exception?.description}") + notifyDeviceService(bleDevice, gatt) + } + }) } override fun onDisConnected( @@ -425,7 +437,6 @@ } override fun onCharacteristicChanged(data: ByteArray) { - // 打开通知后,设备发过来的数据 Log.d(kTag, data.contentToString()) if (data.first() == 51.toByte() && data.size >= 14) { //[51, 57, 50, 48, 50, 52, 48, 49, 48, 48, 48, 54, 32, 0] @@ -449,9 +460,39 @@ //发送数据传送指令 sendCommand(LocaleConstant.OPEN_TRANSFER_COMMAND) } - } else if (data.first() == (-86).toByte() && data.size >= 14) { - //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1] - handleMethaneData(data) + } else if (data.first() == (-86).toByte() && data.size % 14 == 0) { + //处理粘包 + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10] + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10] + for (i in data.indices step 14) { + val bytes = ByteArray(14) + System.arraycopy(data, 0, bytes, 0, 14) + handleMethaneData(bytes) + } + } else { + //[-86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 13, 10, -86] + val indexArray = ArrayList() + data.forEachIndexed { index, byte -> + if (byte == (-86).toByte()) { + indexArray.add(index) + } + } + + //去掉最后一个index,数据肯定不满一帧 + for (i in 0 until indexArray.size - 1) { + if (indexArray[i] % 14 == 0) { + val bytes = ByteArray(14) + System.arraycopy(data, indexArray[i], bytes, 0, 14) + handleMethaneData(bytes) + } else { + //不是14倍数的index+14要小于整体数据长度,不然会越界 + if (indexArray[i] + 14 < data.size) { + val bytes = ByteArray(14) + System.arraycopy(data, indexArray[i], bytes, 0, 14) + handleMethaneData(bytes) + } + } + } } } })