Skip to content

Commit 81b8ae4

Browse files
Android M optimisation
1 parent c8bd965 commit 81b8ae4

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

bluetooth.android.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ Bluetooth._disconnect = function(gatt) {
471471
// note that this doesn't make much sense without scanning first
472472
Bluetooth.connect = function (arg) {
473473
return new Promise(function (resolve, reject) {
474-
try {
474+
try {
475475
// or macaddress..
476476
if (!arg.UUID) {
477477
reject("No UUID was passed");
@@ -482,20 +482,30 @@ Bluetooth.connect = function (arg) {
482482
reject("Could not find peripheral with UUID " + arg.UUID);
483483
} else {
484484
console.log("Connecting to peripheral with UUID: " + arg.UUID);
485-
var bluetoothGatt = bluetoothDevice.connectGatt(
486-
utils.ad.getApplicationContext(), // context
487-
false, // autoconnect
488-
new Bluetooth._MyGattCallback(/* TODO pass in onWhatever function */)
489-
);
490-
// TODO (fill, and use this object in disconnect() as well)
485+
486+
var bluetoothGatt;
487+
if (android.os.Build.VERSION.SDK_INT < 23 /*android.os.Build.VERSION_CODES.M */) {
488+
bluetoothGatt = bluetoothDevice.connectGatt(
489+
utils.ad.getApplicationContext(), // context
490+
false, // autoconnect
491+
new Bluetooth._MyGattCallback(/* TODO pass in onWhatever function */)
492+
);
493+
} else {
494+
bluetoothGatt = bluetoothDevice.connectGatt(
495+
utils.ad.getApplicationContext(), // context
496+
false, // autoconnect
497+
new Bluetooth._MyGattCallback(/* TODO pass in onWhatever function */),
498+
android.bluetooth.BluetoothDevice.TRANSPORT_LE // 2
499+
);
500+
}
501+
491502
Bluetooth._connections[arg.UUID] = {
492503
state: 'connecting',
493504
onConnected: arg.onConnected,
494505
onDisconnected: arg.onDisconnected,
495506
device: bluetoothGatt // TODO rename device to gatt?
496507
};
497508
}
498-
// resolve();
499509
} catch (ex) {
500510
console.log("Error in Bluetooth.connect: " + ex);
501511
reject(ex);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-bluetooth",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description" : "Connect to and interact with Bluetooth LE peripherals",
55
"main" : "bluetooth.js",
66
"nativescript": {

0 commit comments

Comments
 (0)