Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth Examples for Android

Does anybody know of any example available that illustrates Bluetooth development on Android.

I have read the tutorial here and I understand everything on that page.

However when it comes to implementing the Bluetooth code, into an application it is necessary to view the Bluetooth Chat example to understand how it all works.

Bluetooth Chat example here

This example is good, but it is also hard to comprehend because each device is initially set up to be a server.

Who is the server and do both devices send out server sockets until one device scans?

Once a device makes itself discoverable does it become the server?

When does the OnResume activity start, because once that starts and the mChatService has been initialized in SetupChat, the device will start an Accept thread.

Some code examples are given below, and link to the full Bluetooth chat is available above.

@Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");

    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(), so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
    if (mChatService != null) {
        // Only if the state is STATE_NONE, do we know that we haven't started already
        if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
          // Start the Bluetooth chat services
          mChatService.start();
        }
    }
}

private void setupChat() {

    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(this, mHandler);

    // Initialize the buffer for outgoing messages
    mOutStringBuffer = new StringBuffer("");
}


/**
 * Start the chat service. Specifically start AcceptThread to begin a
 * session in listening (server) mode. Called by the Activity onResume() */
public synchronized void start() {
    if (D) Log.d(TAG, "start");

    // Cancel any thread attempting to make a connection
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

    setState(STATE_LISTEN);

    // Start the thread to listen on a BluetoothServerSocket
    if (mSecureAcceptThread == null) {
        mSecureAcceptThread = new AcceptThread(true);
        mSecureAcceptThread.start();
    }
    if (mInsecureAcceptThread == null) {
        mInsecureAcceptThread = new AcceptThread(false);
        mInsecureAcceptThread.start();
    }
}

What I am asking for is for any examples of Bluetooth, that are easier to understand and examples that clearly segregate the server side and the client side of Bluetooth. I have Google'd this, and I have read all details available on the developer.android.com website.

like image 481
Navigatron Avatar asked May 26 '11 16:05

Navigatron


People also ask

What is Bluetooth example?

Below are some other examples of how Bluetooth is used. Bluetooth headphones - Headphones that connect to any Bluetooth device. Bluetooth keyboard and Bluetooth mouse - Wireless keyboards and mice. Bluetooth speaker - Speakers that connect to any Bluetooth audio device.

What is Bluetooth Android?

You can use Bluetooth to connect some devices to your phone without a cord. After you pair a Bluetooth device for the first time, your devices can pair automatically.


1 Answers

From what I have gathered, the distinction: server and client exists only while the Bluetooth connection is being established (ie during the discovery and pairing process). For the connection to be established, one device acts as a server (using an instance of BluetoothServerSocket class) and the other acts as a client (using an instance of the BluetoothSocket class). The (acting) server listens for incoming requests and the client requests listening servers to connect. After the connection is established (see the details of the methods used on the Android Dev Guide), both the (initially called) server and client interact using the BluetoothSocket object only. So no such server/client distinction exists.

You can check out the code of the Bluetooth Chat example on the Dev Guide, particularly of the BluetoothChatService class. Call to the method createRfcommSocketToServiceRecord() returns a BluetotohSocket to the listening(server) device. The requesting device(client) as it is uses a similar object.

True, further example codes would be nicer.

like image 124
Rachee Singh Avatar answered Sep 24 '22 05:09

Rachee Singh