BluetoothKit icon indicating copy to clipboard operation
BluetoothKit copied to clipboard

Delay of Sending or Receiving data to BLE Device?

Open nladuo opened this issue 7 years ago • 6 comments

I am using the BLE CC2541 module to interact with my arduino device.

For testing, I connected the TxD pin and RxD pin directly for that I can get the same data when I send data in iOS App.

Like this:

First, I used the Example of this repository. After I modified the UUID of service and characteristic, I successfully scanned my BLE device. But when I send multiple data, it only received one.

And then I created a simple project, It has a button for sending data, a textfield for what to send and a textview for displaying the received data.

The code is like this:

    @IBAction func onSendBtnClicked(_ sender: Any) {
        
        let data: Data = (inputTextField.text?.data(using: String.Encoding.utf8))!
        
        
        central.sendData(data, toRemotePeer: remotePeripheral) { data, remotePeripheral, error in
            guard error == nil else {
                print("Failed sending to \(remotePeripheral)")
                return
            }
            print("Sent to \(remotePeripheral)")
        }
    }

    func remotePeer(_ remotePeer: BKRemotePeer, didSendArbitraryData data: Data) {
        print("Recieved: \(String(data: data, encoding: String.Encoding.utf8) ?? "error")")
        
        receivedTextView.text = receivedTextView.text + String(data: data, encoding: String.Encoding.utf8)!
    }

I filled the TextField with "gghh", after multiple clicking on sendBtn, what it returned like this:

Previously, I used a objective-c library: SerialGATT.h, SerialGATT.m which can receive the data at real time when I tap the button. I am using pod 'BluetoothKit', '~> 0.4.0'.

nladuo avatar Mar 26 '17 15:03 nladuo

Who is sending the data that you receive there, the external device? I would suggest that you look into why it only sends you that data once.

rhummelmose avatar Mar 29 '17 10:03 rhummelmose

Yes, it is a external device, a Bluetooth 4.0 UART Transceiver Serial Module. When I connected the RxD Pin and the TxD Pin and then sent data to this device, this device would send the data what it had received simultaneously.

nladuo avatar Mar 30 '17 02:03 nladuo

Could be that the device doesn't support receiving as big chunks as the iOS device.

rhummelmose avatar Mar 30 '17 05:03 rhummelmose

It has a objective-c based sample and works well. I want to migrate it to swift-based one.

-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data {
    UInt16 s = [self swap:serviceUUID];
    UInt16 c = [self swap:characteristicUUID];
    NSData *sd = [[NSData alloc] initWithBytes:(char *)&s length:2];
    NSData *cd = [[NSData alloc] initWithBytes:(char *)&c length:2];
    CBUUID *su = [CBUUID UUIDWithData:sd];
    CBUUID *cu = [CBUUID UUIDWithData:cd];
    CBService *service = [self findServiceFromUUIDEx:su p:p];
    if (!service) {
        printf("Could not find service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:su],[self UUIDToString:(__bridge CFUUIDRef )p.identifier]);
        return;
    }
    CBCharacteristic *characteristic = [self findCharacteristicFromUUIDEx:cu service:service];
    if (!characteristic) {
        printf("Could not find characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:cu],[self CBUUIDToString:su],[self UUIDToString:(__bridge CFUUIDRef )p.identifier]);
        return;
    }
    if(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse)
    {
        [p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
    }else
    {
        [p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
    }
}

nladuo avatar Mar 31 '17 05:03 nladuo

I faced the same problem when try to send photos between two devices.It's 50% failed, and need to wait so long to receive the photo.

nhatlee avatar Apr 05 '17 07:04 nhatlee

@nhatlee BLE isn't made for transferring large files, hence the speed. I haven't encountered these issues, I will try to reproduce.

rhummelmose avatar Apr 05 '17 07:04 rhummelmose