B4i Library iExternalAccessory - A flexible and customize way to connect MFi devices

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
as I can see in the library, the buffer is controlled by device, is not from the library, so you can use the method cleardata to clear after you receive:

you see the variable: hasBytesAvailable ?? this is part of control from device

B4X:
while ([[session inputStream] hasBytesAvailable])
    {
        NSInteger bytesRead = [[session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE];
        if (_readData == nil) {
            _readData = [[NSMutableData alloc] init];
        }
        [_readData appendBytes:(void *)buf length:bytesRead];
        //NSLog(@"read %d bytes from input stream", bytesRead);
        if(_DebugMode) NSLog(@"[_readData]:Bytes:%ld",(long)bytesRead);

    }
 

Turbo3

Active Member
Licensed User
Longtime User
That works. I did not see the "cleardata" method listed at the top where you listed the methods so did not know about it.

Is there some reason you defined BytesReceived as a string when it is really an array of bytes? Even your example prints a pointer when it logs "BytesReceived". It is not a string.

_ReadData (BytesReceived As String)
 

Turbo3

Active Member
Licensed User
Longtime User
Its not a matter of conversion. The type is wrong. I fixed it by changing:

ReadData (BytesReceived As String)

to

ReadData (BytesReceived() As Bytes)
 

Turbo3

Active Member
Licensed User
Longtime User
I see a potential problem with the app asynchronously calling "cleardata".

What if more data comes in between the time ReadData is called and "cleardata" is executed. It seems data would be lost.

Cleardata needs to clear only the data returned to the app when ReadData is called. Any data received after Readdata is called needs to be accumulated in a different buffer. So you need a ping-pong set of buffers so nothing gets lost.

I can place the OBDLink MX+ into a mode where it will continuously send data. This means ReadData is going to be called over and over again without the app every sending another command. There is not going to be a safe time for my app to call cleardata without the risk of clearing data the app has not received yet.

Please look at setting up a pair of buffers, that way the cleardata will not be necessary and no data will be lost.
 

Turbo3

Active Member
Licensed User
Longtime User
I am trying to see if I can automatically connect to the device as the list accessory does not work if there is no connection with the device.

Can a function be added to list the Paired Bluetooth devices?

Can a function be added to connect to a Paired device?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…