Skip to content

Commit

Permalink
Merge pull request #260 from xvrh/xha/service_uuids
Browse files Browse the repository at this point in the history
Add serviceUuids in ScanResult
  • Loading branch information
remonh87 authored May 2, 2021
2 parents 23a5721 + 5657317 commit 315672d
Show file tree
Hide file tree
Showing 18 changed files with 1,551 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.signify.hue.flutterreactiveble.ble
import com.polidea.rxandroidble2.RxBleConnection
import java.util.UUID

data class ScanInfo(val deviceId: String, val name: String, val rssi: Int, val serviceData: Map<UUID, ByteArray>, val manufacturerData: ByteArray) {
data class ScanInfo(val deviceId: String, val name: String, val rssi: Int, val serviceData: Map<UUID, ByteArray>, val serviceUuids: List<UUID>, val manufacturerData: ByteArray) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand All @@ -14,6 +14,7 @@ data class ScanInfo(val deviceId: String, val name: String, val rssi: Int, val s
if (name != other.name) return false
if (rssi != other.rssi) return false
if (serviceData != other.serviceData) return false
if (serviceUuids != other.serviceUuids) return false
if (!manufacturerData.contentEquals(other.manufacturerData)) return false

return true
Expand All @@ -24,6 +25,7 @@ data class ScanInfo(val deviceId: String, val name: String, val rssi: Int, val s
result = 31 * result + name.hashCode()
result = 31 * result + rssi
result = 31 * result + serviceData.hashCode()
result = 31 * result + serviceUuids.hashCode()
result = 31 * result + manufacturerData.contentHashCode()
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ open class ReactiveBleClient(private val context: Context) : BleClient {
?: result.bleDevice.name ?: "",
result.rssi,
result.scanRecord.serviceData.mapKeys { it.key.uuid },
result.scanRecord.serviceUuids?.map { it.uuid } ?: emptyList(),
extractManufacturerData(result.scanRecord.manufacturerSpecificData))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ProtobufMessageConverter {
.setName(scanInfo.name)
.setRssi(scanInfo.rssi)
.addAllServiceData(createServiceDataEntry(scanInfo.serviceData))
.addAllServiceUuids(createServiceUuids(scanInfo.serviceUuids))
.setManufacturerData(ByteString.copyFrom(scanInfo.manufacturerData))
.build()

Expand Down Expand Up @@ -213,6 +214,10 @@ class ProtobufMessageConverter {
return serviceDataEntries
}

private fun createServiceUuids(serviceUuids: List<UUID>): List<pb.Uuid> {
return serviceUuids.map { createUuidFromParcelUuid(it) }
}

private fun createUuidFromParcelUuid(uuid: UUID): pb.Uuid {
val convertedUuid = uuidConverter.byteArrayFromUuid(uuid)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ class ProtobufMessageConverterTest {
assertThat(result.serviceDataList.size).isEqualTo(1)
}

@Test
fun `converts serviceuuids into message structure`() {
val scanInfo = createScanInfo()

val result = protobufConverter.convertScanInfo(scanInfo)

assertThat(result.serviceUuidsList.size).isEqualTo(1)
}

@Test
fun `converts servicedataentry value into message structure`() {
val scanInfo = createScanInfo()
Expand Down Expand Up @@ -178,14 +187,18 @@ class ProtobufMessageConverterTest {
val rssi = 200
val uuid = UUID.randomUUID()
val testString = "12".toByteArray()
val serviceUuid = UUID.randomUUID()

val serviceData = mutableMapOf<UUID, ByteArray>()
serviceData[uuid] = testString

val manufacturerData = "123".toByteArray()

return ScanInfo(deviceId = macAdress, name = deviceName,
rssi = rssi, serviceData = serviceData, manufacturerData = manufacturerData)
return ScanInfo(
deviceId = macAdress, name = deviceName,
rssi = rssi, serviceData = serviceData, manufacturerData = manufacturerData,
serviceUuids = listOf(serviceUuid),
)
}

private fun createCharacteristicRequest(deviceId: String, serviceUuid: UUID): pb.ReadCharacteristicRequest {
Expand Down
Loading

0 comments on commit 315672d

Please sign in to comment.