【问题标题】:how to update the battery level for every 5seconds in ble in android如何在android中的ble中每5秒更新一次电池电量
【发布时间】:2014-04-15 06:45:05
【问题描述】:

在下面的编码中,我得到了某个百分比的电池电量。但我想调用通知特性,以便每 5 到 10 秒更新电池百分比。所以请帮助我。以下是我的设备控制活动,在这我编码如下。

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

    }
};

在以下方法中,我正在设置电池值并在图像上以百分比值显示。

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

以下是我的 ble 服务,我添加了设置通知方法 wh ich如下。

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

【问题讨论】:

  • 您的消息很长,很难知道您的代码中有什么不工作或缺少什么
  • 嗨,我应该修改say.i将提供的地方
  • 嗨,您不需要对您的其他问题添加评论,我会自动收到有关您的 cmets 或对此问题的更改的通知 :) 我仍然不知道您的应用程序中哪些有效,哪些无效,请告诉哪些部分正在工作,哪些部分您错过了。您是否只错过了每 5 秒启动一次电池检查的东西,还是有其他东西?我的时间不多,但我可以尝试提供帮助
  • 好吧,它工作正常。但我在上一个问题中所说的那样。从设备扫描活动我会得到 ble 设备的列表 ok
  • 如果我单击一个项目,它会移动到 devicecontrol 活动并在 displayData() 中显示电池电量,如上所示。它来自 public void onReceive(Context context, Intent intent) 中的方法。

标签: android bluetooth-lowenergy


【解决方案1】:

如果我很好地理解了您的问题,您将需要一个计时器来定期检查您的电池电量。

例如,您可以在启动设备控制活动后使用此代码,可能在 onServiceConnected 方法的末尾:

请把定时器放在mServiceConnection对象的onServiceConnected()方法的末尾

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

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

在服务中,你可以放这样的东西:

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

这是一个需要修改的示例,但它可以让您了解如何做。

有人已经在下面的链接中访问了电池值: How to get the battery level after connect to the BLE device?

【讨论】:

  • 好的,我正在考虑添加 public void onReceive(Context context, Intent intent) 这个方法。在计时器中我应该把 if()condition if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)); } 这样,它调用的每个时间间隔。
  • 但是它不在 main 方法中
  • 嗨,yves,我按照我说的添加了这个,但我遇到了错误。我以不同的方式思考。
  • 嗨 yves,如果你不理解我的代码,请检查此代码 我的代码 80% 是这样的,因为我添加了我的要求developer.android.com/samples/BluetoothLeGatt/src/…
  • 请检查 yves,至少 2maro mrg 我应该显示。给定的链接与我所做的完全一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多