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 8.0.

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-" + mMacAddress.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 String setUUIDappIdentifier() {
        return "e0917680-d427-11e4-8830";
    }
    
    @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 onBluetoothMsgStringReceived(String messageReceive) {
    }

    @Override
    public void onBluetoothMsgObjectReceived(Object o) {
    }

    @Override
    public void onBluetoothMsgBytesReceived(byte[] bytes) {
    }

    @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"/>
<!-- For android sdk >= 6.0 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCTION"/>
<!-- For android sdk >= 6.0 -->

For Android 6.0 and upper you will need more permissions to scan bluetooth devices


private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

if (Build.VERSION.SDK_INT >= 23) {
   int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
   permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
   if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
      requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
   }
}
        
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
   switch (requestCode) {
      case PERMISSION_REQUEST_COARSE_LOCATION: {
         // TODO stuff if you need
      }
   }
}

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);

You can now send three types of messages

To send bytes array message :

sendMessageBytesForAll("Hello".getBytes());

To send a String message :

sendMessageStringToAll("Hello");

To send an Serielizable Object :

sendMessageObjectToAll(mObject);

Don't forget to set which message mode you want to use :

setMessageMode(BluetoothManager.MessageMode.String);

Or

setMessageMode(BluetoothManager.MessageMode.Serialized);

Or

setMessageMode(BluetoothManager.MessageMode.Bytes);

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 V2.0.4 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>2.0.4-SNAPSHOT</version>
    </dependency>
  </dependencies>

  <repositories>
    <repository>
      <id>drgames</id>
      <name>Drgames Ftp</name>
      <url>http://drgames.fr/maven2</url>
    </repository>
  </repositories>

GRADLE

repositories {
    maven {
        url "http://drgames.fr/maven2/"
    }
    mavenCentral()
}

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

This library is used in these applications :

If you like this library please download and give me 5 stars ;)

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
* ----------------------------------------------------------------------------
*/

Support or Contact

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