Question

in the following coding i am getting battery level of some percentage.but i want to call notify characteristics so that for every 5 to 10 secs it updates the percentage of battery.so please help me.the following is my device control activity,in this i coded as follows.

             private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();


        if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }

    }
};

and in the following method i am setting battery value and displaying in the value in percentage on image.

                  private void displayData(String data) {
    Log.v("______________________No serives_______________",data );
    if (data != null) { 
        mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
        battery.setText(data);
        int x=Integer.parseInt(battery.getText().toString());
   image_level.getLayoutParams().height =  x*2;
    }
    else if (data==null)
          battery.setText(data);
 }

and the following is my ble service in this i add the set notification method wh ich is as follows.

               public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);


    //For cube write
    if (UUID_BatteryService.equals(characteristic.getUuid())) {
         BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }


}
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
};

public boolean writeCharacteristic(BluetoothGattCharacteristic i){

    //check mBluetoothGatt is available
    if (mBluetoothGatt == null) {
        Log.e(TAG, "lost connection");

        return false;
    }
    BluetoothGattService Service = mBluetoothGatt.getService(UUID_BatteryService);
    if (Service == null) {
        Log.e(TAG, "service not found!");

        //////////NO service found...........
         return false;
    }


    boolean status = mBluetoothGatt.writeCharacteristic(i);

    Log.e(TAG, "bluetooth write status"+status);
    return status;
}

            private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

   private void broadcastUpdate(final String action,
         final BluetoothGattCharacteristic characteristic) {
   final Intent intent = new Intent(action);
         if(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG_BATTERY.
           toString().
      equalsIgnoreCase(characteristic.getUuid().toString())) {
         Log.v("_____________","in broadcastupdate..........");


    final byte[] data = characteristic.getValue();
         if (data != null && data.length > 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for(byte byteChar : data)
        stringBuilder.append(String.format("%02X ", byteChar));

        final   int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
        format = BluetoothGattCharacteristic.FORMAT_UINT16;
        Log.d(TAG, " format UINT16.");
        } else {
        format = BluetoothGattCharacteristic.FORMAT_UINT8;
        Log.d(TAG, "  UINT8.");
        }
        int batterylevel = characteristic.getIntValue(format, 0);
        intent.putExtra(EXTRA_DATA, String.valueOf(batterylevel));
        //intent.putExtra(EXTRA_DATA,new String(data));

        }
        }        
    sendBroadcast(intent);
}
Was it helpful?

Solution

If I well understood your question, you will need a Timer in order to check you battery level regularly.

For instance, you could use this code after starting your device control activity, maybe at the end of the onServiceConnected method :

please put the timer at the end of onServiceConnected() method of mServiceConnection object

Timer timer = new Timer("batteryTimer");
TimerTask task = new TimerTask() {
@Override
public void run() {
mBluetoothLeService.getBattery();
}
};
timer.scheduleAtFixedRate(task, 0, 5000);

And do not forget to call timer.cancel() when the activity is finishing.

And in the service, you could put something like that :

public void getBattery() {

if (mBluetoothGatt == null) {
Log.e(TAG, "lost connection");
} 

BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID);
if(batteryService == null) {
Log.d(TAG, "Battery service not found!");
return;
}

BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID);
if(batteryLevel == null) {
Log.d(TAG, "Battery level not found!");
return;
}

mBluetoothGatt.readCharacteristic(batteryLevel);
}

It is an example which would need to be modified but that gives you an idea on how to do it.

Somebody already did access to the battery value in the link below : How to get the battery level after connect to the BLE device?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top