亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android BluetoothGatt 類編寫特性屬性檢查始終為 false

Android BluetoothGatt 類編寫特性屬性檢查始終為 false

當年話下 2023-06-04 10:29:04
我想創建一個 android 應用程序,以便連接 ESP32 板并從中檢索數據,以及使用低功耗藍牙通信將值發送到板的能力。我有一個內置 BLE 服務器的 ESP32 板。我已經實現了具有以下特征的自定義服務。/* define the characteristic and it's propeties */BLECharacteristic dataCharacteristic(    BLEUUID((uint16_t)0x1A00),    BLECharacteristic::PROPERTY_READ |    BLECharacteristic::PROPERTY_WRITE |        BLECharacteristic::PROPERTY_NOTIFY);我在 android 應用程序中成功實現了所有掃描、讀取和通知功能,但是在編寫 BluetoothGatt.writeCharacteristic 時總是在第一個條件中返回 false:  if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0                && (characteristic.getProperties()                & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) {            return false;        }在調試 android 應用程序時,characteristic.getProperties() 始終為 18。 public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0                && (characteristic.getProperties()                & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) {            return false;        }        if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());        if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false;        BluetoothGattService service = characteristic.getService();        if (service == null) return false;        BluetoothDevice device = service.getDevice();        if (device == null) return false;        synchronized (mDeviceBusy) {            if (mDeviceBusy) return false;            mDeviceBusy = true;        }        try {            mService.writeCharacteristic(mClientIf, device.getAddress(),                    characteristic.getInstanceId(), characteristic.getWriteType(),                    AUTHENTICATION_NONE, characteristic.getValue());        } catch (RemoteException e) {            Log.e(TAG, "", e);            mDeviceBusy = false;            return false;        }        return true;    }
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

經過大量搜索和嘗試各種方法來實現 android 應用程序和內置 BLE 服務器的 ESP32 板的上述通信,我找到了解決方案。


在我所有的測試中,我通過以下方式使用 UUID:


在 ESP32 中聲明特征 uuid


Characteristic_UUID = BLEUUID((uint16_t) 0x1A00))

并使用從android搜索這個uuid


Characteristic _UUID = convertFromInteger (0x1A00)

convertFromInteger 函數:


    public UUID convertFromInteger (int i) 

    {

        final long MSB = 0x0000000000001000L;

        final long LSB = 0x800000805f9b34fbL;

        long value = i & 0xFFFFFFFF;

        return new UUID (MSB | (value << 32), LSB);

    }

當我遵循一個教程時找到了解決方案,在該教程中我注意到了 uuid 以及與我使用的 uuid 的區別。當我用隨機生成的 uuid 替換我的舊 uuid 時,例如“cff6dbb0-996f-427b-9618-9e131a1d6d3f”,整個BLE 服務器的 writeCharacteristic 過程沒有任何問題。


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號