Android Ultimate Plugin tutorial part 4 – Bluetooth

Hi Guys, i know you been thinking on how to use Bluetooth communication on our plugin and here’s the answer.

Permission needed on your Android Manifest file without this it will not work or it will produce an error.

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

let’s start.

First you need to have an instance on BluetoothPlugin

private BlueToothPlugin blueToothPlugin = BlueToothPlugin.GetInstance();

To stop Debug toast message from showing,  pass 0 on SetDebug method

blueToothPlugin.SetDebug(0);

Next you need to initialize the bluetooth plugin

blueToothPlugin.Init();

Then you will setup a listener for Bluetooth events, first is for connection callback events.

blueToothPlugin.SetConnectionCallbackListener(
       OnConnected
       ,OnConnecting
       ,OnNotConnected
       ,OnConnectionFailed
       ,OnConnectionLost
       ,OnConnectToDevice
);

and here’s the sample receiving methods for connection callback

private void OnConnected(string data){
}

private void OnConnecting(string data){
}

private void OnNotConnected(string data){
}

private void OnConnectionFailed(string data){
}

private void OnConnectionLost(string data){
}

private void OnConnectToDevice(string data){
     string deviceConnectedTo = data.ToString();
     string[] deviceInfo = deviceConnectedTo.Split('_');
     string deviceName = deviceInfo.GetValue(0).ToString();
     string macAddress = deviceInfo.GetValue(1).ToString();
}

Second  event listener is for receiving  data, sending data and logmessage events

blueToothPlugin.SetDataCallbackListener(OnRecievedMessage,OnSentMessage,OnLogMessage);

and here’s the sample method for receiving data callback events

private void OnSentMessage(string data){
   string  sentData = data.ToString();
   Debug.Log("[BlueToothEventListener] OnSentMessage " + sentData );
}

private void OnRecievedMessage(string data){
   string  recievedData = data.ToString();
   Debug.Log("[BlueToothEventListener] OnRecievedMessage" + sentData );
}

private void OnLogMessage(string data){
   Debug.Log("[ServerPanelController] OnLogMessage: "  + data);
}

before anything else you must make sure that the bluetooth is enable before starting the Bluetooth communication server. you can check if Bluetooth is enable by calling this method, if it’s true then it’s enable else not enable.

bool isEnable = blueToothPlugin.CheckBlueTooth();

if the Bluetooth is not enable you can call this method to ask user to enable Bluetooth on their android device

blueToothPlugin.NotifyUserToEnableBlueTooth();

or this to ensure that your Android device is discoverable on other android device

blueToothPlugin.EnsureDiscoverable();

and finally if Bluetooth is enable you can now call InitServer() method to start the Bluetooth server

blueToothPlugin.InitServer();

after starting the server you can now get the paired devices by doing this line of codes

// get the paired devices
string pairedDevices = blueToothPlugin.GetPairedDevices();

//checks if there's a paired device
if(!pairedDevices.Equals("",StringComparison.Ordinal)){
   string[] pairedDeviceSet = pairedDevices.Split(',');

   //gets the 1st paired device
   string firstDevice =  pairedDeviceSet.GetValue(0).ToString();
   string[] firstDeviceInfo = firstDevice.Split('_');

   string deviceName = firstDeviceInfo.GetValue(0).ToString();
   string deviceMacAddress = firstDeviceInfo.GetValue(1).ToString();
}

after getting the paired device information you can now connect by passing one of the paired device mac address and telling if you want a secure (true) or not secure connection (false)

blueToothPlugin.Connect(deviceMacAddress ,false);

after calling connect  and when you received the onConnected Event it’s means that you are successfully connected to another Android Device and you can now send and received data.

you can send data by calling  SendData() method and passing the string data that you want to send.

string dataToSend = "someDataHere";
blueToothPlugin.SendData(dataToSend );

Note: Every time so send a data you will received a OnDataSent event and every time you received a data you will received a OnDataRecieved event

if you want to disconnect from server just call DisConnect() method

blueToothPlugin.DisConnect();

Then if  you want to stop the server just call StopServer() method

blueToothPlugin.StopServer();

And that’s then end of this tutorial, I know it’s kinda long but i think you know now how to use Bluetooth communication using our Android Ultimate Plugin.

Note:

right now this Bluetooth plugin can only connect 2 Android Devices but don’t worry we will update this to Accommodate more than 2 connections.

Thanks for your time reading our tutorial , Happy Coding 🙂

if something goes wrong or you encounter bugs or errors or you don’t get it at all, don’t hesitate to email us at gigadrillgames@gmail.com or you can comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.