Skip to content

Example bluetooth chat app using Bluetooth LE library to communicate between android devices

License

Notifications You must be signed in to change notification settings

niedev/BluetoothCommunicatorExample

Repository files navigation

BluetoothCommunicator example

This repository contains an open source sample application of the BluetoothCommunicator library.
The library, using Bluetooth Low Energy, allows you to communicate in P2P mode between two or more android devices in a very simple way.

The application is a Bluetooth chat, when the app is open in the first screen (search) it discovers nearby devices with the app open in the same search screen and shows them in a list containing the code number name of the phones (the name is customizable in the library, but not in the app).

When the user clicks on one of the names the app sends a connection request to that phone and if the latter accepts it, both apps start the chat screen where the two devices can send text messages (the library can send also raw data) to each other.

When one of the user press the back button the connection stops and the apps will return to the search screen.

If you want to see the demo app in action you can download it from here.

BluetoothCommunicator library

BluetoothCommunicator is a library originally created for RTranslator but can be used in any more generic case where a P2P communication system is needed between two or more android devices (approximately up to 4 with a direct connection between all devices, even more with a star structure), for an example app see this repository or RTranslator.

Features

BluetoothCommunicator automatically implements (they are active by default):

  • reconnection in case of temporary connection loss.

  • reliable message sending:

    • splitting and rebuilding of long messages.

    • sending messages with a queue in order to always send the messages even in case of connection problems (they will be sent as soon as the connection is restored) and in the right order.

Tutorial

For use the library in a project you have to add jitpack.io to your root build.gradle (project):

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Then add the last version of BluetoothCommunicator to your app build.gradle

dependencies {
        implementation 'com.github.niedev:BluetoothCommunicator:1.0.6'
}

To use this library add these permissions to your manifest:

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

If you need to use bluetooth advertising or search in background you need to add also the following permission:

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

Then add android:largeHeap="true" to the application tag in the manifest:
Example

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

<application
    android:name="com.bluetooth.communicatorexample.Global"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/Theme.Speech">
    <activity android:name="com.bluetooth.communicatorexample.MainActivity"
        android:configChanges="orientation|screenSize">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

After the installation of the library and the changes to the manifest is time to write the code: create a bluetooth communicator object, it is the object that handles all operations of bluetooth low energy library, if you want to manage the bluetooth connections in multiple activities I suggest you to save this object as an attribute of a custom class that extends Application and create a getter so you can access to bluetoothCommunicator from any activity or service with:

((custom class name) getApplication()).getBluetoothCommunicator();

Next step is to initialize bluetoothCommunicator, the parameters are: a context, the name by which the other devices will see us (limited to 18 characters and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited) and the strategy (for now the only supported strategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)

bluetoothCommunicator = new BluetoothCommunicator(this, "device name", BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION);

Then add the bluetooth communicator callback, the callback will listen for all events of bluetooth communicator: