我想做的就是将我的andriod标签连接到我的raspberrypi。我搜索过,我正在使用示例中的代码,但我仍然遇到麻烦。当我在pi上运行我的python脚本并在我的Android设备上使用Blueterm时,我可以在两者之间建立通信。我甚至可以发送和接收字符串。
然而,对于我自己的androaid应用程序,我遇到麻烦并在尝试连接时遇到异常。任何帮助将不胜感激。
python代码:
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代码:
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;
}
}
}
}
}
运行应用时从Android Studio登录
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)
我一直在寻找解决方案并将继续这样做。我有覆盆子pi和三星标签配对。我甚至将标签作为可信设备。我对python代码很满意,因为当我使用我的选项卡运行blueterm时,当我输入&#34; a&#34;时,我得到预期的响应。或&#34; b&#34;。任何帮助都会很棒。谢谢。
答案 0 :(得分:1)
鉴于正在抛出异常并通过评论确认权限设置正确,这可能是这些问题在这些现有答案中得到解决:
IOException: read failed, socket might closed - Bluetooth on Android 4.3