0

In Delphi FMX 10.3, I can scan for a BLE device and can connect to it, and can also receive data, but when sending data more than 20 bytes, the app cannot receive data. I know modifying the MTU to greater than 20 will work ok.

In Android Studio, it is easy to implement, but in Delphi FMX I cannot find any support.

Android Studio sample:

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!");
                    }
                }
            });
        }
    });
}

But in Delphi, how to implement this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Heimi
  • 1
  • The `BlueToothManager` is declared in `AndroidApi.JNI.Bluetooth.pas`. If you `uses` this unit in your project, you should be able to write the equivalent Delphi code with a little effort. – Freddie Bell Jul 04 '20 at 07:55
  • Thanks Very much , but i can't find AndroidApi.JNI.Bluetooth.pas in delphi , I use TBuleToothLB to connect BLE Deveice – Heimi Jul 05 '20 at 13:40
  • It's in `C:\Program Files (x86)\Embarcadero\Studio\20.0\source\rtl\android` if you have 10.3. I don't know anything about TBuleToothLB, sorry. – Freddie Bell Jul 05 '20 at 15:09
  • 1
    OK, Thanks for your help! – Heimi Jul 07 '20 at 06:41

1 Answers1

0

As far as I understand, 20 bytes is the maximum packet size:

"BLE allow you transfer maximum is 20 bytes."

"You are correct that the BLE specification doesn't allow write operations to exceed 20 bytes."

Maximum packet length for Bluetooth LE? - Stack Overflow

I would look at connection type.

GATT describes in detail how attributes (data) are transferred once devices have a dedicated connection.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Winston
  • 38
  • 6
  • This is not entirely correct. As the answer to the question you posted states: "Using packet length extension introduced in Bluetooth 4.2: Up to 251 bytes at the radio level (255 with MIC), so 242 bytes available for attribute data." – Michael Kotzjan Aug 06 '21 at 04:52
  • 1
    Thanks for clarifying that Michael. Seems like I was using outdated references. Apologies to all concerned. – Winston Sep 15 '22 at 14:25