Android BluetoothGatt-estado 133-registro de devolución de llamada


En primer lugar leí RESUELTO: la devolución de llamada del GATT no se registra y tomé las medidas sugeridas en ese post para resolver este problema sin éxito. La solución recomendada allí si no lo has leído es hacer todas las llamadas BLE desde el hilo principal directamente o con un controlador.

Estoy trabajando en una aplicación BLE quiero ejecutar un servicio (llamado desde la actividad una vez cada 10 segundos) que realiza estas tareas:

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

Así que el problema... Todo funciona muy bien por un poco. Puedo realizar todo el servicio start {startService(...); in MainActivity} to finish {stopSelf(); in Service} 6 times.

En la séptima vez que obtengo BluetoothGatt No pudo registrar la devolución de llamada. No estoy seguro de por qué puedo ejecutarlo 6 veces con éxito y luego fallar en la séptima vez.

Tenga en cuenta que estoy haciendo todas las llamadas BLE desde el hilo principal, y que se ha confirmado en el registro cat desde múltiples ubicaciones.

Aquí está un esbozo de mi código:

SERVICIO.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);
    }
}

Así que, como he dicho, todas las cosas BLE se llaman desde el hilo principal a través de un controlador. El servicio se ejecuta con éxito 6 veces de principio a fin. En la séptima vez que me dicen que tonto no pudo registrar la devolución de llamada.

Puedo proporcionar más información de logcat si cree que es relevante. No lo hice en el post original porque soy salida de una gran cantidad de información a ella para verificar los datos recibidos, etc..

La siguiente información es el logcat información de la 7ª carrera de mi servicio de principio a fin.

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

Si has llegado aquí, ¡gracias! No he podido encontrar ninguna información sobre lo que significa status=133?! Solo sucede cuando la devolución de llamada falla. Cada dos veces es status = 0.

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

Si alguien pudiera responder a esto.. puede que me ayude mucho. O si alguien puede decirme por qué solo funciona 6 veces. Cualquier idea o corazonada podría ser útil!

Gracias a todos!

Author: Community, 2014-08-15

3 answers

Bien lo he descubierto. El problema fue principalmente un descuido de cuando estaba leyendo la documentación BluetoothGatt. Estaba llamando .disconnect(), pero no .close(). Dado que el Galaxy s4 solo puede manejar 6 conexiones a la vez, mi servicio solo se estaba ejecutando 6 veces. Agregar el .close() a mi código le permitió apagar correctamente la conexión y liberar esas conexiones usadas.

Fuente que me hizo volver a leer los documentos con más cuidado!

Así que recuerde usar .close() en su Objeto BluetoothGatt si tiene una conexión recurrente con el mismo dispositivo(s)!!

 52
Author: ck1221,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-24 08:00:00

Después de meses de investigación y tirando de mi cabello, he encontrado una solución de la que normalmente no se habla.

Su solicitud de conexión normal se ve algo como esto:

cGatt.connectGatt(this, false, gattCallback);

Hay otra versión del comando connectGatt, con un 4to parámetro. Este parámetro especifica el tipo de dispositivo bluetooth al que se está conectando. He añadido un " 2 " para especificar que estoy conectando a través de Bluetooth LE. (se llama "transporte", perdóname si mi explicación es incorrecta, pero resuelto todos mis problemas)

Prueba esto:

cGatt.connectGatt(this, false, gattCallback, 2);

Y BAM, ahora mi pesadilla # 133 ha terminado (rezo)!

 9
Author: btmcmahan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-30 17:07:00

Sistema operativo Android

mBluetoothDevice.connectGatt(context, false, callback);

Android OS >= 6.0:

mBluetoothDevice.connectGatt(context, false, callback, BluetoothDevice.TRANSPORT_LE);

En última instancia, se necesita equipo de hardware para resolver completamente este problema.

 2
Author: 郑松岚,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-29 06:08:30