Skip to content

Commit 3d50691

Browse files
committed
updated gatt transport params
1 parent f720da0 commit 3d50691

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/android/BluetoothLePlugin.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,12 +1477,12 @@ 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

14831482
BluetoothGatt bluetoothGatt = null;
14841483
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
1485-
bluetoothGatt = device.connectGatt(cordova.getActivity().getApplicationContext(), autoConnect, bluetoothGattCallback, BluetoothDevice.TRANSPORT_LE);
1484+
int transportMode = getTransportMode(obj);
1485+
bluetoothGatt = device.connectGatt(cordova.getActivity().getApplicationContext(), autoConnect, bluetoothGattCallback, transportMode);
14861486
} else {
14871487
bluetoothGatt = device.connectGatt(cordova.getActivity().getApplicationContext(), autoConnect, bluetoothGattCallback);
14881488
}
@@ -3658,6 +3658,23 @@ private UUID[] getServiceUuids(JSONObject obj) {
36583658
return uuids;
36593659
}
36603660

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+
36613678
private String getAddress(JSONObject obj) {
36623679
//Get the address string from arguments
36633680
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)