I have a bluetooth transmitter trying to connect to a service that I wrote based on BluetoothChat (Android API).
as soon as I run the command mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID), bluetooth automatically connects (before the command accept();, thereby making accept block the stream). any ideas?
I assume that in the previous run there is a socket that did not close properly - while debugging in this direction did not reveal anything interesting.
The code:
public class BluetoothService extends Service {
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
public void onCreate() {
mAdapter = BluetoothAdapter.getDefaultAdapter();
if (mAdapter == null) {
Log.e(TAG, "Bluetooth is not available");
stopSelf();
return;
}
mState = STATE_NONE;
}
@Override
public int onStartCommand(Intent BluetoothService, int flags, int startId ) {
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
if (mAcceptThread == null) {
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
setState(STATE_LISTEN);
return START_NOT_STICKY;
}
private class AcceptThread extends Thread
{
private BluetoothServerSocket mmServerSocket = null;
public AcceptThread()
{
BluetoothServerSocket tmp = null;
try
{
tmp= mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);
}
catch (IOException e)
{
e.printStackTrace();
stopSelf();
}
mmServerSocket = tmp;
}
public void run() {
setName("AcceptThread");
socket = null;
while (mState != STATE_CONNECTED) {
try {
if (socket==null)
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_LISTEN:
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
try {
socket.close();
} catch (IOException e) {
}
break;
}
}
}
}
}
public void cancel()
{
try {
if(mmServerSocket!=null) mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of server failed", e);
}
}
}
private void connectionLost() {
setState(STATE_LISTEN);
if (mAcceptThread == null) {
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
}
private class ConnectedThread extends Thread {
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
InputStream tmpIn = null;
OutputStream tmpOut = null;
tabulkaBluetooth=socket.getRemoteDevice();
tabulkaMac=tabulkaBluetooth.getAddress();
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
int bytes;
String coordinates = "";
String newCoordinates=null;
byte[] buffer = new byte[1024];
int inputStreamAviableBool=0;
byte[] sendBuffer = new byte[3];
sendBuffer[0] = 0x0a;
sendBuffer[1] = 1;
sendBuffer[2] = 'A';
try {
mmOutStream.write(sendBuffer);
} catch (IOException e2) {
e2.printStackTrace();
}
SystemClock.sleep(1000) ;
int numOfInputReads=1;
while(numOfInputReads<3)
{
try {
inputStreamAviableBool=mmInStream.available();
} catch (IOException e1) {
Log.e(TAG, "checking availability of inputStream IOException:", e1);
}
while(inputStreamAviableBool!=0 ){
try{
bytes = mmInStream.read(buffer);
if(bytes == -1)
Log.e(TAG, "input stream is over");
} catch (IOException e) {
Log.e(TAG, "reading from inputStream IOException:", e);
}
newCoordinates = new String(buffer);
coordinates=coordinates+newCoordinates;
try {
numOfInputReads++;
SystemClock.sleep(2000) ;
mmOutStream.write(sendBuffer);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
inputStreamAviableBool=mmInStream.available();
} catch (IOException e1) {
Log.e(TAG, "checking availability of inputStream IOException:", e1);
}
if(inputStreamAviableBool>=3) inputStreamAviableBool=0;
}
numOfInputReads++;
SystemClock.sleep(2000) ;
}
disconnect();
connectionLost();
stopSelf();
}
public void cancel() {
try {
if(socket!=null) socket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
private void disconnect() {
Log.d(TAG, "closing");
if (mmInStream != null) {
try {
mmInStream.close();
} catch (IOException e) {
Log.e(TAG, "isBt IOE", e);
}
mmInStream = null;
}
if (mmOutStream != null) {
try {
mmOutStream.close();
} catch (IOException e) {
Log.e(TAG, "isBt IOE", e);
}
mmOutStream = null;
}
if (socket != null) {
try{
socket.close();
}
catch (IOException e) {
Log.e(TAG, "socket haven't been close", e);
}
socket = null;
}
Log.d(TAG, "closed");
}
}
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
if (D) Log.d(TAG, "connected");
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
setState(STATE_CONNECTED);
}
}
bergo source
share