Özet
İstemci olarak bir Android Telefon ve sunucu olarak Python arasında bir Bluetooth bağlantısı istiyorum. Lütfen bana yardım edemem. Sunucu tarafı Python ile PC'de olmalıdır. Sunucu olarak Python koduna ihtiyacım var ve Sl4A bu bilgisayara bağlanabilir. Çok fazla kod denedim ama işe yaramadı.SL4A istemcisi Python sunucusu
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 CEVAP
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")