using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Unity Bluetooth (Android)
/// https://github.com/JCxYIS/UnityBluetooth
///
public class BluetoothManager
{
private static AndroidJavaClass _javaClass;
private static AndroidJavaObject _javaInstance = null;
/* -------------------------------------------------------------------------- */
[System.Serializable]
public struct BluetoothDevice
{
public string name;
public string mac;
}
/* -------------------------------------------------------------------------- */
private static void EnsureInstance()
{
if (Application.platform != RuntimePlatform.Android)
{
throw new System.NotSupportedException("BluetoothManager currently only works on Android.");
}
if (_javaInstance == null)
{
// context = activity.Call("getApplicationContext");
_javaClass = new AndroidJavaClass("com.bonus.bdxt.BluetoothManager");
//_javaClass = new AndroidJavaClass("im.shv.rtk.BluetoothManager");
_javaInstance = _javaClass.CallStatic("getInstance");
Debug.Log("[BluetoothManager] Inited.");
}
}
/* -------------------------------------------------------------------------- */
///
/// Init permission.
/// It is recommended to call this method in the first scene (before connect).
///
public static void CheckPermission()
{
EnsureInstance();
_javaInstance.Call("CheckPermission");
}
///
/// Start scanning nearby bluetooth devices
///
public static void StartDiscovery()
{
EnsureInstance();
_javaInstance.Call("StartDiscovery");
}
///
/// Get the list of nearby devices.
/// Don't forget to call StartDiscovery() first!
///
/// A string list. String form: "{Name}|{MAC}"
public static List GetAvailableDevices()
{
EnsureInstance();
string[] devices = _javaInstance.Call("GetAvailableDevices");
List deviceList = new List();
string[] deviceRawStr;
for (int i = 0; i < devices.Length; i++)
{
deviceRawStr = devices[i].Split('|');
deviceList.Add(new BluetoothDevice
{
name = deviceRawStr[0],
mac = deviceRawStr[1]
});
}
return deviceList;
}
///
/// Connect to a device with the specified address.
///
/// Address
/// If the device hasn't been bonded and it has PIN, then this field is required.
///
public static bool Connect(string mac, string pin = "")
{
EnsureInstance();
return _javaInstance.Call("Connect", mac, pin);
}
///
/// Is Connected?
///
public static bool IsConnected()
{
EnsureInstance();
return _javaInstance.Call("IsConnected");
}
///
/// Get connected device, if no device is connected, return "|".
///
/// {NAME}|{MAC}
public static string GetConnectedDevice()
{
EnsureInstance();
return _javaInstance.Call("GetConnectedDevice");
}
///
/// Send data to the remote device.
/// You need to append newline (e.g. '\n' or '\r\n') by yourself.
///
///
///
public static bool Send(string data)
{
EnsureInstance();
return _javaInstance.Call("Send", data);
}
///
/// Input stream buffer size
///
/// Negative value means not connected or error
public static int Available()
{
return _javaInstance.Call("Available");
}
///
/// Read a line from input stream
///
///
public static string ReadLine()
{
EnsureInstance();
return _javaInstance.Call("ReadLine");
}
///
/// Stop the connection
///
public static void Stop()
{
EnsureInstance();
_javaInstance.Call("Stop");
}
/* -------------------------------------------------------------------------- */
///
/// Show a Toast (bottom message)
///
public static void Toast(string msg)
{
EnsureInstance();
_javaClass.CallStatic("Toast", msg);
}
/* -------------------------------------------------------------------------- */
public static string[] StrArrTest(int len)
{
EnsureInstance();
return _javaInstance.Call("StrArrTest", len);
}
}