BtLibrary  0.97
Bluetooth Classic Library for Unity
Manual

The BtLibrary

Android Bluetooth Classic API for Unity ... for any questions (email: techtweaking@gmail.com)

Note : If you would like to use PlayMaker instead of this API, email me!. A beta version is available

Connecting by Name or Mac Address

1) We need a BluetoothDevice instance which will represent the remote device.

BluetoothDevice device = new BluetoothDevice();

2) Identify the device either by its mac address or by its name, so the BtLibrary can find it and connect to it:

device.Name = "HC-05";

or

device.MacAddress = "00:11:22:AA:BB:CC"

3) If you want to connect Android to Android, you need to specify the UUID that is being used. (UUID : it is a 128-bit number used to uniquely identify your application's Bluetooth service, there're plenty of random UUID generators on the internet).

device.UUID = "f4144060-a8d0-11e5-b3ba-0002a5d5c51b"

For HC-05 or similar SPP devices, the UUID is set by default, so there's no need to specify it.

4) Now connect to the device

device.connect()

5) To have multiple connections, just repeate the same process for another BluetoothDevice instance that represents a different device.


Connecting using an available BluetoothDevice reference

The following two methods generate a list of BluetoothDevice instances, that you can directly connect to by calling connect() :

BluetoothAdapter.showDevices()

This method shows the default Android Bluetooth devices list. Subscribe to BluetoothAdapter.OnDevicePicked Event in order to get a reference of the picked device.

//The following code tries to connect to any device picked by user.
...
BluetoothAdapter.OnDevicePicked += HandleOnDevicePicked;
...
//Called when device is Picked by user
void HandleOnDevicePicked (BluetoothDevice device)
{
device.UUID = MY_UUID;
device.connect();
}

BluetoothAdapter.getPairedDevices()

This method returns a BluetoothDevice[] array of the paired devices on your Android.

Remarks
{ Get the Name and Mac Address using BluetoothDevice.Name and BluetoothDevice.Name }


Generally any returned BluetoothDevice reference by this library is ready for connection



Connecting as a server

You can make a device act as a server that listen for incoming connection requests, by calling one of those methods : BluetoothAdapter.startServer(UUID), and then listen to the incoming connection requests by listening to the event BluetoothAdapter.OnClientRequest

BluetoothAdapter.OnClientRequest += HandleOnClientRequest;
...
BluetoothAdapter.startServer(MY_UUID);
...
void HandleOnClientRequest (BluetoothDevice device)
{
device.connect();//< will connect to any Bluetooth device tries to initiate a connection with a UUID=MY_UUID.
}

Reading Data from a remote device

Lets create an IEnumerator that will be started when a data receiving channel has just been istablished.

IEnumerator ManageConnection (BluetoothDevice device)
{
while (device.IsReading) {//Reading loop
if (device.IsDataAvailable) {//check if there's data to read
byte [] msg = device.read (10);//read up to 10 bytes or less
if (msg.Length > 0 ) {
Debug.Log (System.Text.ASCIIEncoding.ASCII.GetString (msg));//convert bytes to string
}
}
yield return null;
}
}

The easiest way to start this IEnumerator is by assigning it to the BluetoothDevice.ReadingCoroutine property.

device.ReadingCoroutine = ManageConnection;

Also You can use the Event BluetoothAdapter.OnReadingStarted.



Devices Discovery

If you want to inquiry nearby devices, check out startDiscovery() and OnDeviceDiscovered.

startDiscovery() will start a discovery process of about 12 seconds, while passing any discovered device to the Event OnDeviceDiscovered. It also passes the RSSI value of each device.



Optemizing the underlying threads/processes using the property BluetoothDevice.ThreadID (Only for Multiple Connections at the same time)

The library aims to be asynchronous and non-blocking, so no method call can affect your game loop flow. There are three operations that do extensive work without blocking which are sending data, calling a connect method and reading data by keep listening to the remote device.

Istablising connection and sending data is optemized and won't block, see BluetoothDevice.send() and BluetoothDevice.connect()

On the other hand, reading data and listening to the remote device require a process/thread that will be running in the background, and if the device is listening to multiple remote devices at the same time, then having multiple threads/processes in the background is not always the most efficient way. You can make multiple devices share the same process/thread using the property BluetoothDevice.ThreadID by assigning the same non-zero int value to it for each device. Simply if BluetoothDevice.ThreadID equals to zero _ which is the default value_, the device will have its own single thread that isn't and won't be shared with others.