无法在BLE onn android应用上编写特征

时间:2018-11-24 21:27:44

标签: android bluetooth-lowenergy uuid gatt

无法在BLE上写入特征。该应用程序可以连接至GATT并读取特征。我的第一个想法是我为Tx使用了错误的UUID。否则由于某种原因,我的特征为空。这是Nrf Connect的屏幕截图 enter image description here

GATT属性:

     public class RBLGattAttributes {
private static HashMap<String, String> attributes = new HashMap<String, 
String>();
public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000- 
8000-00805f9b34fb";
public static String BLE_SHIELD_TX = "713d0002-503e-4c75-ba94-3148f18d941e";
public static String BLE_SHIELD_RX = "713d0002-503e-4c75-ba94-3148f18d941e";
public static String BLE_SHIELD_SERVICE = "713d0000-503e-4c75-ba94- 
3148f18d941e";

static {
    // RBL Services.
    attributes.put("713d0000-503e-4c75-ba94-3148f18d941e",
            "BLE Shield Service");
    // RBL Characteristics.
    attributes.put(BLE_SHIELD_TX, "BLE Shield TX");
    attributes.put(BLE_SHIELD_RX, "BLE Shield RX");
}

public static String lookup(String uuid, String defaultName) {
    String name = attributes.get(uuid);
    return name == null ? defaultName : name;
}

BLE服务:

public void writeCharacteristic() {
    BluetoothGattCharacteristic  characteristic=null;

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    if (UUID_BLE_SHIELD_TX.equals(characteristic.getUuid())) {

        String ident="I";
    byte[] arr={1};
    try {
        characteristic.setValue(arr);
    } catch (NullPointerException e){Log.w(TAG,"sth is wrong");}
    mBluetoothGatt.writeCharacteristic(characteristic);

LogCat错误:     java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.bluetooth.BluetoothGattCharacteristic.getProperties()'

特征为空

0 个答案:

没有答案