Fallback to broadcast if direct send fails

This commit is contained in:
GUVWAF
2025-07-12 13:18:09 +02:00
parent d1085fde0b
commit 1b37f46d1e
@@ -203,35 +203,39 @@ class BluetoothConnectionManager(
} }
// Function to send data to a single device (server side) // Function to send data to a single device (server side)
private fun notifyDevice(device: BluetoothDevice, data: ByteArray) { private fun notifyDevice(device: BluetoothDevice, data: ByteArray): Boolean {
try { return try {
characteristic?.let { char -> characteristic?.let { char ->
char.value = data char.value = data
gattServer?.notifyCharacteristicChanged(device, char, false) val result = gattServer?.notifyCharacteristicChanged(device, char, false) ?: false
} result
} ?: false
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, "Error sending to server connection ${device.address}: ${e.message}") Log.w(TAG, "Error sending to server connection ${device.address}: ${e.message}")
connectionScope.launch { connectionScope.launch {
delay(CLEANUP_DELAY) delay(CLEANUP_DELAY)
subscribedDevices.remove(device) subscribedDevices.remove(device)
addressPeerMap.remove(device.getAddress()) addressPeerMap.remove(device.address)
} }
false
} }
} }
// Function to send data to a single device (client side) // Function to send data to a single device (client side)
private fun writeToDeviceConn(deviceConn: DeviceConnection, data: ByteArray) { private fun writeToDeviceConn(deviceConn: DeviceConnection, data: ByteArray): Boolean {
try { return try {
deviceConn.characteristic?.let { char -> deviceConn.characteristic?.let { char ->
char.value = data char.value = data
deviceConn.gatt?.writeCharacteristic(char) val result = deviceConn.gatt?.writeCharacteristic(char) ?: false
} result
} ?: false
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, "Error sending to client connection ${deviceConn.device.address}: ${e.message}") Log.w(TAG, "Error sending to client connection ${deviceConn.device.address}: ${e.message}")
connectionScope.launch { connectionScope.launch {
delay(CLEANUP_DELAY) delay(CLEANUP_DELAY)
cleanupDeviceConnection(deviceConn.device.address) cleanupDeviceConnection(deviceConn.device.address)
} }
false
} }
} }
@@ -255,7 +259,7 @@ class BluetoothConnectionManager(
// If found, send directly // If found, send directly
if (targetDevice != null) { if (targetDevice != null) {
Log.d(TAG, "Send packet type ${packet.type} directly to target device for recipient $recipientID: ${targetDevice.address}") Log.d(TAG, "Send packet type ${packet.type} directly to target device for recipient $recipientID: ${targetDevice.address}")
notifyDevice(targetDevice, data) if (notifyDevice(targetDevice, data))
return // Sent, no need to continue return // Sent, no need to continue
} }
@@ -264,7 +268,7 @@ class BluetoothConnectionManager(
// If found, send directly // If found, send directly
if (targetDeviceConn != null) { if (targetDeviceConn != null) {
Log.d(TAG, "Send packet type ${packet.type} directly to target client connection for recipient $recipientID: ${targetDeviceConn.device.address}") Log.d(TAG, "Send packet type ${packet.type} directly to target client connection for recipient $recipientID: ${targetDeviceConn.device.address}")
writeToDeviceConn(targetDeviceConn, data) if (writeToDeviceConn(targetDeviceConn, data))
return // Sent, no need to continue return // Sent, no need to continue
} }
} }