【问题标题】:How can Android BLE read more than 20 bytes in Delphi FMX 10.3 API is(API 29)Android BLE 如何在 Delphi FMX 10.3 API is(API 29) 中读取超过 20 个字节
【发布时间】:2020-07-04 02:20:56
【问题描述】:

在Delphi FMX 10.3中,我可以扫描BLE设备并可​​以连接,也可以接收数据,但是当发送数据超过20字节时,应用程序无法接收数据。我知道将 MTU 修改为大于 20 就可以了。

在 Android Studio 中,很容易实现,但在 Delphi FMX 中我找不到任何支持。

Android Studio 示例:

private void setMtu(int setMtu) {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            device.connectGatt(DemoActivity.this, true, new BluetoothGattCallback() {
                @Override
                public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                    super.onServicesDiscovered(gatt, status);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        if (setMtu > 23 && setMtu < 512) {
                            gatt.requestMtu(setMtu);
                        }
                    }
                }

                @Override
                public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
                    super.onMtuChanged(gatt, mtu, status);
                    mMtu = mtu;
                    if (BluetoothGatt.GATT_SUCCESS == status && setMtu == mtu) {
                        LogUtils.d("MTU change success = " + mtu);
                    } else {
                        LogUtils.d("MTU change fail!");
                    }
                }
            });
        }
    });
}

但是在Delphi中,如何实现呢?

【问题讨论】:

  • BlueToothManagerAndroidApi.JNI.Bluetooth.pas 中声明。如果你在你的项目中uses这个单元,你应该可以轻松编写等效的Delphi代码。
  • 非常感谢,但是我在delphi中找不到AndroidApi.JNI.Bluetooth.pas,我用TBuleToothLB连接BLE设备
  • 如果你有 10.3,它在 C:\Program Files (x86)\Embarcadero\Studio\20.0\source\rtl\android。我对 TBuleToothLB 一无所知,抱歉。
  • 好的,感谢您的帮助!

标签: delphi bluetooth-lowenergy firemonkey mtu


【解决方案1】:

据我了解,20字节是最大数据包大小:

"BLE 允许您传输的最大值为 20 字节。"

“您说得对,BLE 规范不允许写操作超过 20 个字节。”

Maximum packet length for Bluetooth LE? - 堆栈内存溢出

我会查看连接类型。

GATT 详细描述了设备具有专用连接后如何传输属性(数据)。

【讨论】:

  • 这并不完全正确。正如您发布的问题的答案所述:“使用蓝牙 4.2 中引入的数据包长度扩展:无线电级别最多 251 个字节(MIC 为 255 个),因此 242 个字节可用于属性数据。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 2020-09-22
  • 2017-04-08
  • 1970-01-01
相关资源
最近更新 更多