Como sincronizar leitura / gravação BLE característica no android

votos
0

Meu aplicativo android pode escrever ou ler a característica dependendo da ordem dos métodos. Como posso sincronizar ambos? Eu preciso escrever com um ouvinte onClick e ler valores (não necessariamente respostas a partir da característica de gravação). Com o código abaixo só pode ler. Se eu comentar mBluetoothLeService.readCharacteristic(characteristic);então eu só posso escrever ONCE

Main Activity UI Binder com Service BLE:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

       final String action = intent.getAction();


        if (RBLService.ACTION_GATT_DISCONNECTED.equals(action)) {
        } else if (RBLService.ACTION_GATT_SERVICES_DISCOVERED
                .equals(action)) {
            getGattService(mBluetoothLeService.getSupportedGattService());
            Log.w(TAG,calling gattservice);

        }  if (RBLService.ACTION_DATA_AVAILABLE.equals(action)) {
            Log.w(TAG,sending data to display function);
            displayData(intent.getExtras().getString(RBLService.EXTRA_DATA));
        }
        }
};

Obtendo o Serviço, escrita / leitura:

private void getGattService(BluetoothGattService gattService) {

    BluetoothGattCharacteristic characteristic = gattService
            .getCharacteristic(RBLService.UUID_BLE_SHIELD_TX);
    map.put(characteristic.getUuid(), characteristic);
    mBluetoothLeService.setCharacteristicNotification(characteristic,true);
    mBluetoothLeService.readCharacteristic(characteristic);
    Log.w(TAG,I am at read charac method);

    Log.w(TAG, TX ok:);
    if (wData == TRUE) {
        try {
            characteristic.setValue(arr);
            Log.w(TAG, characteristic set to array + characteristic);
        } catch (NullPointerException e) {
            Log.w(TAG, sth is wrong @player);
        }
        try {
            mBluetoothLeService.writeCharacteristic(characteristic);
            Log.w(TAG, write success + characteristic);

        } catch (NullPointerException e) {
            Log.w(TAG, sth is wrong @player 2);
        }

BLE Service, Escrever Característica:

public void writeCharacteristic(BluetoothGattCharacteristic characteristic) 
{
        //BluetoothGattCharacteristic characteristic=null;
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, BluetoothAdapter not initialized);
            return;
        }
        /*if (UUID_BLE_SHIELD_TX.equals(characteristic.getUuid())){
            Log.w(TAG,characteristic uuid equals with TX);
        }*/
        try{
        mBluetoothGatt.writeCharacteristic(characteristic);
        Log.w(TAG,characteristic write success+characteristic);}
        catch (NullPointerException e){Log.w(TAG,sth is wrong 2);}
        return;
    }

Read Characteristic:
private void broadcastUpdate(final String action,
        final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    if (UUID_BLE_SHIELD_RX.equals(characteristic.getUuid())) {

        final byte[] rx = characteristic.getValue();
        //Log.w(TAG, value of rx[0]+rx[0]);
        int location=(short)((rx[1]&0xff)<<8|(rx[2]&0xff));
        //int location=rlocation &  0XFF;

        Log.w(TAG, inf-Location: +location);

        int pressure=((rx[3]&0xff)<<8|(rx[4]&0xff));
        Log.w(TAG, inf-Pressure: +pressure);
        /*Log.w(TAG,rx[0]+rx[0]);
        Log.w(TAG,rx[1]+rx[1]);
        Log.w(TAG,rx[2]+rx[2]);
        Log.w(TAG,rx[3]+rx[3]);
        Log.w(TAG,rx[4]+rx[4]);*/

StringBuilder hexString = new StringBuilder();
 for (int i = 0; i < rx.length; ++i) {
 //  hexString.append(Integer.toHexString(0xFF & rx[i]));
   hexString.append(String.format(%02X    ,rx[i]));
 }



    final String data=hexString.toString();

        intent.putExtra(EXTRA_DATA, data);
    }
Publicado 27/11/2018 em 18:07
fonte usuário
Em outras línguas...                            

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more