-
Notifications
You must be signed in to change notification settings - Fork 424
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
Most of the time the connection does not work #445
Comments
Could you paste here the code relevant to how you connect to the device? |
Yes. On build.gradle:
On @Override
public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
// Scanning settings
final ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(0)
.setUseHardwareBatchingIfSupported(false)
.build();
final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.startScan(null, settings, mScanCallback);
if (scanSeconds > 0) {
Thread thread = new Thread() {
private int currentScanSession = scanSessionId.incrementAndGet();
@Override
public void run() {
try {
Thread.sleep(scanSeconds * 1000);
} catch (InterruptedException ignored) {
}
runOnUiThread(new Runnable() {
@Override
public void run() {
BluetoothAdapter btAdapter = getBluetoothAdapter();
// check current scan session was not stopped
if (scanSessionId.intValue() == currentScanSession) {
if(btAdapter.getState() == BluetoothAdapter.STATE_ON) {
scanner.stopScan(mScanCallback);
}
WritableMap map = Arguments.createMap();
bleManager.sendEvent("BleManagerStopScan", map);
}
}
});
}
};
thread.start();
}
callback.invoke();
}
private ScanCallback mScanCallback = new ScanCallback() {
public void onScanResult(final int callbackType, @NonNull final ScanResult result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(bleManager.LOG_TAG, "DiscoverPeripheral: " + result.getDevice().getName());
DanPeripheral peripheral = (DanPeripheral) bleManager.getPeripheral(result.getDevice());
if (peripheral == null) {
peripheral = new DanPeripheral(bleManager.getReactContext(), result);
} else {
peripheral.updateData(result);
peripheral.updateRssi(result.getRssi());
}
bleManager.savePeripheral(peripheral);
WritableMap map = peripheral.asWritableMap();
bleManager.sendEvent("BleManagerDiscoverPeripheral", map);
}
});
}
public void onBatchScanResults(@NonNull final List<no.nordicsemi.android.support.v18.scanner.ScanResult> results) {
}
@Override
public void onScanFailed(final int errorCode) {
WritableMap map = Arguments.createMap();
bleManager.sendEvent("BleManagerStopScan", map);
}
}; On public void connect(Callback callback, Activity activity) {
if (!connected) {
BluetoothDevice device = getDevice();
this.connectCallback = callback;
this.connecting = true;
Handler handler = new Handler(Looper.getMainLooper());
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
Log.d(BleManager.LOG_TAG, "connectGatt(activity, false, this, BluetoothDevice.TRANSPORT_LE, PhyRequest.PHY_LE_1M_MASK | PhyRequest.PHY_LE_2M_MASK | PhyRequest.PHY_LE_CODED_MASK, handler)");
gatt = device.connectGatt(activity, false, this,
BluetoothDevice.TRANSPORT_LE, 1 | 1 << 1 | 1 << 2, handler);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
// A variant of connectGatt with Handled can't be used here.
// Check https://github.com/NordicSemiconductor/Android-BLE-Library/issues/54
// This bug specifically occurs in SDK 26 and is fixed in SDK 27
Log.d(BleManager.LOG_TAG, "connectGatt(activity, false, this, BluetoothDevice.TRANSPORT_LE, PhyRequest.PHY_LE_1M_MASK | PhyRequest.PHY_LE_2M_MASK | PhyRequest.PHY_LE_CODED_MASK)");
gatt = device.connectGatt(activity, false, this,
BluetoothDevice.TRANSPORT_LE, 1 | 1 << 1 | 1 << 2/*, handler*/);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.d(BleManager.LOG_TAG, "connectGatt(activity, false, this, BluetoothDevice.TRANSPORT_LE)");
gatt = device.connectGatt(activity, false, this,
BluetoothDevice.TRANSPORT_LE);
} else {
Log.d(BleManager.LOG_TAG, "connectGatt(activity, false, this)");
gatt = device.connectGatt(activity, false, this);
}
} else {
if (gatt != null) {
callback.invoke();
} else {
callback.invoke("BluetoothGatt is null");
}
}
} |
I see you're not using the BLE library, just the native API directly with some code copied from here. Is it because it didn't work for you? Did you try connecting with just |
Sorry... Follow the code using
...
public class Peripheral extends BluetoothGattCallback {
...
public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, ReactContext reactContext) {
this.device = device;
this.bufferedCharacteristics = new HashMap<String, NotifyBufferContainer>();
this.advertisingRSSI = advertisingRSSI;
this.advertisingDataBytes = scanRecord;
this.danBleManager = new DanBleManager(reactContext);
this.reactContext = reactContext;
}
...
public void connect(Callback callback, Activity activity) {
if (!connected) {
BluetoothDevice device = getDevice();
this.connectCallback = callback;
this.connecting = true;
/* called when the request is about to be executed */
this.danBleManager.connect(this.device)
// Automatic retries are supported, in case of 133 error.
.retry(3 /* times, with */, 100 /* ms interval */)
// A connection timeout can be set. This is additional to the Android's connection timeout which is 30 seconds.
.timeout(15_000 /* ms */)
// The auto connect feature from connectGatt is available as well
.useAutoConnect(true)
// This API can be set on any Android version, but will only be used on devices running Android 8+ with
// support to the selected PHY.
.usePreferredPhy(PhyRequest.PHY_LE_1M_MASK | PhyRequest.PHY_LE_2M_MASK | PhyRequest.PHY_LE_CODED_MASK)
// A connection timeout can be also set. This is additional to the Android's connection timeout which is 30 seconds.
.timeout(15_000 /* ms */)
.then(result -> { Log.d(BleManager.LOG_TAG, "Finished connect to device: " + this.device.getAddress());sendConnectionEvent(device, "BleManagerConnectPeripheral", BluetoothGatt.GATT_SUCCESS);Callback.invoke(); /* called when the request was finished with either success, or a failure */ })
.fail(new FailCallback() {
@Override
public void onRequestFailed(@NonNull BluetoothDevice device, int status) {
Log.d(BleManager.LOG_TAG, "Failed to connect to: " + device.getAddress() + " with status " + status);
sendConnectionEvent(device, "BleManagerDisconnectPeripheral", BluetoothGatt.GATT_FAILURE);
callback.invoke("Connection error.");
}
})
// Each request must be enqueued.
// Kotlin projects can use suspend() or suspendForResult() instead.
// Java projects can also use await() which is blocking.
.enqueue();
} else {
if (connected) {
callback.invoke();
} else {
callback.invoke("BluetoothGatt is null");
}
}
}
...
} |
From the log you posted in the first message it looks like you're getting error 133. This is a generic error, which doesn't say much. What you may try:
.usePreferredPhy(PhyRequest.PHY_LE_1M_MASK | PhyRequest.PHY_LE_2M_MASK | PhyRequest.PHY_LE_CODED_MASK)
|
Hi, I have a question in a similar setup. So we use the blinky example, but modified values of the service uuid and chars to connect to a Nordic NUS peripheral. The blinky calls into this Android-BLE-Library. I've tracked the logcat output, it's making call using only 1M for phy, like:
The question is, in the output of HCI snoop log, it enables both the 2M and Coded phy:
Is this normal or a bug somewhere in the Android library or stack? Is there a way to disable the 2M and Coded bits on this HCI command, where to change the code? I hope there is a way to fix it on the Android side, or at least we know better about it. At the same time, we are also looking into the peripheral side. When using sniffer, I can see the LL_FEATURE_REQ are having coded-phy and 2M bits set. I'm assuming if we change the "LE Extended Create Connection" command parameter on HCI, it may fix the problem. The problem happens on multiple recent phones and tablets using Mediatek chipsets. Any comments or hints will be appreciated. Thanks. |
From our experience Android completely ignores "preferred PHY" and knows better. |
I've been trying to find a solution for this error for two days now and I can't understand what happens to the connection not working. I have to try many, many times until the connection works.
Device: Samsung A13
Android version: 12
Log from logcat:
The text was updated successfully, but these errors were encountered: