2016-04-23 21 views
0

私のラズベリーパイがサーバーであり、ラップトップがクライアントである単純なBluetoothクライアント/サーバーを構築しようとしています。Bluetoothサーバーに接続しようとしたときに接続が拒否されました

これは、サーバーのコード(私のラズベリーパイ上で実行されている)である:

#!/usr/bin/python 
# -*- coding: utf-8 

import wifi, bluetooth 

uuid="1e0ca4ea-299d-4335-93eb-27fcfe7fa848" 

print "Setting up Bluetooth socket" 

try: 
    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
    sock.bind(("", 0)) 
    sock.listen(1) 
except IOError as e: 
    print str(e) 


print "Registering service" 
try: 
    bluetooth.advertise_service(sock, "MyService", uuid) 

    while True: 
    print "Waiting for connection..." 
    client_sock,address = sock.accept() 
    print "Accepted connection from {0}".format(address) 

    data = client_sock.recv(1024) 
    print "Received data: {0}".format(data) 

    print "Closing client socket." 
    client_sock.close() 
except IOError as e: 
    print str(e) 

これはWaiting for connection...で、スクリプトが実行され、ブロックを動作するようです。

その後、私のクライアントコード:

#!/usr/bin/python 
# -*- coding: utf-8 

import bluetooth, time 

mac = "00:15:83:E5:E2:46" 
uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848" 

service = [] 
retry = 1 
while len(service) == 0: 
    print "Looking for service on {0}, try {1}".format(mac, retry) 
    service = bluetooth.find_service(address=mac, uuid=uuid) 
    retry = retry + 1 
    time.sleep(1) 

if len(service) == 1: 
    service = service[0] 
    print "Service found. Name={0}".format(service["name"]) 

    print "Connecting to service." 

    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
    try: 
    sock.connect((mac, service["port"])) 
    print "Connected to service on {0} on port {1}".format(mac, service["port"]) 
    except bluetooth.btcommon.BluetoothError as e: 
    print "Connection failed: {0}".format(e) 

elif len(service) == 0: 
    print "No service found on mac {0}.".format(mac) 
else: 
    print "{0} services found for mac/uuid, ignored.".format(len(service)) 

はまた、私はラズベリーパイにconnect()にしようとまでするまで、動作します。私は次のエラーを取得する:

Connecting to service. 
Connection failed: (111, 'Connection refused') 

私は(それはそれを見つけ、それが「接続中」と表示されます)ラズベリーパイにノートパソコンを接続すると、オンラインでの詳細情報を探してみましたが、何かを見つけることができませんでした。

答えて

2

リスニングポートに接続したくない場合にこのエラーが発生します。

Uが.....ポート「0」に耳を傾けている例9999のためにそれを変更し し、クライアントが

+0

[ドキュメント](HTTPSサーバーのアドレスにそのポートに接続する必要があります:// pybluez .googlecode.com/svn/www/docs-0.7/public/bluetooth-module.html#get_available_port)は、「廃止予定です。代わりにポート0にバインドします。 '。また、サービスをリクエストすると、ポート1で実行されていると表示されます。別のポートを選択すると、別のエラーが発生します。 –

+0

曖昧な文書のさらに別の例。ポートを明示的に使用することができました。ポート1は動作しませんでした。 –

関連する問題