iphone - Calling close() in iOS on a bluetooth connection causes data to be lost -
we have application sending data on bluetooth closes connection.
the root problem calling close() before data has been sent on bluetooth causes ios not send data.
this looks ios issue. each write done @ lowest nsoutputstream level, (we've added code catch event generated after each write says more space available) appears ios somehow buffering things internally , out of our control. when final close() called, depending on timing, ios might discard pending data though calls write had returned success , associate event of more space available received. i've been looking @ apple's documentation easessions , associated streams, haven't been able find way determine if there still pending data in flight internally within ios.
so far, rough hack of adding call sleep() arbitrary number of seconds before close() eliminate problem, delay needed dependent on how data sent , how underlying bluetooth transfer takes, there's not easy way know before-hand how many seconds delay needs be.
we have needed add delay after consecutive writes. using value of 20 ms fail while having value of 200ms succeed, results in slow communications.
(nsinteger) write:(nsdata *)data error:(nserror **)error { nsoutputstream *outputstream = [[test_eadsessioncontroller sharedcontroller] getoutputstream]; if(m_isconnected == no) { [testerror seterror:error witherrorcode:test_error_no_connection]; return -1; } const void* dataasbytes = [data bytes]; nsinteger len = [data length]; nsinteger byteswritten = 0; while(byteswritten <len){ int maxtimetowaitforspaceavailable = 2000 * 1000; while([outputstream hasspaceavailable] == no && maxtimetowaitforspaceavailable > 0) { usleep(100 * 1000); maxtimetowaitforspaceavailable -= 100 * 1000; } nsinteger bytestowrite = min(1024, len - byteswritten); nsinteger tmp = [outputstream write:dataasbytes + byteswritten maxlength:bytestowrite]; usleep((int)([self timetowaitafterwriteinmilliseconds] * 1000)); if(tmp <= 0){ byteswritten = -1; [testerror seterror:error witherrorcode:test_error_write_failure]; break; } byteswritten += tmp; } usleep((int)([self timetowaitafterwriteinmilliseconds] * 1000)); return byteswritten;
}
Comments
Post a Comment