【发布时间】: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