BLE GATT, при чтении характеристик с периодичностью иногда возвращается false

Есть приложение, в нем организован фоновый процесс

   private inner class ServiceHandler(looper: Looper) : Handler(looper){
        override fun handleMessage(msg: Message) {
            try {
                Log.d("Start","Service is started")
                sharedPref = getSharedPreferences("BLE_Setting_Device0", Context.MODE_PRIVATE)
                val period: Long = sharedPref.getString("Device_PERIOD","0")?.toLong()
                    ?.times(60000)
                    ?: 60000
                Thread.sleep(1000)

                Log.d("Start","Device connected? = ${devicesGATT[0]?.connect()}")
                BLE_working(devicesGATT[0]).requestTemperature()
                Thread.sleep(2000)
                BLE_working(devicesGATT[0]).readTemperature()
                Thread.sleep(5000)
                serviceHandler?.sendEmptyMessageDelayed(0,period)
                Log.d("Start","${devicesGATT[0]?.discoverServices()}")
            } catch (e:InterruptedException){
                Thread.currentThread().interrupt()
            }

            Log.d("Start","Service is stop")
            stopSelf(msg.arg1)
        }
    }

в BLE_working(devicesGATT[0]).requestTemperature() - происходит запись в характеристику устройства.

fun requestTemperature (){
    val characteristicRequestTemp = bluetoothGatt?.getService(TEMP_SERVICE)?.getCharacteristic(REQUEST_TEMPERATURE) ?: return
    val byteRequest = ByteArray(1)
    byteRequest[0] = (0..9).random().toByte()
    characteristicRequestTemp.value = byteRequest
    bluetoothGatt?.writeCharacteristic(characteristicRequestTemp)
    Thread.sleep(2000)
    val requesting = bluetoothGatt?.readCharacteristic(characteristicRequestTemp)
    Thread.sleep(2000)
    Log.d("Temperature","Temp requesting = $requesting")
    Log.d("Temperature","Values request temp = ${characteristicRequestTemp.value[0]}")
}

в BLE_working(devicesGATT[0]).readTemperature() - происходит чтение характеристики

fun readTemperature(){
    val characteristicReadTemperature = bluetoothGatt?.getService(TEMP_SERVICE)?.getCharacteristic(TEMPERATURE) ?: return
    Thread.sleep(2000)
    val requesting = bluetoothGatt?.readCharacteristic(characteristicReadTemperature)
    if (requesting == false) {
        bluetoothGatt?.readRemoteRssi()
    }
    Thread.sleep(2000)
    Log.d("Temperature","Temp reading = $requesting")
}

Проблема состоит в следующем. При чтении характеристики bluetoothGatt?.readCharacteristic(characteristicReadTemperature) она возвращает иногда false. В чем может быть причина, и как этого избежать? Как сделать так что бы я мог считать характеристику каждую минуту?


Ответы (0 шт):