我正在尝试创建一个Android应用程序,用于接收和显示来自配对蓝牙设备的数据。我有一个工作的python代码,我想在android中编写类似的代码。我可以在Android手机中运行Python但没有UI!代码使用RFCOMM找到端口,然后使用socket连接。蓝牙设备的地址是XX:XX:XX:XX:XX:XX格式,我不知道如何为android编写代码。如果代码是你帮我!谢谢! 这是我的python代码:
from bluetooth import *
from sys import stdout
server_address = "XX:XX:XX:XX:XX:XX"
#port = get_available_port( RFCOMM )
try:
my=sock
except:
sock=BluetoothSocket( RFCOMM )
sock.connect((server_address, 1))
print "connected"
else:
print "---"
data=0
while 1:
data= sock.recv(1)
if data== '':
print "Socket broken"
else:
data= data
print "received"
print text
sock.close()
sock=None
del sock
这个android代码能帮忙吗?
String deviceAddress = "XX:XX:XX:XX:XX:XX";
adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = _adapter.getRemoteDevice(deviceAddress);
try {
socket = device.createRfcommSocketToServiceRecord(BluetoothSerialUuid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:1)
您可以在SDK中找到此示例的完整源代码:
<sdk>/platforms/android-<version>/samples/
BLUETOOTH CHAT
的会看到该代码并过滤您想要的内容:)