Probleme beim connectenvon Galaxy Nexus und SPP Device über bluetooth

  • Antworten:2
  • OffenNicht stickyBentwortet

06.03.2012 15:03:00 via Website

hallo,

ich möchte ein App schreiben, um Daten zwischen dem Handy und einem anderen Bluetooth device auszutauschen. Ich habe mich dabei an der BLuetoothChat vorlage orientiert. Jedoch kann ich die Verbindung auf meinem Galaxy Nexus garnicht und auf meinem Galaxy s nur hin und wieder erfolgreich aufbauen.

Hat jemand eine idee woran das liegen könnte?

mein App is dabei der client und das Bluetooth device der Server.

Danke im voraus.

CODE der beiden Threads:



private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private String mSocketType;

public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
//ParcelUuid[] uuids = servicesFromDevice(device);

try {
tmp = device.createRfcommSocketToServiceRecord(my_uuid);
//Log.d(TAG, "uuids.length: "+ uuids.length);
//Log.d(TAG, "uuid: " + uuids[1].getUuid());
//tmp = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + "create() failed",e);
}
mmSocket = tmp;
}

public void run() {
Log.i(TAG, "BEGIN mConnectThread SocketType:" );
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();
run=true;
} catch (IOException e) {
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() " +
" socket during connection failure", e2);
}
Log.e(TAG, e.getMessage());
connectionFailed();
return;
}

// Reset the ConnectThread because we're done
synchronized (connection.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);
}
}
}

/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread: ");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the BluetoothSocket input and output streams
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");
byte[] buffer = new byte[1024];
int bytes;

// Keep listening to the InputStream while connected
while (run) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
Log.i(TAG, "data received: "+ buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(main.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
//connectionLost();
// Start the service over to restart listening mode
//connection.this.start();
break;
}
}
}

/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);

// Share the sent message back to the UI Activity
mHandler.obtainMessage(main.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}

public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}

07.03.2012 11:05:26 via Website

Hi, arbeite gerade an ziemlich genau dem selben Thema / Problem.

Erster Vorschlag, versuchs mit:

tmp = device.createInsecureRfcommSocketToServiceRecord(my_uuid);

my_uuid wird ja wohl stimmen, oder?

Was meint der Log bei den fehlgeschlagenen Verbindungen?

Gruss Oli

Aktuelle Artikel aus unserem Magazin

07.03.2012 13:40:12 via Website

ok habs gelöst.

Method m = null;
try {
m = device.getClass().getMethod("createInsecureRfcommSocket",
new Class[] { int.class });
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tmp = (BluetoothSocket)m.invoke(device, Integer.valueOf(1));
} 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();
}

nur komisch warum es auf einem handy einigermaßen ging und auf dem anderen nicht :)