我有一个类DiviceControlActivity。课程内部是UUID服务和特征列表。单击UUID特征时,将读取有关特征的数据并显示。
但我想用Button替换列表。当我点击按钮时,它应该读取特征并显示它。我的代码有问题:请帮助我。
这是关于UUID列表的代码:
private final ExpandableListView.OnChildClickListener servicesListClickListner = new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic = mGattCharacteristics
.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic,
// clear
// it first so it doesn't update the data field on the user
// interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
return true;
}
return false;
}
};
这是按钮的代码:
mBattery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final BluetoothGattCharacteristic characteristic = mGattCharacteristics
.get(4).get(1);
displayData(characteristic.getService().toString());
mBluetoothLeService.readCharacteristic(characteristic);
}
});
问题是当我点击按钮时:“BLE已停止”。 非常感谢你!
答案 0 :(得分:0)
您应该检查logcat,以便可以从您正在获取的Exception中查看堆栈跟踪。如果您不知道代码的其他部分是否有适当的检查,您可以轻松获得NullPointerException
或ArrayIndexOutOfBoundsException
,尤其是因为您在{{1}中对数组索引进行了硬编码(即4和1)。
更一般地说,您注册通知的方式不会起作用,可能会导致问题。以下是如何操作的示例:
mBattery.setOnClickListener