I am trying to connect to a bluetooth device on Android. I get status 133 in my handler onClientConnectionState. I do not always get this error - sometimes it connects perfectly. I could not say what was causing the problem. I even got it right after rebooting the device and my application for playback.
I am aware of several issues and proposed solutions to this problem, including ( here , here and here ):
- use UI thread for all BT APIs.
- be sure to close GATT when done
But I do it all. What else, my device is Nexus 5 (Lollipop is running), which, according to some, should not even need BT interactions in the user interface thread.
I put together the simplest reproduction possible. It is 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 is the output from starting this (I also stayed at the exit from Android BluetoothGatt):
*****
*****
07-01 11:53:21.458 D/BluetoothLeScanner(10377): onClientRegistered() - status=0 clientIf=5
*****
*****
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
*****
*****
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 happens in the main stream (# 1). But despite this, I get status 133 in my handler onClientConnectionState.
My manifest has the following 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 compile with the latest Marshmallow tools and target Marshmallow with a minimum target of 4.0.3 (API level 15).
What could be the reason for this?