0

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);
}
image
  • 59
  • 1
  • 14
  • Your message is very long and it is difficult to know what is not working or missing in your code – Yves Delerm Apr 15 '14 at 09:03
  • hi yves where i should modify say.i will provide – image Apr 15 '14 at 09:07
  • Hi, you do not need to add comment on your other question, I get automatically notified on your comments or changes on this question :) I still dont know what is working and what is not in your app, please tell which parts are working and which parts you miss. Do you only miss something that launches the battery check every 5 seconds, or is there other things ? I have few time but I can try to help – Yves Delerm Apr 15 '14 at 09:35
  • ok yves it is working fine.but what i say in previous question like that.from devicescan activity i will get list of ble devices ok – image Apr 15 '14 at 09:40
  • if i click one item it moves to devicecontrol activity and displays battery level in displayData() as above.it gets from method in public void onReceive(Context context, Intent intent). – image Apr 15 '14 at 09:42
  • upto this clear to u. – image Apr 15 '14 at 09:43
  • then all the rest coding is in bleservice class in this i used setCharacteristicNotification(BluetoothGattCharacteristic characteristic,boolean enabled) to get notify and write characteristics to get value of battery level and broadcast update for sending intent. – image Apr 15 '14 at 09:47
  • but my question is it should check every 5 to 10 secs and update my battery level.means i should do for loop or what i dont know. – image Apr 15 '14 at 09:48
  • for example if i had battery level now 100 after 5 to 10 secs it may discharge na so it should update as like 99. – image Apr 15 '14 at 09:50
  • hi yves,did u understand what i say. – image Apr 15 '14 at 09:51

1 Answers1

4

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?

Community
  • 1
  • 1
Yves Delerm
  • 785
  • 4
  • 10
  • ok i am thinking to add in public void onReceive(Context context, Intent intent) this method.in the timer shall i put if()condition if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); } this so that every interval of time it calls. – image Apr 15 '14 at 10:00
  • but yves it is not in main method – image Apr 15 '14 at 10:03
  • hi yves i added this as i said but i got errors.i think in different way. – image Apr 15 '14 at 10:29
  • hi yves, if u does not understand my code please check this code 80% of my code is like this in that i added my requirments http://developer.android.com/samples/BluetoothLeGatt/src/com.example.android.bluetoothlegatt/DeviceControlActivity.html – image Apr 15 '14 at 11:13
  • please check yves,atleast by 2maro mrg i should show.the given link is completely about ble same like that only i did. – image Apr 15 '14 at 11:15
  • hi, I will get a look and try to answer, this is something I am not familiar with – Yves Delerm Apr 15 '14 at 11:38
  • i am getting battery level and i want to update level for every 5 to 10 secs. – image Apr 15 '14 at 12:26
  • hi yves,gd mrg.have u got any info on this question. – image Apr 16 '14 at 04:57
  • Hello, I am still very busy. Whith your current code, do you get the battery level at least once ? – Yves Delerm Apr 16 '14 at 08:38
  • yes, i am getting battery level.it is visible.but my question is it should update for every certain interval of time. – image Apr 16 '14 at 08:41
  • i added this code so that it notify when changes but no use. if ((charrec | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { mNotifyCharacteristic = gattCharacteristic; mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true); } – image Apr 16 '14 at 08:43
  • I am not sure about it but it might be that with setCharacteristicNotification you only get notifications for rare events like battery low, not for every battery percentage change. So if you want to check battery level regularly, you would need to use a Timer like I said. But as I dont know this API, this only is an opinion – Yves Delerm Apr 16 '14 at 08:53
  • i tryed timer but i got errors and app has stopped and movedto scan activity – image Apr 16 '14 at 09:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50764/discussion-between-yves-delerm-and-user3467237) – Yves Delerm Apr 16 '14 at 09:02
  • hi yves,if u r free please check this link http://stackoverflow.com/questions/23213655/how-to-add-edit-button-in-listview-and-refresh-list-after-edit/23213745?noredirect=1#23213745 – image Apr 22 '14 at 08:13
  • hi yves please check this link http://stackoverflow.com/questions/23901986/spinner-not-displaying-telugu-font – image May 28 '14 at 05:22