/// <summary> /// Enable or disable notifications/indications for a given characteristic. /// /// Once notifications are enabled for a characteristic, a BluetoothGattCallback.onCharacteristicChanged(BluetoothGatt, BluetoothGattCharacteristic) callback will be triggered if the remote device indicates that the given characteristic has changed. /// </summary> /// <param name="characteristic">The characteristic for which to enable notifications</param> /// <param name="enable">Set to true to enable notifications/indications</param> /// <returns>true, if the requested notification status was set successfully </returns> /// <seealso href="https://developer.android.com/reference/android/bluetooth/BluetoothGatt#setCharacteristicNotification(android.bluetooth.BluetoothGattCharacteristic,%20boolean)"/> public bool SetCharacteristicNotification(GattCharacteristic characteristic, bool enable) { Assert.IsNotNull(AndroidObject); Assert.IsNotNull(characteristic.AndroidObject); var BluetoothGattDescriptorClass = new AndroidJavaClass("android.bluetooth.BluetoothGattDescriptor"); byte[] notificationValue; if (enable) { notificationValue = BluetoothGattDescriptorClass.GetStatic <byte[]>("ENABLE_NOTIFICATION_VALUE"); } else { notificationValue = BluetoothGattDescriptorClass.GetStatic <byte[]>("DISABLE_NOTIFICATION_VALUE"); } if (AndroidObject.Call <bool>("setCharacteristicNotification", characteristic.AndroidObject, enable)) { /* BluetoothGattDescriptor */ var descriptor = characteristic.AndroidObject.Call <AndroidJavaObject>("getDescriptor", GattCharacteristic.CLIENT_CHARACTERISTIC_CONFIGURATION.ToJavaObject()); if (descriptor.Call <bool>("setValue", notificationValue)) { Debug.Log("Permissions: " + descriptor.Call <int>("getPermissions")); AddRequest(() => { if (!AndroidObject.Call <bool>("writeDescriptor", descriptor)) { PerformNext(); } }); } } return(false); }
/// <summary> /// Writes a given characteristic and its values to the associated remote device. /// </summary> /// <seealso href="https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#writeCharacteristic(android.bluetooth.BluetoothGattCharacteristic)"> public void WriteCharacteristic(GattCharacteristic characteristic) { AddRequest(() => { if (!AndroidObject.Call <bool>("writeCharacteristic", characteristic.AndroidObject)) { PerformNext(); } }); }
public GattService FindService(Uuid uuid) { string uuidStr = uuid.ToString(); if (m_services.ContainsKey(uuidStr)) { return(m_services[uuidStr]); } else { Assert.IsNotNull(AndroidObject); /* BluetoothGattService */ AndroidJavaObject service = AndroidObject.Call <AndroidJavaObject>("getService", uuid.ToJavaObject()); if (service == null) { return(null); } var gattService = new GattService(service, this); m_services.Add(uuidStr, gattService); return(gattService); } }
public void Disable() { AndroidObject.Call <bool>("disable"); }
public void Enable() { AndroidObject.Call <bool>("enable"); }
public bool IsEnabled() { return(AndroidObject.Call <bool>("isEnabled")); }
public void StopLeScan(AndroidJavaObject callback) { AndroidObject.Call("stopLeScan", callback); }
public void AttemptEnablingBluetooth() { AndroidObject.Call("attemptEnablingBluetooth"); }
public AndroidJavaObject GetSystemAdapter() { return(AndroidObject.Call <AndroidJavaObject>("getSystemAdapter")); }
public void Close() { Assert.IsNotNull(AndroidObject); AndroidObject.Call("close"); }
public void DiscoverServices() { Assert.IsNotNull(AndroidObject); AndroidObject.Call <bool>("discoverServices"); }