2 回答

TA貢獻(xiàn)1810條經(jīng)驗 獲得超4個贊
您的代碼中缺少的一件事是設(shè)置通知,以便 Central 可以收聽 Peripheral 的響應(yīng)。嘗試這樣的事情:
public void setNotifications() {
BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
characteristic.addDescriptor(descriptor);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
bluetoothGatt.writeDescriptor(descriptor);
bluetoothGatt.setCharacteristicNotification(characteristic, true);
}
之后,您可以向設(shè)備發(fā)送命令并聽到它返回。
public void returnData(String data) {
BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);
String dataString = data;
dataString.getBytes();
writeCharacteristic.setValue(dataString);
writeCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
bluetoothGatt.writeCharacteristic(writeCharacteristic);
}

TA貢獻(xiàn)1836條經(jīng)驗 獲得超3個贊
首先,閱讀藍(lán)牙概述。查看項目是否添加了藍(lán)牙權(quán)限。
這里的一個錯誤是isConnected=true
設(shè)置得太早了,因為你可以認(rèn)為你是在發(fā)現(xiàn) ble 服務(wù)之后連接的(status == BluetoothGatt.GATT_SUCCESS)
。否則無法讀寫特性。
一個很好的起點可以是這個來自谷歌的回購協(xié)議。這是一個舊項目,我不確定您是否必須更新一些依賴項才能使其編譯,但這并不重要。
建立連接并開始讀取字節(jié)很容易,但是如果你想與 ble 設(shè)備建立可靠的連接,由于 android 碎片化和制造商不遵循藍(lán)牙規(guī)范,這可能非常困難,幾乎不可能使藍(lán)牙低適用于所有設(shè)備的能量。
一旦你開始閱讀一些字節(jié),我建議你通過這個視頻學(xué)習(xí)一些重要的技巧。如果你想更深入,請仔細(xì)閱讀這個關(guān)于 ble 的精彩資源。
一旦你開始對 ble 感到絕望,可能是閱讀這個已知問題列表的好時機(jī)
最后,您會發(fā)現(xiàn)在 android 中使用 ble low energy 可以做的最好的事情是使用開源庫,例如 Nordic semiconductor ble 庫或RxAndroid Ble。
但在使用 ble 庫之前,最好先了解該庫在做什么并了解您為什么需要它。
編輯:我從未使用過 appcelerator,但這里有一個用于 appcelerator titanium 的藍(lán)牙模塊。

TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊
您的客戶端特征配置描述符的 uuid 有誤。它應(yīng)該是 00002902-0000-1000-8000-00805f9b34fb 但你寫了 6E400002-B5A3-F393-E0A9-E50E24DCCA9E。
添加回答
舉報