Here's some basic code snippets.

Server code

    UUID uuid = uuid.fromString("27648B4D-D854-5674-FA60E4F535E44AF7");  //
generate your own UUID at http://www.uuidgenerator.com
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothServerSocket serverSocket =
adapter.listenUsingRfcommWithServiceRecord("MyBluetoothApp", uuid);
    BluetoothSocket socket = serverSocket.accept();  // blocks until a
connection is accepted
    serverSocket.close();  // close the listening socket - does not close
the connected client socket

Client code

    UUID uuid = uuid.fromString("27648B4D-D854-5674-FA60E4F535E44AF7");  //
UUID of server socket
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice device = adapter.getRemoteDevice("00:11:22:33:44:55");
 // BT MAC address of server
    BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
    adapter.cancelDiscovery();
    adapter.connect();  // blocks until connection established

At this point both sides have a connected BluetoothSocket that you can go
crazy with.

    InputStream in = socket.getInputStream();
    OutputStream out = socket.getOutputStream();
    out.write(...);
    in.read(...);
    ...
    in.close();
    out.close();
    socket.close();


Nick


On Mon, Nov 23, 2009 at 10:18 PM, AGA <[email protected]> wrote:

> How can a bluetooth client connect to a bluetooth server?
> As we know, in tcp mode, the client fire the connection with a given
> pair of IP address and port. From the server side a listening thread/
> process waits for the connection request and setup a data connection.
> But how it works in bluetooth wireless network? There's only a
> bluetooth Mac address, but how about the port number? I've noticed
> there's a UUID parameter in the method
> BluetoothAdapter.listenUsingRfcommWithServiceRecord but how it works?
> Or does anybody have a bluetooth connection establishment work flow
> chart?
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to