У меня проблемы с обработка свойств характеристик, определенных в устройствах BLE.
Вот мой код для действия, которое считывает свойства, связанные с характеристикой.Properties.java:
Код: Выделить всё
public class Properties extends AppCompatActivity {
private static final String TAG = "Properties";
private BluetoothGattCharacteristic characteristic;
private BluetoothGatt gatt;
private Button btnRead, btnWrite, btnNotify;
private boolean isNotifying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_properties);
btnRead = findViewById(R.id.btnRead);
btnWrite = findViewById(R.id.btnWrite);
btnNotify = findViewById(R.id.btnNotify);
// Retrieve the characteristic UUID from the Intent
String characteristicUUID = getIntent().getStringExtra("characteristicUUID");
// Retrieve the BluetoothGatt and find the characteristic by UUID
gatt = GattManager.getGatt();
if (gatt == null) {
Log.e(TAG, "No Gatt instance received from MainActivity to Properties");
} else {
Log.d(TAG, "Gatt instance received from MainActivity to Properties");
}
characteristic = findCharacteristicByUUID(characteristicUUID);
if (characteristic == null) {
Log.e(TAG, "No BluetoothGattCharacteristic received from intent to Properties");
} else {
Log.d(TAG, "BluetoothGattCharacteristic received from intent to Properties");
}
// check and enable available properties
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0) {
btnRead.setVisibility(View.VISIBLE);
btnRead.setOnClickListener(view -> readCharacteristic());
}
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) {
btnWrite.setVisibility(View.VISIBLE);
btnWrite.setOnClickListener(view -> writeCharacteristic());
}
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
btnNotify.setVisibility(View.VISIBLE);
btnNotify.setOnClickListener(view -> toggleNotify());
}
}
private BluetoothGattCharacteristic findCharacteristicByUUID(String uuid) {
if (gatt != null) {
for (BluetoothGattService service : gatt.getServices()) {
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
if (characteristic.getUuid().toString().equals(uuid)) {
return characteristic;
}
}
}
}
return null;
}
private void readCharacteristic() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
gatt.readCharacteristic(characteristic);
onCharacteristicRead(characteristic);
}
private void writeCharacteristic() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Write Value");
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("OK", (dialog, which) -> {
String value = input.getText().toString();
characteristic.setValue(value.getBytes());
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
boolean success = gatt.writeCharacteristic(characteristic);
if (success) {
Toast.makeText(this, "Write successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Write failed", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
private void toggleNotify() {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if (!isNotifying) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
gatt.setCharacteristicNotification(characteristic, true);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
btnNotify.setText("Stop Notify");
Toast.makeText(this, "Notifications enabled", Toast.LENGTH_SHORT).show();
} else {
gatt.setCharacteristicNotification(characteristic, false);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
btnNotify.setText("Start Notify");
Toast.makeText(this, "Notifications disabled", Toast.LENGTH_SHORT).show();
}
isNotifying = !isNotifying;
}
public void onCharacteristicRead(BluetoothGattCharacteristic characteristic) {
// Display characteristic value in a dialog
String value = new String(characteristic.getValue());
new AlertDialog.Builder(this)
.setTitle("Characteristic Value")
.setMessage("Value: " + value)
.setPositiveButton("OK", null)
.show();
}
}
The GattManager.java:
Код: Выделить всё
public class GattManager {
private static BluetoothGatt gatt;
public static BluetoothGatt getGatt() {
return gatt;
}
public static void setGatt(BluetoothGatt gattInstance) {
gatt = gattInstance;
}
}
**
Я пробовал в соответствии с приведенными кодами. Но у меня возникли проблемы с управлением свойствами чтения и записи.
Подробнее здесь: https://stackoverflow.com/questions/791 ... le-devices
Мобильная версия