Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLE GATT getting disconnected sometimes after connection with exception 'GattCallback error: 133' in xamarin forms

I'm implementing BLE in Xamarin forms. I'm able to do all the BLE operation read, write and notification. But sometimes the device gets disconnected after connection. This is the exception I get

Plugin.BLE.Abstractions.Exceptions.DeviceConnectionException: 'GattCallback error: 133'

This is the code where I'm doing the connection

 private async Task ScanForDevices(ScanData scanData)
        {
            
            _adapter = CrossBluetoothLE.Current.Adapter;
            _adapter.ScanMode = ScanMode.LowLatency;
            Device.BeginInvokeOnMainThread(async () =>
            {
                _adapter.DeviceDiscovered += async (s, a) =>
            {
                NativeDeviceAdd = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device);
                PropertyInfo propInfo = NativeDeviceAdd.GetType().GetProperty("Name");
                BleDeviceName = (string)propInfo.GetValue(NativeDeviceAdd, null);
               
                string substr = scanData.blename;
                if (BleDeviceName == substr)
                {
                  
                     _device = a.Device;
                   
                   await _adapter.StopScanningForDevicesAsync();
                   await ConnectForDevice(_characteristicsBLE);

                }
            };
            });
            await _adapter.StartScanningForDevicesAsync();
        }


       
        private async Task ConnectForDevice(ICharacteristic characteristics)
        {
           
            await _adapter.ConnectToDeviceAsync(_device);
        }

These are the logs.

01-08 17:31:50.435 D/BluetoothManager(26670): getConnectionState()
01-08 17:31:50.435 D/BluetoothManager(26670): getConnectedDevices
01-08 17:31:50.439 D/BluetoothGatt(26670): connect() - device: 34:C9:F0:8D:F4:57, auto: false
01-08 17:31:50.439 D/BluetoothGatt(26670): registerApp()
01-08 17:31:50.439 D/BluetoothGatt(26670): registerApp() - UUID=84c84faa-46ff-4e02-9f9a-f8d14fed8ed0
01-08 17:31:50.441 D/BluetoothGatt(26670): onClientRegistered() - status=0 clientIf=13
01-08 17:31:51.587 D/BluetoothGatt(26670): onClientConnectionState() - status=133 clientIf=13 device=34:C9:F0:8D:F4:57
01-08 17:31:51.594 D/BluetoothGatt(26670): onClientConnectionState() - status=133 clientIf=12 device=34:C9:F0:8D:F4:57
Thread started:  #10
Thread started:  #11
01-08 17:31:51.680 D/BluetoothGatt(26670): close()
01-08 17:31:51.680 D/BluetoothGatt(26670): close()
01-08 17:31:51.680 D/BluetoothGatt(26670): unregisterApp() - mClientIf=12
01-08 17:31:51.680 D/BluetoothGatt(26670): unregisterApp() - mClientIf=13
**Plugin.BLE.Abstractions.Exceptions.DeviceConnectionException:** 'GattCallback error: 133'

I'm not getting any solution for this from Xamarin side. Any suggestion?

like image 238
Judson Abraham Avatar asked Jan 08 '21 13:01

Judson Abraham


1 Answers

Unfortunately error 133 is relatively common and is usually related to the hardware/stack (especially if this is happening on Android). It has no clearly defined fix yet (as far as I know) but there have been mentions of a few workarounds, including attempting to silently reconnect or attempting to create a bond. Have a look at the link below and search for "133" for more info on the workaround:-

  • Ultimate Guide to Android BLE Development

Also some references on what some other users have tried:-

  • GATT Error 133 on onConnectionStateChange
  • Android 10 with GATT Error 133
  • GATT Error 133 with Android
  • Making Android BLE Work
  • Lessons for Android BLE Developers
like image 96
Youssif Saeed Avatar answered Sep 29 '22 06:09

Youssif Saeed