当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Android bluetooth communication does not work

问题描述:

Greeings

I have Micromax A110 (android os 4.0.4) and samsung galaxy pop (android os 2.2.1)

downloaded the bluetoothchat sample from sdk and tried running it

when i tried to coonect from micromax to samsung it throws exception with message (e.getMessage)

"connect: Connection is not created (failed or aborted)."

when i tried connect from samsung to micromax it says

"Service discovery failed"

Now i changed the UUID to

private static final UUID MY_UUID = UUID.fromString("fafafafa-afac-11de-8a39-0800200c9a66");

now micromax to samsung same exception message

"connect: Connection is not created (failed or aborted)."

but samsung to micromax it says

"connection refused"

Then i changed the line

 tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

to

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

Method m;

m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

tmp = (BluetoothSocket) m.invoke(device, 1);

as per info in stackoverflow

Now

now micromax to samsung same exception message

"connect: Connection is not created (failed or aborted)."

but samsung to micromax says

"connected" and when i type some message it sends to micromax

But the micromax side no message disaplayed and still waiting for client connections

socket = mmServerSocket.accept();

I couldn't' understand what is going on. The interesting thing is the same used to work well

around a year back. Don't' know any software upgrade happened in these mobiles

Please let me know what could be the problem.

code from the sample

private class AcceptThread extends Thread {

// The local server socket

private final BluetoothServerSocket mmServerSocket;

public AcceptThread() {

BluetoothServerSocket tmp = null;

// Create a new listening server socket

try {

tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);

} catch (IOException e) {

Log.e(TAG, "listen() failed", e);

}

mmServerSocket = tmp;

}

public void run() {

if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);

setName("AcceptThread");

BluetoothSocket socket = null;

// Listen to the server socket if we're not connected

while (mState != STATE_CONNECTED) {

try {

// This is a blocking call and will only return on a

// successful connection or an exception

socket = mmServerSocket.accept();

} catch (IOException e) {

Log.e(TAG, "accept() failed", e);

break;

}

// If a connection was accepted

if (socket != null) {

synchronized (BluetoothChatService.this) {

switch (mState) {

case STATE_LISTEN:

case STATE_CONNECTING:

// Situation normal. Start the connected thread.

connected(socket, socket.getRemoteDevice());

break;

case STATE_NONE:

case STATE_CONNECTED:

// Either not ready or already connected. Terminate new socket.

try {

socket.close();

} catch (IOException e) {

Log.e(TAG, "Could not close unwanted socket", e);

}

break;

}

}

}

}

if (D) Log.i(TAG, "END mAcceptThread");

}

public void cancel() {

if (D) Log.d(TAG, "cancel " + this);

try {

mmServerSocket.close();

} catch (IOException e) {

Log.e(TAG, "close() of server failed", e);

}

}

}

/**

* This thread runs while attempting to make an outgoing connection

* with a device. It runs straight through; the connection either

* succeeds or fails.

*/

private class ConnectThread extends Thread {

private final BluetoothSocket mmSocket;

private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {

mmDevice = device;

BluetoothSocket tmp = null;

// Get a BluetoothSocket for a connection with the

// given BluetoothDevice

try {

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

Method m;

m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

tmp = (BluetoothSocket) m.invoke(device, 1);

} catch (IOException e) {

e.printStackTrace();

}

catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

catch (SecurityException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (NoSuchMethodException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

mmSocket = tmp;

}

public void run() {

Log.i(TAG, "BEGIN mConnectThread");

setName("ConnectThread");

// Always cancel discovery because it will slow down a connection

mAdapter.cancelDiscovery();

// Make a connection to the BluetoothSocket

try {

// This is a blocking call and will only return on a

// successful connection or an exception

mmSocket.connect();

} catch (IOException e) {

connectionFailed();

// Close the socket

try {

mmSocket.close();

} catch (IOException e2) {

Log.e(TAG, "unable to close() socket during connection failure", e2);

}

// Start the service over to restart listening mode

BluetoothChatService.this.start();

return;

}

// Reset the ConnectThread because we're done

synchronized (BluetoothChatService.this) {

mConnectThread = null;

}

// Start the connected thread

connected(mmSocket, mmDevice);

}

public void cancel() {

try {

mmSocket.close();

} catch (IOException e) {

Log.e(TAG, "close() of connect socket failed", e);

}

}

}

网友答案:

solved it by doing the factory reset of the micromax A110

分享给朋友:
您可能感兴趣的文章:
随机阅读: