Android没有发现Bluez服务

时间:2012-05-14 17:08:48

标签: android bluetooth bluez

我不知道会发生什么。我在笔记本电脑上使用BlueZ开发了蓝牙应用程序。我已经设法创建了一个使用Bluez连接到该服务器的客户端,但我无法使用Android。

手机似乎找不到设备/端口/服务:

05-14 18:54:40.395: D/BluetoothService(134): Cleaning up failed UUID channel lookup:   00:02:72:00:D4:29 00000000-0000-0000-0000-000033320000  
05-14 18:54:40.395: W/System.err(4895): java.io.IOException: Service discovery failed

查找Bluez服务器和Android代码的以下部分:

配合bluez

sdp_session_t *register_service() {
     uint32_t service_uuid_int[] = { 0, 0, 0, 0x3233 };
     .... code taken from http://people.csail.mit.edu/albert/bluez-intro/x604.html
}

int main() 
{
    sdp_session_t *session = register_service();

    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
    char buf[1024] = { 0 };
    int s, client, bytes_read;
    socklen_t opt = sizeof(rem_addr);

    char src[18] = "XX:XX:XX:XX:XX:XX";

    // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // bind socket to port 1 of the first available 
    // local bluetooth adapter
    loc_addr.rc_family = AF_BLUETOOTH;
    str2ba(src, &loc_addr.rc_bdaddr);
    //loc_addr.rc_bdaddr = *BDADDR_ANY;
    loc_addr.rc_channel = (uint8_t) 11;
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

    // put socket into listening mode
    listen(s, 1);

    // accept one connection
    client = accept(s, (struct sockaddr *)&rem_addr, &opt);
    ba2str( &rem_addr.rc_bdaddr, buf );
    fprintf(stderr, "accepted connection from %s\n", buf);
    memset(buf, 0, sizeof(buf));

    // read data from the client
    bytes_read = read(client, buf, sizeof(buf));
    if( bytes_read > 0 ) {
        printf("received [%s]\n", buf);
    }

    // close connection
    close(client);
    close(s);
    return 0;
}

运行sdptool检查服务是否已正确注册我发现UUID与预期的UUID不同。我在我的Android应用程序上尝试了几个。

sdptool browse --tree local

的输出
UUID128 : 0x00000000-0000-0000-0000-00003332-0000

的Android

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BluetoothAdapter mBluetoothAdapter =
            BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
        Log.d(TAG, "No bluetooth");
    }

    Log.d(TAG, "Generated bluetooth adapter");

    MY_UUID = UUID.fromString("00000000-0000-0000-0000-000033320000");

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
            device = mBluetoothAdapter.getRemoteDevice(remote_addr);
        socket =
                device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
        mBluetoothAdapter.cancelDiscovery();            
        socket.connect();

                OutputStream tmpOut = null;
                    tmpOut = socket.getOutputStream();

                    String str = "Helloo";
                tmpOut.write(str.getBytes());

                    socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

结果是我在调用connect()时遇到异常:

05-14 18:54:40.395: D/BluetoothService(134): Cleaning up failed UUID channel lookup: XX:XX:XX:XX:XX:XX 00000000-0000-0000-0000-000033320000
05-14 18:54:40.395: W/System.err(4895): java.io.IOException: Service discovery failed

任何帮助都非常受欢迎。我认为问题在于未正确定义的UUID,但不知道。

TA

1 个答案:

答案 0 :(得分:1)

解决方案可在this link中找到。