SL4A 客户端 Python 服务器
SL4A client Python server
总结
我想要 Android Phone 作为客户端和 Python 作为服务器之间的蓝牙连接。请帮帮我,我想不通。服务器端必须在 Python 的 PC 上。我需要 Python 代码作为服务器,Sl4A 可以连接到这台电脑。我尝试了很多代码,但没有让它工作。
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")
尝试使用 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
总结
我想要 Android Phone 作为客户端和 Python 作为服务器之间的蓝牙连接。请帮帮我,我想不通。服务器端必须在 Python 的 PC 上。我需要 Python 代码作为服务器,Sl4A 可以连接到这台电脑。我尝试了很多代码,但没有让它工作。
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")
尝试使用 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