connect_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ConnectThread connection = new ConnectThread(bluetoothList.get(lastChildSelect));
connection.start();
}
I have this button that is supposed to start a new Thread, which in turn launches a bluetooth connection to a device that is already paired. bluetoothList is a list of nearby adapters, and lastChildSelect is the number of the item I have selected in the displayed list (there is only one bluetooth adapter nearby and it is already paired). Here is the thread, taken straight from the developer android examples, but somehow not working properly, even after implementing a fallback hack that I found:
class ConnectThread extends Thread
{
private BluetoothSocket mmClientSock;
private BluetoothDevice device;
public ConnectThread(BluetoothDevice device)
{
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
this.device = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) { }
mmClientSock = tmp;
clientSock = mmClientSock;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmClientSock.connect();
Toast.makeText(getApplicationContext(),"Trying to connect to " + device.getName() + " At" + device.getAddress() ,Toast.LENGTH_LONG).show();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
connectException.printStackTrace();
try {
Log.e("","trying fallback...");
mmClientSock = (BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
mmClientSock.connect();
Log.e("", "Connected");
}
catch (Exception e2) {
Log.e("", "Couldn't establish Bluetooth connection!");
try {
mmClientSock.close();
} catch (IOException closeException) { }
return;
}
}
new ClientSock("Hello World");
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
clientSock.close();
} catch (IOException e) { }
}
}
The error I get is this:
read failed, socket might closed or timeout, read ret: -1
The reason I say there is undefined or random behavior is because very rarely it will connect to my bluetooth server and send a message like it did today when I first loaded it up but now I can't even get a connection let alone send a message. Here is my python server code as well:
import bluetooth
hostMACAddress = "00:1A:7D:DA:71:08"
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port=bluetooth.PORT_ANY
s.bind(("", port))
s.listen(backlog)
port=s.getsockname()[1]
print 'Server listening on port ' + str(port)
while True:
client, clientInfo = s.accept()
print "Got connection with" , clientInfo
try:
while True:
data = client.recv(size)
if data:
print "received [%s] \n " % data
client.send(data)
if data == "Hello World!":
print "Response from client - Hello World"
client.send("Hi yourself")
except:
print("Closing socket")
client.close()
s.close()
clientInfo.close()
Aucun commentaire:
Enregistrer un commentaire