Alex
- Forum-Beiträge: 17
06.03.2012, 15:03:00 via Website
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);
}
}
}
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);
}
}
}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.