Skip to content

Commit 7cfc244

Browse files
committed
Merge pull request #259 from randdusing/wasConnected
wasConnected
2 parents 5fe7af5 + 52f10ed commit 7cfc244

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Neither Android nor iOS support Bluetooth on emulators, so you'll need to test o
132132
* [bluetoothle.isInitialized] (#isinitialized)
133133
* [bluetoothle.isEnabled] (#isenabled)
134134
* [bluetoothle.isScanning] (#isscanning)
135+
* [bluetoothle.wasConnected] (#wasconnected)
135136
* [bluetoothle.isConnected] (#isconnected)
136137
* [bluetoothle.isDiscovered] (#isdiscovered)
137138
* [bluetoothle.hasPermission] (#haspermission) (Android 6+)
@@ -1325,6 +1326,35 @@ bluetoothle.isScanning(isScanning);
13251326

13261327

13271328

1329+
### wasConnected ###
1330+
Determine whether the device was connected, or error if not initialized.
1331+
1332+
```javascript
1333+
bluetoothle.wasConnected(wasConnectedSuccess, wasConnectedError, params);
1334+
```
1335+
1336+
#### Params ####
1337+
* address = The address/identifier provided by the scan's return object
1338+
1339+
```javascript
1340+
{
1341+
"address": "ECC037FD-72AE-AFC5-9213-CA785B3B5C63"
1342+
}
1343+
```
1344+
1345+
##### Success #####
1346+
* status => wasConnected = true/false
1347+
1348+
```javascript
1349+
{
1350+
"name": "Polar H7 3B321015",
1351+
"address": "ECC037FD-72AE-AFC5-9213-CA785B3B5C63",
1352+
"wasConnected": false
1353+
}
1354+
```
1355+
1356+
1357+
13281358
### isConnected ###
13291359
Determine whether the device is connected, or error if not initialized or never connected to device.
13301360

src/android/BluetoothLePlugin.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,13 @@ public void run() {
492492
}
493493
});
494494
return true;
495+
} else if ("wasConnected".equals(action)) {
496+
cordova.getThreadPool().execute(new Runnable() {
497+
public void run() {
498+
wasConnectedAction(args,callbackContext);
499+
}
500+
});
501+
return true;
495502
} else if (isConnectedActionName.equals(action)) {
496503
cordova.getThreadPool().execute(new Runnable() {
497504
public void run() {
@@ -2427,6 +2434,47 @@ private void isScanningAction(CallbackContext callbackContext) {
24272434
callbackContext.success(returnObj);
24282435
}
24292436

2437+
private void wasConnectedAction(JSONArray args, CallbackContext callbackContext) {
2438+
if (isNotInitialized(callbackContext, true)) {
2439+
return;
2440+
}
2441+
2442+
JSONObject obj = getArgsObject(args);
2443+
if (isNotArgsObject(obj, callbackContext)) {
2444+
return;
2445+
}
2446+
2447+
String address = getAddress(obj);
2448+
if (isNotAddress(address, callbackContext)) {
2449+
return;
2450+
}
2451+
2452+
HashMap<Object, Object> connection = connections.get(address);
2453+
if (connection == null) {
2454+
JSONObject returnObj = new JSONObject();
2455+
2456+
addProperty(returnObj, "wasConnected", false);
2457+
2458+
addProperty(returnObj, keyAddress, address);
2459+
2460+
callbackContext.success(returnObj);
2461+
2462+
return;
2463+
}
2464+
2465+
BluetoothGatt bluetoothGatt = (BluetoothGatt)connection.get(keyPeripheral);
2466+
2467+
BluetoothDevice device = bluetoothGatt.getDevice();
2468+
2469+
JSONObject returnObj = new JSONObject();
2470+
2471+
addProperty(returnObj, "wasConnected", true);
2472+
2473+
addDevice(returnObj, device);
2474+
2475+
callbackContext.success(returnObj);
2476+
}
2477+
24302478
private void isConnectedAction(JSONArray args, CallbackContext callbackContext) {
24312479
if (isNotInitialized(callbackContext, true)) {
24322480
return;

src/ios/BluetoothLePlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- (void)isInitialized:(CDVInvokedUrlCommand *)command;
4545
- (void)isEnabled:(CDVInvokedUrlCommand *)command;
4646
- (void)isScanning:(CDVInvokedUrlCommand *)command;
47+
- (void)wasConnected:(CDVInvokedUrlCommand *)command;
4748
- (void)isConnected:(CDVInvokedUrlCommand *)command;
4849
- (void)isDiscovered:(CDVInvokedUrlCommand *)command;
4950
- (void)hasPermission:(CDVInvokedUrlCommand *)command;

src/ios/BluetoothLePlugin.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,55 @@ - (void)isScanning:(CDVInvokedUrlCommand *)command {
18201820
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
18211821
}
18221822

1823+
- (void)wasConnected:(CDVInvokedUrlCommand *)command {
1824+
//Ensure Bluetooth is enabled
1825+
if ([self isNotInitialized:command]) {
1826+
return;
1827+
}
1828+
1829+
//Get the arguments
1830+
NSDictionary* obj = [self getArgsObject:command.arguments];
1831+
if ([self isNotArgsObject:obj :command]) {
1832+
return;
1833+
}
1834+
1835+
//Get the connection address
1836+
NSUUID* address = [self getAddress:obj];
1837+
if ([self isNotAddress:address :command]) {
1838+
return;
1839+
}
1840+
1841+
NSMutableDictionary* connection = [connections objectForKey:address];
1842+
if (connection == nil) {
1843+
//Return wasConnected => false
1844+
NSMutableDictionary* returnObj = [NSMutableDictionary dictionary];
1845+
1846+
[returnObj setValue:address.UUIDString forKey:keyAddress];
1847+
1848+
[returnObj setValue:[NSNumber numberWithBool:false] forKey:@"wasConnected"];
1849+
1850+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:returnObj];
1851+
[pluginResult setKeepCallbackAsBool:false];
1852+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
1853+
1854+
return;
1855+
}
1856+
1857+
//Get the peripheral
1858+
CBPeripheral* peripheral = [connection objectForKey:keyPeripheral];
1859+
1860+
//Return wasConnected => true
1861+
NSMutableDictionary* returnObj = [NSMutableDictionary dictionary];
1862+
1863+
[self addDevice:peripheral :returnObj];
1864+
1865+
[returnObj setValue:[NSNumber numberWithBool:true] forKey:@"wasConnected"];
1866+
1867+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:returnObj];
1868+
[pluginResult setKeepCallbackAsBool:false];
1869+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
1870+
}
1871+
18231872
- (void)isConnected:(CDVInvokedUrlCommand *)command {
18241873
//Ensure Bluetooth is enabled
18251874
if ([self isNotInitialized:command]) {

src/osx/BluetoothLePlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- (void)isInitialized:(CDVInvokedUrlCommand *)command;
4545
- (void)isEnabled:(CDVInvokedUrlCommand *)command;
4646
- (void)isScanning:(CDVInvokedUrlCommand *)command;
47+
- (void)wasConnected:(CDVInvokedUrlCommand *)command;
4748
- (void)isConnected:(CDVInvokedUrlCommand *)command;
4849
- (void)isDiscovered:(CDVInvokedUrlCommand *)command;
4950
- (void)hasPermission:(CDVInvokedUrlCommand *)command;

src/osx/BluetoothLePlugin.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,55 @@ - (void)isScanning:(CDVInvokedUrlCommand *)command {
18201820
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
18211821
}
18221822

1823+
- (void)wasConnected:(CDVInvokedUrlCommand *)command {
1824+
//Ensure Bluetooth is enabled
1825+
if ([self isNotInitialized:command]) {
1826+
return;
1827+
}
1828+
1829+
//Get the arguments
1830+
NSDictionary* obj = [self getArgsObject:command.arguments];
1831+
if ([self isNotArgsObject:obj :command]) {
1832+
return;
1833+
}
1834+
1835+
//Get the connection address
1836+
NSUUID* address = [self getAddress:obj];
1837+
if ([self isNotAddress:address :command]) {
1838+
return;
1839+
}
1840+
1841+
NSMutableDictionary* connection = [connections objectForKey:address];
1842+
if (connection == nil) {
1843+
//Return wasConnected => false
1844+
NSMutableDictionary* returnObj = [NSMutableDictionary dictionary];
1845+
1846+
[returnObj setValue:address.UUIDString forKey:keyAddress];
1847+
1848+
[returnObj setValue:[NSNumber numberWithBool:false] forKey:@"wasConnected"];
1849+
1850+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:returnObj];
1851+
[pluginResult setKeepCallbackAsBool:false];
1852+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
1853+
1854+
return;
1855+
}
1856+
1857+
//Get the peripheral
1858+
CBPeripheral* peripheral = [connection objectForKey:keyPeripheral];
1859+
1860+
//Return wasConnected => true
1861+
NSMutableDictionary* returnObj = [NSMutableDictionary dictionary];
1862+
1863+
[self addDevice:peripheral :returnObj];
1864+
1865+
[returnObj setValue:[NSNumber numberWithBool:true] forKey:@"wasConnected"];
1866+
1867+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:returnObj];
1868+
[pluginResult setKeepCallbackAsBool:false];
1869+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
1870+
}
1871+
18231872
- (void)isConnected:(CDVInvokedUrlCommand *)command {
18241873
//Ensure Bluetooth is enabled
18251874
if ([self isNotInitialized:command]) {

www/bluetoothle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ var bluetoothle = {
8181
isScanning: function(successCallback) {
8282
cordova.exec(successCallback, successCallback, bluetoothleName, "isScanning", []);
8383
},
84+
wasConnected: function(successCallback, errorCallback, params) {
85+
cordova.exec(successCallback, errorCallback, bluetoothleName, "wasConnected", [params]);
86+
},
8487
isConnected: function(successCallback, errorCallback, params) {
8588
cordova.exec(successCallback, errorCallback, bluetoothleName, "isConnected", [params]);
8689
},

0 commit comments

Comments
 (0)