简体   繁体   English

Android未发现Bluez服务

[英]Android not discovering Bluez service

I don't know what can be happening. 我不知道会发生什么。 I have a bluetooth application developed on my laptop using BlueZ. 我的笔记本电脑上使用BlueZ开发了一个蓝牙应用程序。 I have managed to create a client that connect to that server using Bluez, but I'm unable to do so with Android. 我已经设法创建了一个使用Bluez连接到该服务器的客户端,但是使用Android无法做到这一点。

It seems that the phone doesn't find the device/port/service: 手机似乎找不到设备/端口/服务:

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

Find below parts of the code both Bluez server and Android: 查找Bluez服务器和Android的以下代码部分:

Bluez 配合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;
}

Running sdptool to check that the service is well registered I find out that the UUID is different from the one expected. 运行sdptool来检查服务是否注册正确,我发现UUID与预期的不同。 I have tried with several on my android app. 我在Android应用程序中尝试了几种。

Output of sdptool browse --tree local sdptool browse --tree local输出

UUID128 : 0x00000000-0000-0000-0000-00003332-0000

Android 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();
    }

The outcome is I get an exception just when calling connect() : 结果是仅在调用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

Any help is more than welcome. 任何帮助都超过了欢迎。 I think the problem is with the UUID that is not correctly define, but don't know. 我认为问题出在UUID定义不正确,但不知道。

TA. TA。

Solution is found in this link . 此链接中找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM