public void BtGattDisconnect() { try { if (_gattCharacteristicSppRead != null) { try { _bluetoothGatt?.SetCharacteristicNotification(_gattCharacteristicSppRead, false); } catch (Exception) { // ignored } _gattCharacteristicSppRead = null; } _gattCharacteristicSppWrite = null; _gattConnectionState = State.Disconnected; _gattServicesDiscovered = false; if (_bluetoothGatt != null) { try { _bluetoothGatt.Disconnect(); } catch (Exception) { // ignored } _bluetoothGatt.Dispose(); _bluetoothGatt = null; } if (_btGattSppInStream != null) { _btGattSppInStream.Dispose(); _btGattSppInStream = null; } if (_btGattSppOutStream != null) { _btGattSppOutStream.Dispose(); _btGattSppOutStream = null; } } catch (Exception) { // ignored } }
public bool ConnectLeGattDevice(Context context, BluetoothDevice device) { try { BtGattDisconnect(); _gattConnectionState = State.Connecting; _gattServicesDiscovered = false; _btGattSppInStream = new MemoryQueueBufferStream(true); _btGattSppOutStream = new BGattOutputStream(this); _bluetoothGatt = device.ConnectGatt(context, false, new BGattCallback(this)); if (_bluetoothGatt == null) { LogString("*** ConnectGatt failed"); return(false); } _btGattConnectEvent.WaitOne(2000, false); if (_gattConnectionState != State.Connected) { LogString("*** GATT connection timeout"); return(false); } _btGattDiscoveredEvent.WaitOne(2000, false); if (!_gattServicesDiscovered) { LogString("*** GATT service discovery timeout"); return(false); } IList <BluetoothGattService> services = _bluetoothGatt.Services; if (services == null) { LogString("*** No GATT services found"); return(false); } #if DEBUG foreach (BluetoothGattService gattService in services) { if (gattService.Uuid == null || gattService.Characteristics == null) { continue; } Android.Util.Log.Info(Tag, string.Format("GATT service: {0}", gattService.Uuid)); foreach (BluetoothGattCharacteristic gattCharacteristic in gattService.Characteristics) { if (gattCharacteristic.Uuid == null) { continue; } Android.Util.Log.Info(Tag, string.Format("GATT characteristic: {0}", gattCharacteristic.Uuid)); Android.Util.Log.Info(Tag, string.Format("GATT properties: {0}", gattCharacteristic.Properties)); } } #endif _gattCharacteristicSppRead = null; _gattCharacteristicSppWrite = null; _gattCharacteristicUuidSppRead = null; _gattCharacteristicUuidSppWrite = null; BluetoothGattService gattServiceSpp = _bluetoothGatt.GetService(GattServiceCarlySpp); BluetoothGattCharacteristic gattCharacteristicSpp = gattServiceSpp?.GetCharacteristic(GattCharacteristicCarlySpp); if (gattCharacteristicSpp != null) { if ((gattCharacteristicSpp.Properties & (GattProperty.Read | GattProperty.Write | GattProperty.Notify)) == (GattProperty.Read | GattProperty.Write | GattProperty.Notify)) { _gattCharacteristicSppRead = gattCharacteristicSpp; _gattCharacteristicSppWrite = gattCharacteristicSpp; _gattCharacteristicUuidSppRead = GattCharacteristicCarlySpp; _gattCharacteristicUuidSppWrite = GattCharacteristicCarlySpp; #if DEBUG Android.Util.Log.Info(Tag, "SPP characteristic Carly found"); #endif } } else { gattServiceSpp = _bluetoothGatt.GetService(GattServiceWgSoftSpp); BluetoothGattCharacteristic gattCharacteristicSppRead = gattServiceSpp?.GetCharacteristic(GattCharacteristicWgSoftSppRead); BluetoothGattCharacteristic gattCharacteristicSppWrite = gattServiceSpp?.GetCharacteristic(GattCharacteristicWgSoftSppWrite); if (gattCharacteristicSppRead != null && gattCharacteristicSppWrite != null) { if (((gattCharacteristicSppRead.Properties & (GattProperty.Read | GattProperty.Notify)) == (GattProperty.Read | GattProperty.Notify)) && ((gattCharacteristicSppWrite.Properties & (GattProperty.Write)) == (GattProperty.Write))) { _gattCharacteristicSppRead = gattCharacteristicSppRead; _gattCharacteristicSppWrite = gattCharacteristicSppWrite; _gattCharacteristicUuidSppRead = GattCharacteristicWgSoftSppRead; _gattCharacteristicUuidSppWrite = GattCharacteristicWgSoftSppWrite; } #if DEBUG Android.Util.Log.Info(Tag, "SPP characteristic WgSoft found"); #endif } } if (_gattCharacteristicSppRead == null || _gattCharacteristicSppWrite == null) { LogString("*** No GATT SPP characteristic found"); return(false); } if (!_bluetoothGatt.SetCharacteristicNotification(_gattCharacteristicSppRead, true)) { LogString("*** GATT SPP enable notification failed"); return(false); } BluetoothGattDescriptor descriptor = _gattCharacteristicSppRead.GetDescriptor(GattCharacteristicConfig); if (descriptor == null) { LogString("*** GATT SPP config descriptor not found"); return(false); } if (BluetoothGattDescriptor.EnableNotificationValue == null) { LogString("*** GATT SPP EnableNotificationValue not present"); return(false); } _gattWriteStatus = GattStatus.Failure; descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray()); if (!_bluetoothGatt.WriteDescriptor(descriptor)) { LogString("*** GATT SPP write config descriptor failed"); return(false); } if (!_btGattWriteEvent.WaitOne(2000)) { LogString("*** GATT SPP write config descriptor timeout"); return(false); } if (_gattWriteStatus != GattStatus.Success) { LogString("*** GATT SPP write config descriptor status failure"); return(false); } #if false byte[] sendData = Encoding.UTF8.GetBytes("ATI\r"); _btGattSppOutStream.Write(sendData, 0, sendData.Length); while (_btGattReceivedEvent.WaitOne(2000, false)) { #if DEBUG Android.Util.Log.Info(Tag, "GATT SPP data received"); #endif } while (_btGattSppInStream.HasData()) { int data = _btGattSppInStream.ReadByteAsync(); if (data < 0) { break; } #if DEBUG Android.Util.Log.Info(Tag, string.Format("GATT SPP byte: {0:X02}", data)); #endif } #endif return(true); } catch (Exception) { _gattConnectionState = State.Disconnected; _gattServicesDiscovered = false; return(false); } }