Skip to content

Commit 65d5892

Browse files
authored
Merge pull request #647 from p-andreas/master
android: fixed connection failed with status code 133
2 parents a7e710a + 3d50691 commit 65d5892

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/android/BluetoothLePlugin.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,10 +1477,15 @@ private void connectAction(JSONArray args, CallbackContext callbackContext) {
14771477
if (obj != null) {
14781478
autoConnect = obj.optBoolean("autoConnect", false);
14791479
}
1480-
14811480
connections.put(device.getAddress(), connection);
14821481

1483-
BluetoothGatt bluetoothGatt = device.connectGatt(cordova.getActivity().getApplicationContext(), autoConnect, bluetoothGattCallback);
1482+
BluetoothGatt bluetoothGatt = null;
1483+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
1484+
int transportMode = getTransportMode(obj);
1485+
bluetoothGatt = device.connectGatt(cordova.getActivity().getApplicationContext(), autoConnect, bluetoothGattCallback, transportMode);
1486+
} else {
1487+
bluetoothGatt = device.connectGatt(cordova.getActivity().getApplicationContext(), autoConnect, bluetoothGattCallback);
1488+
}
14841489

14851490
connection.put(keyPeripheral, bluetoothGatt);
14861491
}
@@ -3653,6 +3658,23 @@ private UUID[] getServiceUuids(JSONObject obj) {
36533658
return uuids;
36543659
}
36553660

3661+
3662+
private int getTransportMode(JSONObject obj) {
3663+
int transportMode = 0;
3664+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
3665+
transportMode = BluetoothDevice.TRANSPORT_AUTO;
3666+
}
3667+
3668+
if (obj != null && !obj.isNull("transport")) {
3669+
try {
3670+
transportMode = obj.getInt("transport");
3671+
} catch (JSONException e) {
3672+
Log.e("BLE", "An exception occurred while transport connection parameter, fall back to: BluetoothDevice.TRANSPORT_AUTO");
3673+
}
3674+
}
3675+
return transportMode;
3676+
}
3677+
36563678
private String getAddress(JSONObject obj) {
36573679
//Get the address string from arguments
36583680
String address = obj.optString(keyAddress, null);

types/index.d.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ declare namespace BluetoothlePlugin {
103103
* Connect to a Bluetooth LE device
104104
* @param connectSuccess The success callback that is passed with device object
105105
* @param connectError The callback that will be triggered when the connect operation fails
106-
* @param params The address/identifier
107106
*
107+
* @param params connection params
108108
*/
109109
connect(
110110
connectSuccess: (status: DeviceInfo) => void,
111111
connectError: (error: Error) => void,
112-
params: { address: string, autoConnect?: boolean }): void;
112+
params: ConnectionParams): void;
113113

114114
/**
115115
* Reconnect to a previously connected Bluetooth device
@@ -619,6 +619,36 @@ declare namespace BluetoothlePlugin {
619619
isConnectable?: boolean
620620
}
621621

622+
interface ConnectionParams{
623+
address: string;
624+
autoConnect?: boolean;
625+
/**
626+
* Transport mode. Available from API 23 (Android).
627+
* If none is specified the default behavior is TRANSPORT_AUTO
628+
*
629+
* Note: On Android 10, TRANSPORT_AUTO can lead to connection errors with Status code 133.
630+
* In this case TRANSPORT_LE can be used.
631+
*/
632+
transport?: AndroidGattTransportMode;
633+
}
634+
635+
enum AndroidGattTransportMode{
636+
/**
637+
* No preference of physical transport for GATT connections to remote dual-mode devices
638+
*/
639+
TRANSPORT_AUTO = 0,
640+
641+
/**
642+
* Prefer BR/EDR transport for GATT connections to remote dual-mode devices
643+
*/
644+
TRANSPORT_BREDR = 1,
645+
646+
/**
647+
* Prefer LE transport for GATT connections to remote dual-mode devices
648+
*/
649+
TRANSPORT_LE = 2,
650+
}
651+
622652
interface NotifyParams {
623653
/** Service's UUID */
624654
service: string,

0 commit comments

Comments
 (0)