rhummelmose / BluetoothKit

Easily communicate between iOS/OSX devices using BLE
Other
2.27k stars 267 forks source link

Delay of Sending or Receiving data to BLE Device? #70

Open nladuo opened 7 years ago

nladuo commented 7 years ago

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'.

rhummelmose commented 7 years ago

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.

nladuo commented 7 years ago

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.

rhummelmose commented 7 years ago

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

nladuo commented 7 years ago

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];
    }
}
nhatlee commented 7 years ago

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.

rhummelmose commented 7 years ago

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