SL4A客户端Python服务器(SL4A client Python server)
概要
我想在Android Phone作为客户端和Python作为服务器之间建立蓝牙连接。 请帮帮我,我无法弄清楚。 服务器端必须在PC上使用Python。 我需要Python代码作为服务器,而Sl4A可以连接到这台PC。 我尝试了很多代码,但没有让它工作。
SL4A Bluetooth_chat.py
import android
import time
droid = android.Android()
droid.toggleBluetoothState(True)
droid.dialogCreateAlert('Be a server?')
droid.dialogSetPositiveButtonText('Yes')
droid.dialogSetNegativeButtonText('No')
droid.dialogShow()
result = droid.dialogGetResponse()
is_server = result.result['which'] == 'positive'
if is_server:
droid.bluetoothMakeDiscoverable()
droid.bluetoothAccept()
else:
droid.bluetoothConnect()
if is_server:
result = droid.dialogGetInput('Chat', 'Enter a message').result
if result is None:
droid.exit()
droid.bluetoothWrite(result + '\n')
while True:
result = droid.dialogGetInput('Chat', 'Enter a message').result
if result is None:
break
droid.bluetoothWrite(result + '\n')
droid.exit()
我的答案
import requests
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"
advertise_service( server_sock, "SampleServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
print("Waiting for connection on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data)
params=userdata)
except IOError:
pass
print("disconnected")
client_sock.close()
server_sock.close()
print("all done")
Summary
I want a Bluetooth connection between an Android Phone as client and Python as server. Please help me I can't figure it out. Server side must be on PC with Python. I need Python code as server and Sl4A can connect to this PC. I tried a lot of code but didn't get it to work.
SL4A Bluetooth_chat.py
import android
import time
droid = android.Android()
droid.toggleBluetoothState(True)
droid.dialogCreateAlert('Be a server?')
droid.dialogSetPositiveButtonText('Yes')
droid.dialogSetNegativeButtonText('No')
droid.dialogShow()
result = droid.dialogGetResponse()
is_server = result.result['which'] == 'positive'
if is_server:
droid.bluetoothMakeDiscoverable()
droid.bluetoothAccept()
else:
droid.bluetoothConnect()
if is_server:
result = droid.dialogGetInput('Chat', 'Enter a message').result
if result is None:
droid.exit()
droid.bluetoothWrite(result + '\n')
while True:
result = droid.dialogGetInput('Chat', 'Enter a message').result
if result is None:
break
droid.bluetoothWrite(result + '\n')
droid.exit()
MY ANSWER
import requests
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"
advertise_service( server_sock, "SampleServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
print("Waiting for connection on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data)
params=userdata)
except IOError:
pass
print("disconnected")
client_sock.close()
server_sock.close()
print("all done")
原文:https://stackoverflow.com/questions/36358930
更新时间:2020-02-17 09:37
最满意答案
尝试使用Google发布的SL4A和ACTS:
SL4A位置: aosp_root/external/sl4a
ACTS测试框架位置: aosp_root/tools/test/connectivity
在测试框架内有一个测试用例,可以帮助您了解BT
通过SPP进行测试: aosp_root/tools/test/connectivity/tests/google/bt/SppTest.py
Try using SL4A and ACTS that Google released:
SL4A location: aosp_root/external/sl4a
ACTS Test Framework Location: aosp_root/tools/test/connectivity
Within the test framework there is a testcase that can give you a start on BT
testing over SPP: aosp_root/tools/test/connectivity/tests/google/bt/SppTest.py
2016-05-04
相关问答
用SD卡啊。 复制到卡上,然后用文件管理器打开就可以了。 我以前在android, symbian上都安装过python。不过android上的python只是偶尔用过几次。
好的,所以我很难找到一个droid命令来写文件。 标准的Python open,write,read,close命令工作正常。 Ok, so I was silly to look for a droid command to write files. The standard Python open, write, read, close commands work fine.
我想你要把你的脚本嵌入一个apk上才行,看看维基吧: http://code.google.com/p/android-scripting/wiki/SharingScripts#Scripts_as_APKs
我决定写我自己的(见下面供参考)。 这可能会变得更好,欢迎任何建议。 import android, os, time
droid = android.Android()
# Specify root directory and make sure it exists.
base_dir = '/sdcard/sl4a/scripts/'
if not os.path.exists(base_dir): os.makedirs(base_dir)
def show_dir(path=base_
...
看来你需要的是蓝牙外观。 以下是一些可能对您有帮助的蓝牙命令: bluetoothAccept
bluetoothActiveConnections
bluetoothConnect
bluetoothDiscoveryCancel
bluetoothDiscoveryStart
bluetoothGetConnectedDeviceName
bluetoothGetLocalAddress
bluetoothGetLocalName
bluetoothGetRemoteDeviceName
bl
...
由于Android内核中缺少驱动程序, pyserial无法正常工作。 然而, sl4a内置USB主机串行支持,具有良好的兼容性 ( 此处为示例代码 )。 pyserial is not working due to missing drivers in the Android kernel. However sl4a has builtin USB Host Serial support with good compatibility (example code here).
首先将name值包装在按钮的引号中: TEST
其次,您需要将该button (以及任何其他输入)包装在该isset($_POST['test'])的表单中isset($_POST['test']) ,以便按预期提交:
...
在Python客户端中:您的提示包含\n但输入的结果不是? 尝试添加一个\n在发送之前进行test 。 In the Python client: Your prompt contains a \n but the result from input does not? Try adding a \n to test before sending.
尝试使用Google发布的SL4A和ACTS: SL4A位置: aosp_root/external/sl4a ACTS测试框架位置: aosp_root/tools/test/connectivity 在测试框架内有一个测试用例,可以帮助您了解BT 通过SPP进行测试: aosp_root/tools/test/connectivity/tests/google/bt/SppTest.py Try using SL4A and ACTS that Google released: SL4A lo
...
看看Python龙卷风http://www.tornadoweb.org/ 。 它实际上是一个非阻塞的Web服务器,并且您有一个WebSocketHandler对象,它可能对您想要执行的操作非常有用。 http://www.tornadoweb.org/en/stable/websocket.html 问候 have a look on Python tornado http://www.tornadoweb.org/. It is in fact a non-blocking webserver
...