Android
Multi Bluetooth
Library

View the Project on GitHub arissa34/Android-Multi-Bluetooth-Library

Introduction

This library allows you to easily create a socket bluetooth connection for multiple android devices with one server and 7 clients max. This library is compatible with the Android SDK 2.3 to 5.1.

This library is based on my Android Bluetooth Library

How it works

For a connection between the server and the client, we need a unique UUID.
This library will scan all devices around you and create an server thread with an unique UUID to allow the client a connection.

After finding a device, this library create the UUID following this code :

UUID.fromString("e0917680-d427-11e4-8830-" + mAddress.replace(":", ""));

To make it work an simple implementation with extended BluetoothActivity or FragmentActivity.

public class MainActivity extends BluetoothActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public int myNbrClientMax() {
        return 7;
    }

    @Override
    public void onBluetoothStartDiscovery() {
    }

    @Override
    public void onBluetoothDeviceFound(BluetoothDevice device) {
    }

    @Override
    public void onClientConnectionSuccess() {
    }

    @Override
    public void onClientConnectionFail() {
    }

    @Override
    public void onServeurConnectionSuccess() {
    }

    @Override
    public void onServeurConnectionFail() {
    }

    @Override
    public void onBluetoothCommunicator(String messageReceive) {
    }

    @Override
    public void onBluetoothNotAviable() {
    }
}

You can also extend your fragment with BluetoothFragment. Don't forget to past the activity result from your activity to your fragment.

AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

Sample

To start the server you must start the bluetooth discovery with a long time and call this method :

setTimeDiscoverable(BluetoothManager.BLUETOOTH_TIME_DICOVERY_3600_SEC);
selectServerMode();

For the client you can use small time for your bluetooth discovery and call this method :

setTimeDiscoverable(BluetoothManager.BLUETOOTH_TIME_DICOVERY_120_SEC);
selectClientMode();

For the server to register all clients around just call this method :

scanAllBluetoothDevice();

To connect the client to your server call this method :

createClient(mServerAddressMac);

To Send a message call this method :

sendMessage("Hello");

And if you want to disconnect :

disconnectServer();

or

disconnectClient();

Bonus

If you need more than seven devices connected in your server. Every clients can be also a server in the same time. So you can try to start a server from your client and connect another client on your new server.

Download

Android Multi Bluetooth Library V1.1 JAR

The source code to the library and sample application as well as this website is available on GitHub.

MAVEN

If you are using Maven for compilation you can declare the library as a dependency.

 <dependencies>
    <dependency>
      <groupId>com.ramimartin.multibluetooth</groupId>
      <artifactId>AndroidMultiBluetoothLibrary</artifactId>
      <version>1.1-SNAPSHOT</version>
    </dependency>
  </dependencies>

  <repositories>
    <repository>
      <id>sfdn-ftp</id>
      <name>Sfdn Ftp</name>
      <url>http://sfdn.ddns.net/maven2</url>
    </repository>
  </repositories>

GRADLE

repositories {
    maven {
        url "http://sfdn.ddns.net/maven2/"
    }
    mavenCentral()
}

dependencies {
    compile 'com.ramimartin.multibluetooth:AndroidMultiBluetoothLibrary:1.1-SNAPSHOT'
}

License

/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Rami Martin wrote this file.  As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
* ----------------------------------------------------------------------------
*/

Help me to improve this library by offering me one 🍺 or more 🍺 🍺 🍺

1QBj3o5kAyXnn86DZsNsZ1u2kk1i9nfWpA

Support or Contact

If you have any questions please feel free to contact me at this email address : rami.34000@gmail.com