简体   繁体   中英

Android not discovering Bluez service

I don't know what can be happening. I have a bluetooth application developed on my laptop using BlueZ. I have managed to create a client that connect to that server using Bluez, but I'm unable to do so with 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

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. I have tried with several on my android app.

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

The outcome is I get an exception just when calling 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.

TA.

Solution is found in this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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