如何更新为BLE每5秒在Android的电池电量 [英] how to update the battery level for every 5seconds in ble in android

查看:833
本文介绍了如何更新为BLE每5秒在Android的电池电量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码我得到的一些电池电量percentage.but我要打电话通知,好让每5到10秒就更新battery.so的百分比,请帮助me.the以下是我的设备控制活动在此我codeD如下:

 私人最终的BroadcastReceiver mGattUpdateReceiver =新的BroadcastReceiver(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        最后弦乐行动= intent.getAction();


        如果(BluetoothLeService.ACTION_DATA_AVAILABLE.equals(动作)){
            displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }

    }
};
 

和在下面的方法我设置电池值和图像显示中的值中的百分比

 私人无效displayData(字符串数据){
    Log.v(______________________没有serives _______________,数据);
    如果(数据!= NULL){
        mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic,真正的);
        battery.setText(数据);
        INT X =的Integer.parseInt(battery.getText()的toString());
   。image_level.getLayoutParams()高度= X * 2;
    }
    否则,如果(数据== NULL)
          battery.setText(数据);
 }
 

和下面是我BLE服务,在此我添加一组通知方法WH ICH如下:

 公共无效setCharacteristicNotification(BluetoothGattCharacteristic特点,
                                          布尔启用){
    如果(mBluetoothAdapter == NULL || mBluetoothGatt == NULL){
        Log.w(TAG,BluetoothAdapter未初始化);
        返回;
    }
    mBluetoothGatt.setCharacteristicNotification(特点,启用);


    //对于立方体写
    如果(UUID_BatteryService.equals(characteristic.getUuid())){
         BluetoothGattDescriptor描述符= characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(描述);
    }


}
        公共无效onCharacteristicChanged(BluetoothGatt关贸总协定
                                        BluetoothGattCharacteristic特性){
        broadcastUpdate(ACTION_DATA_AVAILABLE,特点);
    }
};
 

公共布​​尔writeCharacteristic(BluetoothGattCharacteristic我){

  //检查mBluetoothGatt可用
    如果(mBluetoothGatt == NULL){
        Log.e(TAG,失去联系);

        返回false;
    }
    BluetoothGattService服务= mBluetoothGatt.getService(UUID_BatteryService);
    如果(服务== NULL){
        Log.e(TAG的服务没有找到!);

        //////////没有服务发现...........
         返回false;
    }


    布尔值状态= mBluetoothGatt.writeCharacteristic(我);

    Log.e(TAG,蓝牙写入的状态,+状态);
    返回状态;
}

            私人无效broadcastUpdate(最后的字符串操作){
    最终意向意图=新的意图(动作);
    sendBroadcast(意向);
}

   私人无效broadcastUpdate(最后的字符串操作,
         最后BluetoothGattCharacteristic特性){
   最终意向意图=新的意图(动作);
         如果(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG_BATTERY。
           的toString()。
      equalsIgnoreCase(characteristic.getUuid()的toString())){
         Log.v(_____________,在broadcastupdate ..........);


    最后一个字节[]数据= characteristic.getValue();
         如果(数据= NULL和放大器;!&安培; data.length> 0){
        最后StringBuilder的StringBuilder的=新的StringBuilder(data.length);
        对于(字节byteChar:数据)
        stringBuilder.append(的String.Format(%02X,byteChar));

        最终诠释标志= characteristic.getProperties();
        INT格式= -1;
        如果((标志&安培;!为0x01)= 0){
        格式= BluetoothGattCharacteristic.FORMAT_UINT16;
        Log.d(TAG,格式UINT16。);
        } 其他 {
        格式= BluetoothGattCharacteristic.FORMAT_UINT8;
        Log.d(TAG,UINT8。);
        }
        INT batterylevel = characteristic.getIntValue(格式,0);
        intent.putExtra(EXTRA_DATA,将String.valueOf(batterylevel));
        //intent.putExtra(EXTRA_DATA,new字符串(数据));

        }
        }
    sendBroadcast(意向);
}
 

解决方案

如果我很好理解你的问题,你需要以定期检查你的电池电量定时器。

例如,你可以开始你的设备控制活动后使用code,也许在onServiceConnected方法的末尾:

请把计时器在onServiceConnected()mServiceConnection对象的方法

的结束

 定时器定时=新的定时器(batteryTimer);
TimerTask的任务=新的TimerTask(){
@覆盖
公共无效的run(){
mBluetoothLeService.getBattery();
}
};
timer.scheduleAtFixedRate(任务,0,5000);
 

不要忘记调用timer.cancel()时,活动结束。

和在服务上,你可以把这样的事情:

 公共无效getBattery(){

如果(mBluetoothGatt == NULL){
Log.e(TAG,失去联系);
}

BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID);
如果(batteryService == NULL){
Log.d(TAG,电池服务未找到!);
返回;
}

BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID);
如果(batteryLevel == NULL){
Log.d(TAG,电池电量没有找到!);
返回;
}

mBluetoothGatt.readCharacteristic(batteryLevel);
}
 

这是这将需要修改的一个例子,但是,让你如何做到这一点的想法。

有人已经做了访问下面的链接电池值: <一href="http://stackoverflow.com/questions/19539535/how-to-get-the-battery-level-after-connect-to-the-ble-device">How拿到电池后级连接BLE设备?

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);
}

解决方案

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?

这篇关于如何更新为BLE每5秒在Android的电池电量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆