Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

混雑する信号でBLEのパケットが大きすぎる問題 #40

Open
syntaro opened this issue Oct 9, 2024 · 0 comments
Open

混雑する信号でBLEのパケットが大きすぎる問題 #40

syntaro opened this issue Oct 9, 2024 · 0 comments

Comments

@syntaro
Copy link

syntaro commented Oct 9, 2024

ご参考にしていただけましたら。

つまり、詰まっている場合の処理を変更しました。
パケット数に制限をつけることで、遅延こそあれ、ロストが起きなくしました。

MidiOutputDevice.java
``

final Thread transferDataThread = new Thread(new Runnable() {
    @Override
    public void run() {
        transferDataThreadAlive = true;

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        while (transferDataThreadAlive) {
            stream.reset();
            PacketData packet = _queue.pop();
            packet._time = 0;

            stream.write((byte) (0x80 | ((packet._time >> 7) & 0x3f)));
            stream.write((byte) (0x80 | (packet._time & 0x7f)));
            stream.write(packet._data, packet._offset, packet._count);
            boolean sysex1 = (packet._data[packet._offset] & 0xff) == 0xf0;
            _packetGenerator.markRecyled(packet);
            int poped = 1;

            if (sysex1) {
                transferData(stream.toByteArray());
            }
            else {
                while (!_queue.isEmpty()) {
                    packet = _queue.pop();
                    boolean sysex2 = (packet._data[packet._offset] & 0xff) == 0xf0;
                    if (sysex2) {
                        _queue.back(packet);
                        break;
                    }
                    poped ++;
                    if (poped >= 7) {
                        _queue.back(packet);
                        break;
                    }
                    packet._time = 0;
                    stream.write((byte) (0x80 | (packet._time & 0x7f)));
                    stream.write(packet._data, packet._offset, packet._count);
                    _packetGenerator.markRecyled(packet);
                    packet = null;
                }
                transferData(stream.toByteArray());
            }
            if (poped >= 4) {
                Log.e(TAG, "Poped " + poped);
            }
        }
    }
});

``

BleMidiCallback.java

``

   @Override
    public boolean transferData(@NonNull byte[] writeBuffer) throws SecurityException {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                int result = bluetoothGatt.writeCharacteristic(midiOutputCharacteristic, writeBuffer, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                if (result == 201) {
                    int retry = 0;
                    do {
                        try {
                            Thread.sleep(20);
                        }catch (InterruptedException ex) {
                        }
                        ++ retry;
                        if (retry >= 2) {
                            Log.e(Constants.TAG, "201 retry " +  retry);
                        }
                        if (retry >= 100) {
                            break;
                        }
                        result = bluetoothGatt.writeCharacteristic(midiOutputCharacteristic, writeBuffer, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                        if (result != 0 && result != 201) {
                            Log.e(Constants.TAG, "*** " + result + " retry " +  retry);
                        }
                    }while(result == 201);
                }
                return result == BluetoothStatusCodes.SUCCESS;
            } else {
                midiOutputCharacteristic.setValue(writeBuffer);
                boolean result = bluetoothGatt.writeCharacteristic(midiOutputCharacteristic);
                if (result != true) {
                    Log.e(Constants.TAG, "transferData 2 Error "+ result);
                }
                return result;
            }
        } catch (Throwable ignored) {
            // android.os.DeadObjectException will be thrown
            // ignore it
            return false;
        }
    }

``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant