BtLibrary
0.97
Bluetooth Classic Library for Unity
|
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
1) We need a BluetoothDevice instance which will represent the remote device.
2) Identify the device either by its mac address or by its name, so the BtLibrary can find it and connect to it:
or
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).
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
5) To have multiple connections, just repeate the same process for another BluetoothDevice instance that represents a different device.
The following two methods generate a list of BluetoothDevice instances, that you can directly connect to by calling connect() :
This method shows the default Android Bluetooth devices list. Subscribe to BluetoothAdapter.OnDevicePicked Event in order to get a reference of the picked device.
This method returns a BluetoothDevice[]
array of the paired devices on your Android.
Name
and Mac Address
using BluetoothDevice.Name and BluetoothDevice.Name }
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
Lets create an IEnumerator that will be started when a data receiving channel has just been istablished.
The easiest way to start this IEnumerator is by assigning it to the BluetoothDevice.ReadingCoroutine property.
Also You can use the Event
BluetoothAdapter.OnReadingStarted.
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.
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.