Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

bluetooth lowenergy - how to update the battery level for every 5seconds in ble in android

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...