Skip to content

Commit a4880ff

Browse files
authored
Merge pull request #430 from silviucm/master
Add getAdapterInfo for Android
2 parents 1e4681e + 4bfff44 commit a4880ff

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Neither Android nor iOS support Bluetooth on emulators, so you'll need to test o
111111
* [bluetoothle.initialize] (#initialize)
112112
* [bluetoothle.enable] (#enable) (Android)
113113
* [bluetoothle.disable] (#disable) (Android)
114+
* [bluetoothle.getAdapterInfo] (#getAdapterInfo) (Android)
114115
* [bluetoothle.startScan] (#startscan)
115116
* [bluetoothle.stopScan] (#stopscan)
116117
* [bluetoothle.retrieveConnected] (#retrieveconnected)
@@ -291,6 +292,27 @@ bluetoothle.disable(disableSuccess, disableError);
291292
The successCallback isn't actually used. Listen to initialize callbacks for change in Bluetooth state. A successful disable will return an error => enable via initialize error callback.
292293

293294

295+
### getAdapterInfo ###
296+
Retrieve useful information such as the address, name, and various states (initialized, enabled, scanning, discoverable).
297+
This can be very useful when the general state of the adapter has been lost, and we would otherwise need to go through a series of callbacks to get the correct state (first initialized, then enabled, then isScanning, and so forth).
298+
The result of this method allows us to take business logic decisions while avoiding a large part of the callback hell.
299+
300+
Currently the discoverable state does not have any relevance because there is no "setDiscoverable" functionality in place.
301+
That may change in the future.
302+
303+
```javascript
304+
bluetoothle.getAdapterInfo(successCallback);
305+
```
306+
307+
##### Success #####
308+
The successCallback contains the following properties:
309+
* name = the adapters's display name
310+
* address = the adapter's address
311+
* isInitialized = boolean value, true if the adapter was initialized, otherwise false
312+
* isEnabled = boolean value, true if the adapter was enabled, otherwise false
313+
* isScanning = boolean value, true if the adapter is currently scanning, otherwise false
314+
* isDiscoverable = boolean value, true if the adapter is in discoverable mode, otherwise false (currently largely false)
315+
294316

295317
### startScan ###
296318
Scan for Bluetooth LE devices. Since scanning is expensive, stop as soon as possible. The Cordova app should use a timer to limit the scan interval. Also, Android uses an AND operator for filtering, while iOS uses an OR operator. Android API >= 23 requires ACCESS_COARSE_LOCATION permissions to find unpaired devices. Permissions can be requested by using the hasPermission and requestPermission functions. Android API >= 23 also requires location services to be enabled. Use ```isLocationEnabled``` to determine whether location services are enabled. If not enabled, use ```requestLocation``` to prompt the location services settings page.

src/android/BluetoothLePlugin.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public class BluetoothLePlugin extends CordovaPlugin {
123123
private final String keyIsBonded = "isBonded";
124124
private final String keyIsConnected = "isConnected";
125125
private final String keyIsDiscovered = "isDiscovered";
126+
private final String keyIsDiscoverable = "isDiscoverable";
126127
private final String keyPeripheral = "peripheral";
127128
private final String keyState = "state";
128129
private final String keyDiscoveredState = "discoveredState";
@@ -309,6 +310,8 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
309310
initializeAction(args, callbackContext);
310311
} else if ("enable".equals(action)) {
311312
enableAction(callbackContext);
313+
} else if ("getAdapterInfo".equals(action)) {
314+
getAdapterInfoAction(callbackContext);
312315
} else if ("disable".equals(action)) {
313316
disableAction(callbackContext);
314317
} else if ("startScan".equals(action)) {
@@ -1008,6 +1011,47 @@ private void initializeAction(JSONArray args, CallbackContext callbackContext) {
10081011
}
10091012
}
10101013

1014+
1015+
/**
1016+
* Retrieves a minimal set of adapter details
1017+
* (address, name, initialized state, enabled state, scanning state, discoverable state)
1018+
*/
1019+
private void getAdapterInfoAction(CallbackContext callbackContext) {
1020+
JSONObject returnObj = new JSONObject();
1021+
1022+
// Not yet initialized
1023+
if (bluetoothAdapter == null) {
1024+
Activity activity = cordova.getActivity();
1025+
BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
1026+
BluetoothAdapter bluetoothAdapterTmp = bluetoothManager.getAdapter();
1027+
1028+
// Since the adapter is not officially initialized, retrieve only the address and the name from the temp ad-hoc adapter
1029+
addProperty(returnObj, keyAddress, bluetoothAdapterTmp.getAddress());
1030+
addProperty(returnObj, keyName, bluetoothAdapterTmp.getName());
1031+
addProperty(returnObj, keyIsInitialized, false);
1032+
addProperty(returnObj, keyIsEnabled, false);
1033+
addProperty(returnObj, keyIsScanning, false);
1034+
addProperty(returnObj, keyIsDiscoverable, false);
1035+
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, returnObj);
1036+
pluginResult.setKeepCallback(true);
1037+
callbackContext.sendPluginResult(pluginResult);
1038+
return;
1039+
} else {
1040+
// Already initialized, so use the bluetoothAdapter class property to get all the info
1041+
addProperty(returnObj, keyAddress, bluetoothAdapter.getAddress());
1042+
addProperty(returnObj, keyName, bluetoothAdapter.getName());
1043+
addProperty(returnObj, keyIsInitialized, true);
1044+
addProperty(returnObj, keyIsEnabled, bluetoothAdapter.isEnabled());
1045+
addProperty(returnObj, keyIsScanning, (scanCallbackContext != null));
1046+
addProperty(returnObj, keyIsDiscoverable, bluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
1047+
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, returnObj);
1048+
pluginResult.setKeepCallback(true);
1049+
callbackContext.sendPluginResult(pluginResult);
1050+
return;
1051+
}
1052+
1053+
}
1054+
10111055
private void enableAction(CallbackContext callbackContext) {
10121056
if (isNotInitialized(callbackContext, false)) {
10131057
return;

www/bluetoothle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var bluetoothle = {
99
disable: function(successCallback, errorCallback) {
1010
cordova.exec(successCallback, errorCallback, bluetoothleName, "disable", []);
1111
},
12+
getAdapterInfo: function(successCallback) {
13+
cordova.exec(successCallback, successCallback, bluetoothleName, "getAdapterInfo", []);
14+
},
1215
startScan: function(successCallback, errorCallback, params) {
1316
cordova.exec(successCallback, errorCallback, bluetoothleName, "startScan", [params]);
1417
},

0 commit comments

Comments
 (0)