简体   繁体   中英

Transfer large binary string to BLE device from ios app using corebluetooth

I want send large binary string to BLE device(peripheral) from my ios app(central device). Its working fine with small string, but when iam trying to send large string, It was not receiving and the connection was automatically disconnecting. I have read that we need to divide the large data into multiple chunks to send it. But i didn't find any working sample on that.

Please look at the code send the string

let stringToSend = "0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000,0001010101010101111111111111000000@"

.

let data : NSData = stringToSend.dataUsingEncoding(NSUTF8StringEncoding)!

if positionCharacteristic != nil {

self.polarH7HRMPeripheral.writeValue(data, forCharacteristic: positionCharacteristic, type: .WithoutResponse)

self.polarH7HRMPeripheral.setNotifyValue(true, forCharacteristic: positionCharacteristic)


}

Thanks in advance

Some devices registers are limited in size. They can't contain more than X bytes for processing. Let's assume said BLE device expects a long command in a format of a few short commands. Let's also assume said device able to receive short commands as well. How would the device know the difference between a short command and a partial long command? Using a command format which states command type. For example: A command will contain 1 header byte, 6 content bytes, 1 command type byte. Partial long command will have 11111111 as its command type with the final part as 11111110. Short command will have 00000000 as the its command type.

This is how I "defined" a simple protocol for difference commands for a device.

I would recommend you look for that protocol in the developer manual of that device

Some device accepts 20 bytes at most, so you have to spit your string.

for strMsgPart in stringToSend.split(by: 20) {
    if positionCharacteristic != nil {
        self.polarH7HRMPeripheral.writeValue(strMsgPart, forCharacteristic: positionCharacteristic, type: .WithoutResponse)
        self.polarH7HRMPeripheral.setNotifyValue(true, forCharacteristic: positionCharacteristic)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM