Search code examples
androidkotlinbluetooth-lowenergyuuidandroid-bluetooth

How to covert UUID to 16bit in android kotlin


I am working on bluetooth in my application. I found the article to display allocated UUID from the doc. The two id which I found for blood pressure monitor is 0x1810, 0x2A35 which is for Blood Pressure and Blood Pressure Measurement respectively. I am totally new in this stuff so I don't understand properly. I found this Stack overflow 1 and Stack overflow 2. So I am adding value in my btyeArrayof but it giving me error on my side. Please also guide me how can I pass data to scan filters with multiple id

private fun scanFilters() {
        val scanFilterList = mutableListOf<ScanFilter>()
        val serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"
        val setServiceData = byteArrayOf(
            0x1810, 0x2A35
        )
        val scanFilter = ScanFilter.Builder().setServiceUuid(setServiceData, serviceUuidMaskString).build()
        scanFilterList.add(scanFilter)
    }

Errors

The integer literal does not conform to the expected type Byte

on coming this piece of code

val setServiceData = byteArrayOf(0x1810, 0x2A35)

enter image description here

Another error

Type mismatch.
Required:
ParcelUuid!
Found:
ByteArray

or

Type mismatch.
Required:
ParcelUuid!
Found:
String

val scanFilter = ScanFilter.Builder().setServiceUuid(setServiceData, serviceUuidMaskString).build()

enter image description here


Solution

  • You can express a 16-bit service UUID as a 128-bit service UUID like this:

    0000xxxx-0000-1000-8000-00805F9B34FB

    So for Blood Pressure Management with 16-bit Service UUID 0x2A35 you can use:

    00002A35-0000-1000-8000-00805F9B34FB

    And for Blood Pressure with 16-bit Service UUID 0x1810 you can use:

    00001810-0000-1000-8000-00805F9B34FB

    You can only put one service UUID in a single filter, so if you want to look for both, you need to use two filters. Here's how you do that:

    val filter1 = ScanFilter.Builder().setServiceUuid(ParcelUuid(
      UUID.fromString("00002A35-0000-1000-8000-00805F9B34FB"))).build();
    
    val filter2 = ScanFilter.Builder().setServiceUuid(ParcelUuid(
      UUID.fromString("00001810-0000-1000-8000-00805F9B34FB"))).build();
    
    scanFilterList.add(scanFilter1)
    scanFilterList.add(scanFilter2)