2016-04-16 23 views
8

Sto provando a connettermi a un termometro Bluetooth LE. Il collegamento al dispositivo funziona correttamente. L'unica parte che mi blocca è il gattCallBack ed è onCharacteristicChanged/Read. il "setNotification" e il descrittore "setValue" e "writeDescriptor" restituiscono tutti true. OnCharacteristicChanged non viene mai chiamato per restituire un valore.Android BLE onCharacteristicRead e onCharacteristicChanged non vengono mai chiamati

Ho utilizzato un piccolo programma molto pratico dal Play Store chiamato BLE Scanner per aiutarmi a fornirmi ulteriori informazioni sul dispositivo e sui suoi servizi e caratteristiche.

enter image description here

Questo è il motivo per cui ho semplicemente hard coded di servizio 2, caratteristica 0. io proprio non riesco a capire perché dopo che ho writeDescriptor, non ho mai visto nulla di tornare. La cosa interessante è che posso usare alcune delle altre caratteristiche (una è Intervallo di temperatura) e ricevo una risposta (anche se i dati sono confusi.)

Inoltre, per curiosità, perché ci sono 2 descrittori su questo caratteristica?

Questo codice è contenuto nel mio metodo MainActivity. Non sono sicuro se questo farebbe la differenza qui. Ho visto e provato diversi metodi pubblicati qui senza fortuna.

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() 
{ 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 
    { ... } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) 
    { 
     mGatt = gatt; 

     List<BluetoothGattService> services = mGatt.getServices(); 
     Log.i("onServicesDiscovered", services.toString()); 

     BluetoothGattCharacteristic characteristic = services.get(2).getCharacteristics().get(0); 

     mGatt.setCharacteristicNotification(characteristic, true); 

     BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); 
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 

     mGatt.writeDescriptor(descriptor); 

    }    

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 
    { ... } 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 
    { ... } 
}; 

UPDATE: ho deciso di controllare il metodo onDescriptorWrite e Log alcune informazioni.

@Override    
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) 
    { 
     Log.i("descriptorWRITE", Integer.toString(status)); 
    } 

Cosa interessante è che lo stato sta tornando 13 che è 'un'operazione di scrittura supera la lunghezza massima dell'attributo'.

Lo esaminerò ulteriormente.

risposta

4

Ho trovato il problema qui. Supponevo che il termometro stia usando il servizio BLE standard e l'impostazione delle caratteristiche. Non è. Hanno creato le loro caratteristiche personalizzate. Una volta passato a quella caratteristica, il metodo 'cambiato' ha iniziato a sparare.

0

Penso che manchi mGatt.readCharacteristic(characteristic).

Quando lo chiamate, attenderà che la lettura finisca e chiama onCharacteristicRead.