79106580

Date: 2024-10-20 07:40:49
Score: 3
Natty:
Report link

how do it work for yours?

long _fixLastScan = 0;
String _fixTargetAddress = null;

final ScanCallback _fixCallback = new ScanCallback() {
    @SuppressLint("MissingPermission")
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        BluetoothDevice seek = result.getDevice();
        String addr1 = seek.getAddress();
        if (addr1 != null) {
            if (addr1.equals(_fixTargetAddress)) {
                debug("trying fix Reconnect " + _fixScanner  + " seeking " + addr1);
                seek.connectGatt(context, false, BleMidiCentralCallback.this).connect();
            }
        }
    }
};

BluetoothManager _fixBluetoothManager = null;
BluetoothAdapter _fixbluetoothAdapter = null;
BluetoothLeScanner _fixScanner = null;
List<ScanFilter> _fixFilters = null;

@SuppressLint("MissingPermission")
public void stopFixScan() {
    debug("stop fix Reconnect");

    if (_fixScanner != null) {
        _fixScanner.stopScan(_fixCallback);
        _fixScanner = null;
    }
}

public void reconnect(BluetoothDevice device) throws  SecurityException{
    _fixTargetAddress = device.getAddress();
    debug("need fix Reconnect");

    if (_fixBluetoothManager == null || _fixScanner == null) {
        _fixBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        _fixbluetoothAdapter = _fixBluetoothManager.getAdapter();
        _fixScanner = _fixbluetoothAdapter.getBluetoothLeScanner();
        _fixFilters = BleMidiDeviceUtils.getBleMidiScanFilters(context);
    }

    if (_fixLastScan != 0 && _fixLastScan + 30000 > System.currentTimeMillis()) {
        //nothing
    }else if (_fixLastScan != 0) {
        _fixScanner.stopScan(_fixCallback);
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            reconnect(device);
        }, 1000);
        _fixLastScan = 0;
    }else {
        _fixLastScan = System.currentTimeMillis();

        ScanSettings scanSettings = new ScanSettings.Builder()
                .setLegacy(false)
                .setScanMode(ScanSettings.SCAN_MODE_BALANCED)
                .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
                .build();

        _fixScanner.startScan(_fixFilters, scanSettings, _fixCallback);
    }
}

private volatile static Object gattDiscoverServicesLock = null;
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) throws SecurityException {
    debug( "onConnectoinStateChange " + status + " -> " + newState);
    // In this method, the `status` parameter shall be ignored.
    // so, look `newState` parameter only.
    if ((status == 133 && newState == 0) || (newState == 8)) {//both timeout
        gatt.close();
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            BluetoothDevice device = gatt.getDevice();
            reconnect(device);
        }, 5000);
        return;
    }

    if (newState == BluetoothProfile.STATE_CONNECTED) {
        stopFixScan();
        //do something
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
    }
}
Reasons:
  • Blacklisted phrase (1): how do i
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): how do it
  • Low reputation (1):
Posted by: SynthTAROU