Skip to content

Commit 2ea0c8b

Browse files
authored
Merge pull request #607 from mattkhaw/master
Fixed bluetooth state changes for peripheral
2 parents 4e24262 + 4382eb0 commit 2ea0c8b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/android/BluetoothLePlugin.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,7 @@ private void initializePeripheralAction(JSONArray args, CallbackContext callback
440440

441441
initPeripheralCallback = callbackContext;
442442

443-
//Re-opening Gatt server seems to cause some issues
444-
if (gattServer == null) {
445-
Activity activity = cordova.getActivity();
446-
BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
447-
gattServer = bluetoothManager.openGattServer(activity.getApplicationContext(), bluetoothGattServerCallback);
448-
}
443+
initGattServer();
449444

450445
JSONObject returnObj = new JSONObject();
451446
addProperty(returnObj, keyStatus, statusEnabled);
@@ -2790,19 +2785,30 @@ public void onReceive(Context context, Intent intent) {
27902785
}
27912786
scanCallbackContext = null;
27922787

2788+
gattServer = null;
2789+
27932790
pluginResult = new PluginResult(PluginResult.Status.OK, returnObj);
27942791
pluginResult.setKeepCallback(true);
27952792
initCallbackContext.sendPluginResult(pluginResult);
27962793

2794+
if (initPeripheralCallback != null) initPeripheralCallback.sendPluginResult(pluginResult);
2795+
2796+
break;
2797+
case BluetoothAdapter.STATE_TURNING_OFF:
2798+
gattServer.close();
27972799
break;
27982800
case BluetoothAdapter.STATE_ON:
27992801

28002802
addProperty(returnObj, keyStatus, statusEnabled);
28012803

2804+
initGattServer();
2805+
28022806
pluginResult = new PluginResult(PluginResult.Status.OK, returnObj);
28032807
pluginResult.setKeepCallback(true);
28042808
initCallbackContext.sendPluginResult(pluginResult);
28052809

2810+
if (initPeripheralCallback != null) initPeripheralCallback.sendPluginResult(pluginResult);
2811+
28062812
break;
28072813
}
28082814
}
@@ -4536,4 +4542,13 @@ public void onServiceAdded(int status, BluetoothGattService service) {
45364542
}
45374543
}
45384544
};
4545+
4546+
private void initGattServer() {
4547+
//Re-opening Gatt server seems to cause some issues
4548+
if (gattServer == null) {
4549+
Activity activity = cordova.getActivity();
4550+
BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
4551+
gattServer = bluetoothManager.openGattServer(activity.getApplicationContext(), bluetoothGattServerCallback);
4552+
}
4553+
}
45394554
}

0 commit comments

Comments
 (0)