2015-06-14 45 views
1

我有一个非常特别的Android的蓝牙问题:具体Android的蓝牙服务器问题

我有一个设备(几乎没有文档)充当蓝牙客户端,并尝试连接(配对后),以蓝牙Server中使用UUID“1234”进行侦听。我已经预先使用python脚本进行了测试,该设备工作并且连接到服务器。从脚本中的相关代码:

import bluetooth 

uuid = "1234" 

pc = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 

pc.bind(("", bluetooth.PORT_ANY)) 
pc.listen(1) 

port = pc.getsockname()[1] 
print 'Active RFCOMM port: ', port, '\r\n' 

bluetooth.advertise_service(pc, "Server", 
    service_id = uuid, 
    service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ], 
    profiles = [ bluetooth.SERIAL_PORT_PROFILE ] 
) 

print 'Waiting for connection...\r\n' 
address = pc.accept() 
print 'Accepting connection from: ', address, '\r\n' 

我想创建使用BluetoothServerSocket在Android类似的服务器。我已将根据此article的16位UUID(“1234”)转换为“000-0000-1000-8000-00805F9B34FB”。我用python脚本测试过服务器是有效的,并且可以使用指定的UUID连接到它。使用的代码是非常标准的,因为它可以在互联网的许多例子中找到:

@Override 
public void run() { 
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 

    Timber.i("Start Server"); 
    try { 
     final BluetoothServerSocket bluetoothServer = adapter.listenUsingRfcommWithServiceRecord("Server", mUUID); 
     Timber.i("Server Started: " + mUUID.toString()); 

     BluetoothSocket socket = bluetoothServer.accept(); 
     Timber.i("Socket initiated: " + socket.getRemoteDevice().getName()); 

     bluetoothServer.close(); 
    } catch (IOException e) { 
     Timber.e("Error starting server: " + e.getMessage()); 
    } 
} 

我试图与这两个listenUsingRfcommWithServiceRecordlistenUsingInsecureRfcommWithServiceRecord。我确定手机和设备已配对,并且手机可以发现。我曾尝试过使用不同操作系统版本(2.3.7,4.0.3,4.4.2,5.1.0)的不同手机,因为我认为Android 4.2中蓝牙堆栈的变化是个问题。似乎没有任何东西让设备连接到我的手机。我还可以在设备尝试连接时从我的Nexus 5中提供一个hci转储,但我无法确定问题。

任何帮助,将不胜感激。

编辑:

这是用于验证Android的蓝牙服务器的Python代码:

import sys 
import bluetooth 

uuid = "1234" 
service_matches = bluetooth.find_service(uuid = uuid) 

if len(service_matches) == 0: 
    print "couldn't find the service" 
    sys.exit(0) 

first_match = service_matches[0] 
port = first_match["port"] 
name = first_match["name"] 
host = first_match["host"] 

print "connecting to \"%s\" on %s" % (name, host) 

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
sock.connect((host, port)) 
sock.send("Test") 
sock.close() 

不用说,它的工作没有任何问题。

+0

请张贴您的客户代码吗? – 7383

回答

0

由于别人遇到这种类型的问题,事实证明蓝牙客户端设备只连接到服务器,如果它有一定的bluetooth device class,这显然不是智能手机。