【发布时间】:2015-12-14 12:49:42
【问题描述】:
我正在制作一个通过蓝牙连接到 sensortag cc2650 的应用程序,但是当我尝试将值写入其特性的描述符时,该特性没有,我该如何配置它?
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
byte[] data = characteristic.getValue();
Log.v("valores", String.valueOf(data));
}
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
bluetoothGatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
List<BluetoothGattService> services = bluetoothGatt.getServices();
for (BluetoothGattService service : services) {
if (service.getUuid().compareTo(movementServiceUUID) == 0) {
writeCharacteristic(service.getCharacteristics(), service);
break;
}
}
}
};
这是我连接到蓝牙设备并传递回调的部分
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ItemListView item = adapter.getItem(position);
bluetoothGatt = devicesMap.get(item.getId()).connectGatt(MainActivity.this, false, btleGattCallback);
}
});
这是连接后的回调
private void writeCharacteristic(List<BluetoothGattCharacteristic> list, BluetoothGattService service) {
for (BluetoothGattCharacteristic charac : list) {
if (charac.getUuid().compareTo(movementDataUUID) == 0) {
bluetoothGatt.setCharacteristicNotification(charac, true);
//This descriptor ok
BluetoothGattDescriptor descriptor = charac.getDescriptor(this.movementDataDescriptorUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
if (charac.getUuid().compareTo(movementConfigUUID) == 0) {
bluetoothGatt.setCharacteristicNotification(charac, true);
//This descriptor is null
BluetoothGattDescriptor descriptor = charac.getDescriptor(this.movementConfigUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
if (charac.getUuid().compareTo(movementPeriodUUID) == 0) {
bluetoothGatt.setCharacteristicNotification(charac, true);
//This descriptor is null
BluetoothGattDescriptor descriptor = charac.getDescriptor(this.movementPeriodUUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
}
}
【问题讨论】:
标签: java android bluetooth bluetooth-lowenergy texas-instruments