Android BluetoothGatt - 状态 133 - 注册回调

标签 android bluetooth-lowenergy gatt

首先我阅读了SOLVED: GATT callback fails to register并采取了该帖子中建议的步骤来解决此问题,但没有成功。如果您还没有阅读,建议的修复方法是直接从主线程或使用处理程序进行所有 BLE 调用。

我正在开发一个 BLE 应用程序,想要运行一个服务(每 10 秒从 Activity 调用一次)来执行这些任务:

1)Gets list of our products available to connect to (done, works)

2)For each available device:

          2a)connect to device
          2b)discover services
          2c)read 5 characteristics in this fashion:
             2c1)read characteristic
             2c2)onCharacteristicRead parse data
             2c3)when finished with data read next characteristic
             2c4)repeat until all are read (this is done using a state var and switch statement)
         2d)disconnect from device
         2e)connect to next device
         2f)repeat until all devices are read from
         2g)stopSelf()

所以问题......一切都很好。我可以执行整个服务启动 {startService(...);在 mainActivity} 中完成 {stopSelf();在服务中} 6 次。

第 7 次我收到 BluetoothGatt 无法注册回调。我不知道为什么我可以成功运行 6 次,然后第 7 次失败。

请记住,我正在从主线程进行所有 BLE 调用,这已在多个位置的 log cat 中得到确认。

这是我的代码大纲:

SERVICE.JAVA

private Handler handler = new Handler();
private BluetoothGatt cGatt = null;
private int unitIndex = 0; // keep track of currently connected unit
private int state = 0; //used to keep track of which characteristic to read next

public int onStartCommand(Intent intent, int flags, int startId) 
{
    Log.i(TAG, "Service Started...");
    //get ArrayList of units

    if(units.size > 0)
        handler.post(connectNextRunnable); //calls connectNextDevice()
    else
        stopSelf();   
}

private Runnable discoverServices = new Runnable()
{
    public void run()
    {
        cGatt.discoverServices();
    }
}

private Runnable readNextValue = new Runnable()
{
    public void run()
    {
        BluetoothGattCharacteristic c = null;
        switch(state)
        {
            //set c to appropriate characteristic
        default: // all characteristics read
            unitIndex++;
            handler.post(connectNextRunnable)
            return
        }

        cGatt.readCharacteristic(c);
    }
}

private void connectNextDevice()
{
    if(unitIndex == 0)
        store System.nanoTime in variable

    if(unitIndex >= units.size) //finished will all units
        stopSelf();

    if(unitIndex < units.size)
        cGatt.disconnect //if null
        cGatt.connectGatt(this, false, gattCallback)
}

private BluetoothGattCallback gattCallback = new BluetoothGattCallback() 
{
    public void onConnectionStateChange() 
    {
        handler.post(discoverServices);
    }

    public void onServicesDeiscovered() 
    {
        handler.post(readNextValue);
    }

    public void onCharacteristicRead() 
    {
        ParseData();
    }

    private void ParseData()
    {
        //do stuff with data
        handler.post(readNextValue);
    }
}

所以,就像我说过的,所有 BLE 的东西都是通过处理程序从主线程调用的。该服务从开始到结束成功运行 6 次。第 7 次,我收到那个笨蛋注册回调失败。

如果您认为相关,我可以提供更多 logcat 信息。我没有在原始帖子中,因为我向它输出了大量信息以验证收到的数据等。

以下信息是我的服务从头到尾第7次运行的logcat信息。

08-15 12:00:10.746: I/PMIQ BTS(32027): Service Started...
08-15 12:00:10.746: I/PMIQ BTS(32027): Units: 1
08-15 12:00:10.746: D/AbsListView(32027): unregisterIRListener() is called 
08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to next device...
08-15 12:00:10.766: I/PMIQ BTS(32027): Unit index = 0
08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to pmIQ-IQ130_D93A
08-15 12:00:10.766: I/System.out(32027): main
08-15 12:00:10.766: D/BluetoothGatt(32027): connect() - device: 00:1E:C0:19:D9:3A, auto: false
08-15 12:00:10.766: D/BluetoothGatt(32027): registerApp()
08-15 12:00:10.766: D/BluetoothGatt(32027): registerApp() - UUID=e9d10870-4b09-451c-a9fa-c6b5f3594a77
08-15 12:00:10.766: I/BluetoothGatt(32027): Client registered, waiting for callback
08-15 12:00:10.766: D/BluetoothGatt(32027): onClientRegistered() - status=133 clientIf=0
08-15 12:00:10.766: I/PMIQ BTS(32027): CONECTION STATE CHANGED...Binder_2
**08-15 12:00:10.766: E/BluetoothGatt(32027): Failed to register callback**
08-15 12:00:10.766: I/PMIQ BTS(32027): Could not connect to null ... 257
08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to next device...
08-15 12:00:10.766: I/PMIQ BTS(32027): Unit index = 1
08-15 12:00:10.766: I/PMIQ BTS(32027): ******************************
08-15 12:00:10.766: I/PMIQ BTS(32027): Start Time: 4360642409647
08-15 12:00:10.766: I/PMIQ BTS(32027): End Time: 4360648970925
08-15 12:00:10.766: I/PMIQ BTS(32027): Difference: 6561278
08-15 12:00:10.766: I/PMIQ BTS(32027): Time to complete: 6
08-15 12:00:10.766: I/PMIQ BTS(32027): ******************************
08-15 12:00:10.876: I/PMIQ BTS(32027): ...Service Destroyed

如果你在这里成功了,谢谢!我找不到任何关于 status=133 意味着什么的信息?!它仅在回调失败时发生。每隔一段时间它是 status=0。

08-15 12:00:10.766: D/BluetoothGatt(32027): onClientRegistered() - status=133 clientIf=0

如果有人能回答这个问题.. 它可能对我有很大帮助。或者,如果有人能告诉我为什么它只运行 6 次。任何见解或预感都会有所帮助!

谢谢大家!

最佳答案

好吧,我想通了。这个问题主要是我在阅读 BluetoothGatt 文档时的疏忽。我调用的是 .disconnect(),而不是 .close()。由于 Galaxy s4 一次只能处理 6 个连接,我的服务只运行了 6 次。将 .close() 添加到我的代码中,可以正确关闭连接并释放那些使用过的连接。

Source that made me re-read the docs more carefully!

因此,如果您与同一设备有重复连接,请记住在您的 BluetoothGatt 对象上使用 .close()!!

关于Android BluetoothGatt - 状态 133 - 注册回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25330938/

相关文章:

Android:在异步任务期间处理后退按钮

android - 无法使用 Robotium 单击全屏

ios - iOS10上的CoreBluetooth:CBPeripheral discoverServices之后超时

android - BLE Android写入特征数据

android - 断开连接的 BLE 设备的 GATT 服务器

android - 电子邮件地址已被另一个帐户使用(使用电子邮件/密码 Firebase 进行身份验证)

android - Android 上的 TCP 数据包编辑

ios - CBPeripheralManager : Does add(_ service: CBMutableService) advertise peripheral?

android - Android 6.0.1 中的 Gatt 缓存