I'm attempting to connect to a Bluetooth device on Android. I'm receiving status 133 in my onClientConnectionState
handler. I don't always get this error - sometimes it connects fine. I've not been able to put a finger on what triggers the problem. I've even had it immediately after restarting the device and my repro app.
I'm aware of several questions and suggested solutions to this problem, including (from here, here, and here):
- use the UI thread for all BT APIs
- be sure to close the GATT when done
But I am doing all that. What's more, my device is a Nexus 5 (running Lollipop) which according to some shouldn't even need the BT interactions to be on the UI thread.
I have put together the simplest possible repro. It's in C# but the Java equivalent should be obvious:
[Activity(Label = "BluetoothGatt133ErrorRepro", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Android.OS.Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var button = FindViewById<Button>(Resource.Id.button);
button.Click += this.OnClick;
}
private async void OnClick(object sender, EventArgs e)
{
Action<string> log = message => Console.WriteLine($"***** #{Environment.CurrentManagedThreadId} {message}");
log("Beginning");
var bluetoothManager = (BluetoothManager)Application.Context.GetSystemService(Context.BluetoothService);
var adapter = bluetoothManager.Adapter;
var scanner = adapter.BluetoothLeScanner;
var callback = new Callback();
var filters = new List<ScanFilter>();
var settings = new ScanSettings.Builder()
.SetScanMode(global::Android.Bluetooth.LE.ScanMode.LowLatency)
.Build();
log("Starting scan");
scanner.StartScan(filters, settings, callback);
var result = await callback.Result;
log($"Got device: {result.Device.Name}");
var remoteDevice = adapter.GetRemoteDevice(result.Device.Address);
var gattCallback = new GattCallback(log);
log("Connecting GATT");
var gatt = remoteDevice.ConnectGatt(Application.Context, true, gattCallback);
gatt.Connect();
await gattCallback.Result;
log("Disconnecting GATT");
gatt.Close();
gatt.Dispose();
}
private sealed class Callback : ScanCallback
{
private readonly TaskCompletionSource<ScanResult> result;
public Callback()
{
this.result = new TaskCompletionSource<ScanResult>();
}
public Task<ScanResult> Result => this.result.Task;
public override void OnBatchScanResults(IList<ScanResult> results)
{
foreach (var result in results)
{
this.HandleResult(result);
}
}
public override void OnScanResult(ScanCallbackType callbackType, ScanResult result)
{
this.HandleResult(result);
}
public override void OnScanFailed(ScanFailure errorCode)
{
this.result.TrySetException(new InvalidOperationException($"Failed with error code {errorCode}."));
}
private void HandleResult(ScanResult result)
{
if (result.Device.Name.Contains("elided"))
{
this.result.TrySetResult(result);
}
}
}
private sealed class GattCallback : BluetoothGattCallback
{
private readonly Action<string> log;
private readonly TaskCompletionSource<bool> result;
public GattCallback(Action<string> log)
{
this.log = log;
this.result = new TaskCompletionSource<bool>();
}
public Task<bool> Result => this.result.Task;
public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
{
this.log($"Connection state changed to {newState} with status {status}.");
this.result.TrySetResult(true);
}
}
}
And here's the output from running this (I've left in the output from Android's BluetoothGatt source too):
***** #1 Beginning
***** #1 Starting scan
07-01 11:53:21.458 D/BluetoothLeScanner(10377): onClientRegistered() - status=0 clientIf=5
***** #1 Got device: elided
***** #1 Connecting GATT
07-01 11:53:22.833 D/BluetoothGatt(10377): connect() - device: 00:00:DE:AD:BE:EF, auto: true
07-01 11:53:22.833 D/BluetoothGatt(10377): registerApp()
07-01 11:53:22.833 D/BluetoothGatt(10377): registerApp() - UUID=fa5bce8a-416d-47fe-9a8a-e44156f7e865
07-01 11:53:22.834 D/BluetoothGatt(10377): onClientRegistered() - status=0 clientIf=6
07-01 11:53:24.622 D/BluetoothGatt(10377): onClientConnectionState() - status=133 clientIf=6 device=00:00:DE:AD:BE:EF
***** #4 Connection state changed to Disconnected with status 133.
***** #1 Disconnecting GATT
07-01 11:53:24.707 D/BluetoothGatt(10377): close()
07-01 11:53:24.707 D/BluetoothGatt(10377): unregisterApp() - mClientIf=6
As you can see, all my interaction with the Bluetooth stack is occurring on the main thread (#1). But despite that, I'm receiving status 133 in my onClientConnectionState
handler.
My manifest has these permissions:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm compiling with latest Marshmallow tooling, and am targeting Marshmallow with a minimum target of 4.0.3 (API level 15).
What might be causing this?