Reading long characteristic values using CoreBluetooth

后端 未结 2 382
太阳男子
太阳男子 2020-12-04 13:42

I have a characteristic value which contains the data for an image. In the peripheral I setup the value like this:

_photoUUID = [CBUUID UUIDWithString:bPhoto         


        
2条回答
  •  猫巷女王i
    2020-12-04 14:18

    I tried the approach described by Cora Middleton, but couldn't get it to work. If I understand her approach correctly, she would send all partial data through the update notifications. The problem for me seemed to be that there was no guarantee each update would be read by the central if the values in these notifications would change often in short succession.

    So because that approach didn't work, I did the following:

    • There's some characteristic that I use to keep track of the state of the peripheral. This characteristic would only contain some flags and would send out notifications if one or more flags change. Interactions by the user on the peripheral would change the state and there's one action on the peripheral that the user can perform to trigger a download from a connected central.

    • The data to be downloaded from the central is added to a stack on the peripheral. The last item on the stack is a terminator indicator (an empty NSData object)

    • The central registers to receive notifications of the aforementioned state characteristic. If some flag is set, a download is triggered.

    • On the peripheral side, every time I receive a read request for a certain characteristic, I remove 1 item from the stack and return this item.

    • On the central side I add all data that is returned from the read requests. If the empty data value is retrieved, then I create an object from the returned data (in my case it's a JSON string).

    • On the peripheral side I also know the download is finished after returning the empty NSData object, so afterwards I can change the state once again for the peripheral.

提交回复
热议问题