コード例 #1
0
        public BluetoothDeviceWrapper(AndroidJavaObject bluetoothDeviceJavaObject, BluetoothController bluetoothController)
        {
            try
            {
                if (bluetoothDeviceJavaObject.IsNull())
                {
                    throw new ArgumentNullException(nameof(bluetoothDeviceJavaObject));
                }

                this.bluetoothDeviceJavaObject = bluetoothDeviceJavaObject;
                this.bluetoothController       = bluetoothController;

                Name    = bluetoothDeviceJavaObject.Call <string>("getName");
                Address = bluetoothDeviceJavaObject.Call <string>("getAddress");

                AndroidJavaObject deviceClassJavaObject =
                    bluetoothDeviceJavaObject.Call <AndroidJavaObject>("getBluetoothClass");
                int deviceClassFull = deviceClassJavaObject.Call <int>("getDeviceClass");

                DeviceSubType = (BluetoothDeviceSubType)deviceClassFull;
                DeviceType    = BluetoothHelpers.GetDeviceType(DeviceSubType);
                IsConnectable = BluetoothHelpers.IsProbablyDataCapable(DeviceType);

                Name = string.IsNullOrEmpty(Name) ? Address : Name.Trim();

                Debug.Log($"New BluetoothDevice: {this}");
            }
            catch
            {
                Debug.LogError("Exception while converting BluetoothDevice");
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BluetoothDevice"/> class from the Java BluetoothDevice.
        /// </summary>
        /// <param name="bluetoothDeviceJavaObject">
        /// The Java object that is an instance of android.bluetooth.BluetoothDevice.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="bluetoothDeviceJavaObject"/> is null.
        /// </exception>
        internal BluetoothDevice(AndroidJavaObject bluetoothDeviceJavaObject)
        {
            try {
                if (bluetoothDeviceJavaObject.IsNull())
                {
                    throw new ArgumentNullException("bluetoothDeviceJavaObject");
                }

                Name      = bluetoothDeviceJavaObject.Call <string>("getName").Trim();
                Address   = bluetoothDeviceJavaObject.Call <string>("getAddress");
                BondState = (DeviceBondState)bluetoothDeviceJavaObject.Call <int>("getBondState");

                AndroidJavaObject deviceClassJavaObject = bluetoothDeviceJavaObject.Call <AndroidJavaObject>("getBluetoothClass");
                int deviceClassFull = deviceClassJavaObject.Call <int>("getDeviceClass");
                DeviceClass      = (BluetoothDeviceClass.Class)deviceClassFull;
                DeviceMajorClass = DeviceClass.GetMajorClass();
                IsConnectable    = DeviceClass.IsProbablyHandheldDataCapableDevice();

                if (Name == string.Empty)
                {
                    Name = Address;
                }
            } catch {
                Debug.LogError("Exception while converting BluetoothDevice");
                throw;
            }
        }
コード例 #3
0
        /// <summary>
        /// Initializes <see cref="AndroidBluetoothMultiplayer"/> class.
        /// Retrieves a pointer to the Java plugin object.
        /// Initalizes the singleton instance on the first usage of the class.
        /// </summary>
        static AndroidBluetoothMultiplayer()
        {
            _plugin            = null;
            _isPluginAvailable = false;

            try {
                UpdateInstance();
            } catch {
                // Happens when this static constructor is called from a GameObject being created.
                // Just ignoring, as this is intended.
            }

#if !UNITY_EDITOR && UNITY_ANDROID
            // Retrieve BluetoothMediator singleton instance
            try {
                using (AndroidJavaClass mediatorClass = new AndroidJavaClass(kPluginClassName)) {
                    if (!mediatorClass.IsNull())
                    {
                        _plugin            = mediatorClass.CallStatic <AndroidJavaObject>("getSingleton");
                        _isPluginAvailable = !_plugin.IsNull();
                    }
                }
            } catch {
                Debug.LogError("AndroidBluetoothMultiplayer initialization failed. Probably .jar not present?");
                throw;
            }
#endif
        }
        /// <summary>
        /// Returns <see cref="BluetoothDevice"/> of the current device the application runs on.
        /// </summary>
        /// <returns><see cref="BluetoothDevice"/> if Bluetooth connectivity is available and enabled, null otherwise or on error.</returns>
        public static BluetoothDevice GetCurrentDevice()
        {
            if (!_isPluginAvailable)
            {
                return(null);
            }

            AndroidJavaObject bluetoothDeviceJavaObject = _plugin.Call <AndroidJavaObject>("getCurrentDevice");

            if (bluetoothDeviceJavaObject.IsNull())
            {
                return(null);
            }

            BluetoothDevice currentDevice = new BluetoothDevice(bluetoothDeviceJavaObject);

            return(currentDevice);
        }
        /// <summary>
        /// Returns <see cref="BluetoothDevice"/> of a device with address <paramref name="deviceAddress"/>.
        /// </summary>
        /// <returns><see cref="BluetoothDevice"/> if Bluetooth connectivity is available and enabled, null otherwise or on error.</returns>
        public static BluetoothDevice GetDeviceFromAddress(string deviceAddress)
        {
            if (!_isPluginAvailable)
            {
                return(null);
            }

            AndroidJavaObject bluetoothDeviceJavaObject = _plugin.Call <AndroidJavaObject>("getBluetoothDeviceFromAddress", deviceAddress);

            if (bluetoothDeviceJavaObject.IsNull())
            {
                return(null);
            }

            BluetoothDevice bluetoothDevice = new BluetoothDevice(bluetoothDeviceJavaObject);

            return(bluetoothDevice);
        }
        /// <summary>
        /// Converts a Java Set of android.Bluetooth.BluetoothDevice into its C# representation.
        /// </summary>
        /// <param name="bluetoothDeviceJavaSet">The Java Set of android.Bluetooth.BluetoothDevice.</param>
        /// <returns>The converted <see cref="BluetoothDevice[]"/>.</returns>
        private static BluetoothDevice[] ConvertJavaBluetoothDeviceSet(AndroidJavaObject bluetoothDeviceJavaSet)
        {
            try {
                if (bluetoothDeviceJavaSet.IsNull())
                {
                    return(null);
                }

                AndroidJavaObject[] deviceJavaArray = bluetoothDeviceJavaSet.Call <AndroidJavaObject[]>("toArray");
                BluetoothDevice[]   deviceArray     = new BluetoothDevice[deviceJavaArray.Length];
                for (int i = 0; i < deviceJavaArray.Length; i++)
                {
                    deviceArray[i] = new BluetoothDevice(deviceJavaArray[i]);
                }

                return(deviceArray);
            } catch {
                Debug.LogError("Exception while converting BluetoothDevice Set");
                throw;
            }
        }
        /// <summary>
        /// Initializes <see cref="AndroidBluetoothMultiplayer"/> class.
        /// Retrieves a pointer to the Java plugin object.
        /// </summary>
        static AndroidBluetoothMultiplayer()
        {
            _plugin            = null;
            _isPluginAvailable = false;

#if !UNITY_EDITOR && UNITY_ANDROID
            // Retrieve BluetoothMediator singleton instance
            try {
                using (AndroidJavaClass mediatorClass = new AndroidJavaClass(kPluginClassName)) {
                    if (!mediatorClass.IsNull())
                    {
                        _plugin            = mediatorClass.CallStatic <AndroidJavaObject>("getSingleton");
                        _isPluginAvailable = !_plugin.IsNull();
                    }
                }
            } catch {
                Debug.LogError("AndroidBluetoothMultiplayer initialization failed. Probably .aar not present?");
                throw;
            }
#endif
        }