2016-09-22 182 views
9

我正在开发一款应用程序,可以在其中查找和配置BLE设备。我使用标准的Android BLE API,但最近我遇到了一些奇怪的问题。Android停止查找BLE设备:onClientRegistered() - status = 133 clientIf = 0

当我打开我的应用程序时,BLE扫描正常。

mBluetoothAdapter.startLeScan(mLeScanCallback); // for Kitkat and below 

mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); // for Lollipop and above 

在我收到以下消息logcat的(我想这是很重要的这个问题):

D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5 

在我的应用我我使用扫描也可以读取我的BLE设备的某些特性(例如电池状态)。我连接到一个设备,使用以下单独的片段读取此特征:

mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE); 
mBluetoothAdapter = mBluetoothManager.getAdapter(); 
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress); 
mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback); 

特征被正确读取。在onCharacteristicRead回调我也断开,靠近盖特:

mBluetoothGatt.disconnect(); 
mBluetoothGatt.close(); 

每次我打开一个片段来读取的特性(不管它是否是同一设备或不)clientIf值增加。我可以在logcat中看到:直到clientIf值等于10,BLE扫描停止寻找任何设备

D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6 
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7 
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8 
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9 
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10 

一切正常,我无法连接到我的任何设备读取任何特性等,以及logcat的无限显示这些消息:

D/BluetoothGatt: unregisterApp() - mClientIf=0 
D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0 

修复它是杀死该应用,然后重新启动,或通过将其关闭,并手动重新启动上的蓝牙的唯一方法。我只在某些设备上遇到此问题,例如Xperia Z1(Android KitKat)和Galaxy S4(Android Lollipop)。我无法在运行Android Marshmallow的Xperia Z3 Compact上重现此问题...

有什么我可以做的吗?我已经尝试了多个“解决方案”,例如 - 从UI线程调用所有BLE方法并关闭/断开GATT,但没有任何帮助。我该如何解决它?

回答

8

我会回答我自己的问题,因为propably它会帮助有同样问题的人。

首先,我无法解决mClientIf值达到10后蓝牙故障的问题。不过,我发现了一个解决方法,这有助于我的情况。

尽管我在第一个Fragment停止了灯塔搜索,并且在另一个开始读取了特征,但BLE API显然没有立即停止搜索,并且在打开下一个Fragment后系统创建了另一个“客户端”。

这就是为什么在离开第一个Fragment并开始读取另一个特征之前需要等待一段时间的原因。

这种方法是所谓的“新” Fragment(使用postDelayed帮我解决这个问题)的onCreateView

private void getBatteryCharacteristic() { 
     new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       mBeaconBatteryStateReader = new BeaconBatteryStateReader(
         BeaconDetailsActivity.this, 
         mBeacon.getMacAddress()); 
       mBeaconBatteryStateReader.readBatteryState(BeaconDetailsActivity.this); 
      } 
     }, 100); 
    } 
+1

我也有类似的问题,我将断开之间有点延迟扫描解决这个问题也是。谢谢! – Tijn

+0

我找到了和你一样的解决方案。详情:https://github.com/googlesamples/android-BluetoothLeGatt/issues/44 –

0

关闭gatt对象是释放资源的正确方法。如果它仍然无法正常工作,那么在特定手机上的Android中会有一个错误。

为了尽量减少达到极限的机会,可以确保每个设备永远不会有多个gatt对象。

相关问题