Android BluetoothGatt-状态133-注册回调 [英] Android BluetoothGatt - status 133 - register callback

查看:116
本文介绍了Android BluetoothGatt-状态133-注册回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我阅读了已解决:GATT回调无法注册,采取了该帖子中建议的步骤来成功解决此问题.如果您还没有读过,建议的解决方法是直接从主线程或使用处理程序进行所有BLE调用.

First of all I read SOLVED: GATT callback fails to register and took the steps suggested in that post to solve this issue with no success. The recommended fix in there if you haven't read it is to make all BLE calls from the main thread directly or with a handler.

我正在使用BLE应用程序,希望运行一项服务(每10秒从活动中调用一次)来执行以下任务:

I am working on a BLE app want to run a service (called from activity once every 10 seconds) that performs these tasks:

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次.

So the issue... Everything works great for a little bit. I can perform the entire service start {startService(...); in mainActivity} to finish {stopSelf(); in Service} 6 times.

我第七次收到BluetoothGatt未能注册回调.我不确定为什么我可以成功运行6次,然后在第7次失败.

On the 7th time I get BluetoothGatt Failed to register callback. I'm not sure why I can run it 6 times successfully and then fail on the 7th time.

请记住,我正在从主线程进行所有BLE调用,并且已在日志目录中的多个位置确认了这一点.

Keep in mind I am making all BLE calls from the main thread, and that has been confirmed in the log cat from multiple locations.

这是我的代码的概述:

SERVICE.JAVA

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次,我收到那个哑巴,无法注册回调.

So, like I have said, all BLE stuff is called from the main thread through a handler. The service successfully runs 6 times from start to finish. On the 7th time I get that dumb failed to register callback.

如果您认为相关,我可以提供更多logcat信息.我不在原始帖子中,因为我向其中输出了很多信息以验证收到的数据等.

I can provide more logcat information if you think it is relevant. I did not in the original post because I am output a lot of information to it to verify data received etc..

以下信息是我的服务自始至终第七次运行的logcat信息.

The information below is the logcat information for the 7th run of my service from start to finish.

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意味着什么的任何信息?!它仅在回调失败时发生.每隔一次状态为= 0.

If you have made it here, thanks! I could not find ANY information on what status=133 means?! It only happens when the callback fails. Every other time it is status=0.

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

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

If anyone could even answer this.. it may help me greatly. Or if anyone can tell me why it only runs 6 times. Any insight or hunch could be helpful!

谢谢大家!

推荐答案

好的,我已经弄清楚了.这个问题主要是我阅读BluetoothGatt文档时的疏忽.我正在呼叫.disconnect(),而不是.close().由于Galaxy s4一次只能处理6个连接,因此我的服务仅运行6次.在我的代码中添加.close()可以使其正确关闭连接并释放那些使用的连接.

Alright I have figured it out. The issue was mainly an oversight of when I was reading through the BluetoothGatt documentation. I was calling .disconnect(), but not .close(). Since the Galaxy s4 can only handle 6 connections at a time, my service was only running 6 times. Adding the .close() to my code allowed it to properly shut down the connection and freed up those used connections.

使我重新仔细阅读文档的资源!

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

So remember to use .close() on your BluetoothGatt object if you have a recurring connection to the same device(s)!!

这篇关于Android BluetoothGatt-状态133-注册回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆