using the version of BluetoothServerSocket.accept that doesn't take a

timeout makes it slightly more likely that HTC Sensation will notice
an incoming connection, but it's still unusable most of the time.
This commit is contained in:
Eric House 2012-02-22 14:08:38 -08:00
parent cf77872dd4
commit 1b542a1d22

View file

@ -30,7 +30,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -320,8 +319,9 @@ public class BTService extends Service {
@Override @Override
public void run() { public void run() {
try { try {
String appName = XWApp.getAppName( BTService.this );
m_serverSocket = m_adapter. m_serverSocket = m_adapter.
listenUsingRfcommWithServiceRecord( XWApp.getAppName(), listenUsingRfcommWithServiceRecord( appName,
XWApp.getAppUUID() ); XWApp.getAppUUID() );
} catch ( java.io.IOException ioe ) { } catch ( java.io.IOException ioe ) {
DbgUtils.logf( "listenUsingRfcommWithServiceRecord=>%s", DbgUtils.logf( "listenUsingRfcommWithServiceRecord=>%s",
@ -335,8 +335,8 @@ public class BTService extends Service {
DataInputStream inStream = null; DataInputStream inStream = null;
int nRead = 0; int nRead = 0;
try { try {
DbgUtils.logf( "run: calling accept(60000)" ); DbgUtils.logf( "run: calling accept()" );
socket = m_serverSocket.accept( 60000 ); // blocks socket = m_serverSocket.accept(); // blocks
addAddr( socket ); addAddr( socket );
DbgUtils.logf( "run: accept() returned" ); DbgUtils.logf( "run: accept() returned" );
inStream = new DataInputStream( socket.getInputStream() ); inStream = new DataInputStream( socket.getInputStream() );
@ -384,12 +384,14 @@ public class BTService extends Service {
public void stopListening() public void stopListening()
{ {
try { if ( null != m_serverSocket ) {
m_serverSocket.close(); try {
} catch ( java.io.IOException ioe ) { m_serverSocket.close();
DbgUtils.logf( "close()=>%s", ioe.toString() ); } catch ( java.io.IOException ioe ) {
DbgUtils.logf( "close()=>%s", ioe.toString() );
}
m_serverSocket = null;
} }
m_serverSocket = null;
interrupt(); interrupt();
} }
@ -490,7 +492,8 @@ public class BTService extends Service {
R.string.new_btmove_body ); R.string.new_btmove_body );
// do nothing // do nothing
} else { } else {
DbgUtils.logf( "nobody took msg for gameID %d", gameID ); DbgUtils.logf( "nobody took msg for gameID %X",
gameID );
} }
} else { } else {
DbgUtils.logf( "receiveMessages: read only %d of %d bytes", DbgUtils.logf( "receiveMessages: read only %d of %d bytes",
@ -631,7 +634,7 @@ public class BTService extends Service {
DataOutputStream os = connect( socket, BTCmd.PING ); DataOutputStream os = connect( socket, BTCmd.PING );
if ( null != os ) { if ( null != os ) {
os.flush(); os.flush();
Thread killer = killSocketIn( socket, 10 ); Thread killer = killSocketIn( socket );
DataInputStream is = DataInputStream is =
new DataInputStream( socket.getInputStream() ); new DataInputStream( socket.getInputStream() );
@ -708,7 +711,7 @@ public class BTService extends Service {
outStream.write( elem.m_msg, 0, elem.m_msg.length ); outStream.write( elem.m_msg, 0, elem.m_msg.length );
outStream.flush(); outStream.flush();
Thread killer = killSocketIn( socket, 10 ); Thread killer = killSocketIn( socket );
DataInputStream inStream = DataInputStream inStream =
new DataInputStream( socket.getInputStream() ); new DataInputStream( socket.getInputStream() );
@ -940,6 +943,11 @@ public class BTService extends Service {
body ); body );
} }
private Thread killSocketIn( final BluetoothSocket socket )
{
return killSocketIn( socket, 10 );
}
private Thread killSocketIn( final BluetoothSocket socket, int seconds ) private Thread killSocketIn( final BluetoothSocket socket, int seconds )
{ {
final int millis = seconds * 1000; final int millis = seconds * 1000;