android 蓝牙ble 133,java – Android蓝牙错误133

我正在尝试连接到

Android上的蓝牙设备.我在onClientConnectionState处理程序中收到状态133.我并不总是得到这个错误 – 有时连接很好.我无法指出触发问题的原因.重新启动设备和我的repro应用程序后,我甚至立即使用它.

我知道几个问题并建议解决这个问题,包括(从here,here和here):

>将UI线程用于所有BT API

>完成后务必关闭关贸总协定

但我正在做这一切.更重要的是,我的设备是一个Nexus 5(运行Lollipop),根据一些人甚至不需要BT交互在UI线程上.

我把最简单的复制品放在了一起.它在C#中,但Java等价物应该是显而易见的:

[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(Resource.Id.button);

button.Click += this.OnClick;

}

private async void OnClick(object sender, EventArgs e)

{

Action 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();

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 result;

public Callback()

{

this.result = new TaskCompletionSource();

}

public Task Result => this.result.Task;

public override void OnBatchScanResults(IList 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 log;

private readonly TaskCompletionSource result;

public GattCallback(Action log)

{

this.log = log;

this.result = new TaskCompletionSource();

}

public Task 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);

}

}

}

这是运行它的输出(我已经离开了Android的BluetoothGatt源的输出):

***** #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

如您所见,我与蓝牙堆栈的所有交互都发生在主线程上(#1).但尽管如此,我在onClientConnectionState处理程序中收到状态133.

我的清单有这些权限:

我正在使用最新的Marshmallow工具进行编译,目标是Marshmallow,最低目标为4.0.3(API级别15).

可能是什么导致了这个?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值