Reputation: 255
All that I would like to do is connect my andriod tab to my raspberrypi. I have searched and I am using code found in examples but I am still having troubles. I can establish communication between the two when I run my python script on the pi and use Blueterm on my android device. I can even send and receive strings.
However with my own androaid app I am having trouble and getting an exception when trying to connect. Any help would be greatly appreciated.
python code:
from bluetooth import *
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
x = 0
while x<3:
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
x = 1
while x == 1:
try:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
if data == 'temp':
data = str(read_temp())+'!'
elif data == 'a':
data = 'A A A!'
elif data == 'b':
data = 'B B B'
else:
data = 'WTF!'
x = 5
client_sock.send(data)
print "sending [%s]" % data
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break
if x == 5:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
Android Code:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
Button tryBluetoothButton;
BluetoothSocket mmSocket = null;
BluetoothDevice mmDevice = null;
BluetoothAdapter mBluetoothAdapter;
public void tryBT(View view) {
Log.i("myStuff", "Button Clicked");
sendBtMsg("a");
}
public void sendBtMsg(String msg2send) {
UUID uuid = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
Log.i("myStuff", "Conected OK!");
} catch (IOException e) { }{
Log.i("myStuff", "EXCEPTION THROWN");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tryBluetoothButton = (Button) findViewById(R.id.tryBluetoothButton);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
Log.i("myStuff", "Bluetooth Enabled");
} else {
Log.i("myStuff", "Bluetooth Already Enabled");
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName().equals("raspberrypi"))
{
mmDevice = device;
Log.i("myStuff", "Device equals " + device.getName());
break;
}
}
}
}
}
Log from Android Studio when I run the app
01-20 16:33:42.983 8994-8994/paul.piconnect2 I/myStuff: Bluetooth Already Enabled
01-20 16:33:43.003 8994-8994/paul.piconnect2 I/myStuff: Device equals raspberrypi
01-20 16:33:48.889 8994-8994/paul.piconnect2 I/myStuff: Button Clicked
01-20 16:33:50.831 8994-8994/paul.piconnect2 I/myStuff: EXCEPTION THROWN
01-20 18:30:53.092 12714-12714/paul.piconnect2 I/myStuff: EXCEPTION THROWN
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:582)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:593)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:378)
at paul.piconnect2.MainActivity.sendBtMsg(MainActivity.java:35)
at paul.piconnect2.MainActivity.tryBT(MainActivity.java:25)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19270)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
I have been searching for a solution and will continue to do so. I have the raspberry pi and the samsung tab paired. I even have the tab as a trusted device. I am good with the python code because when I run blueterm with my tab I get the expected response when I type in "a" or "b". Any help would be great. Thank you.
Upvotes: 0
Views: 4665
Reputation: 7935
Given the exception that is being thrown and confirming via comments that permissions are set correctly, perhaps this is an issue that's addresssed in these existing answers:
IOException: read failed, socket might closed - Bluetooth on Android 4.3
BluetoothSocket.connect() throwing exception "read failed"
Upvotes: 1