Skip to content

Commit 057c197

Browse files
author
silviucm
authored
implement getAdapterInfo (Android only)
getAdapterInfo allows an easy way to check the state of the adapter and retrieve the address and the name even when it had not been initialized yet
1 parent 1e4681e commit 057c197

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

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, bluetoothAdapter.isDiscovering());
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;

0 commit comments

Comments
 (0)