Skip to content

Commit 7076cc0

Browse files
committed
fix(ios): fix for wrong first isEnabled state
1 parent 9ec5826 commit 7076cc0

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/bluetooth.ios.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,10 @@ export class Bluetooth extends BluetoothCommon {
660660
// doing nothing on ios
661661
}
662662

663-
_state: CBManagerState;
663+
_state: CBManagerState = CBManagerState.Unsupported;
664664
set state(state: CBManagerState) {
665665
if (this._state !== state) {
666+
console.log('on ble state change', state);
666667
this._state = state;
667668
this.sendEvent(BluetoothCommon.bluetooth_status_event, {
668669
state: state === CBManagerState.Unsupported ? 'unsupported' : state === CBManagerState.PoweredOn ? 'on' : 'off',
@@ -679,20 +680,24 @@ export class Bluetooth extends BluetoothCommon {
679680
}
680681
return this._centralDelegate;
681682
}
682-
get centralManager() {
683+
684+
ensureCentralManager(){
683685
if (!this._centralManager) {
684686
const options: NSMutableDictionary<any, any> = new (NSMutableDictionary as any)([this.showPowerAlertPopup], [CBCentralManagerOptionShowPowerAlertKey]);
685687
if (this.restoreIdentifier) {
686688
options.setObjectForKey(this.restoreIdentifier, CBCentralManagerOptionRestoreIdentifierKey);
687689
}
688690
this._centralManager = CBCentralManager.alloc().initWithDelegateQueueOptions(this.centralDelegate, null, options);
689-
setTimeout(() => {
690-
this.state = this._centralManager.state;
691-
}, 100);
691+
// setTimeout(() => {
692+
// this.state = this._centralManager.state;
693+
// }, 100);
692694
if (Trace.isEnabled()) {
693695
CLog(CLogTypes.info, `this._centralManager: ${this._centralManager}`);
694696
}
695697
}
698+
}
699+
get centralManager() {
700+
this.ensureCentralManager();
696701
return this._centralManager;
697702
}
698703

@@ -706,8 +711,8 @@ export class Bluetooth extends BluetoothCommon {
706711
CLog(CLogTypes.info, 'onListenerAdded', eventName, count);
707712
}
708713
if (eventName === Bluetooth.bluetooth_status_event) {
709-
// ensure centralManager is set
710-
const result = this.centralManager;
714+
// ensure centralManager is set to have status event
715+
this.ensureCentralManager();
711716
}
712717
}
713718

@@ -762,19 +767,16 @@ export class Bluetooth extends BluetoothCommon {
762767
this._connectedPeripherals[UUID] = peripheral;
763768
}
764769

765-
// needed for consecutive calls to isBluetoothEnabled. Until readyToAskForEnabled, everyone waits!
766-
readyToAskForEnabled = false;
767770
public async isBluetoothEnabled() {
768-
if (!this.readyToAskForEnabled) {
769-
// the centralManager return wrong state just after initialization
770-
// so create it and wait a bit
771-
// eslint-disable-next-line no-unused-expressions
772-
this.centralManager;
771+
if (this._state === CBManagerState.Unsupported ) {
773772
if (Trace.isEnabled()) {
774-
CLog(CLogTypes.info, 'isBluetoothEnabled', 'waiting a bit');
773+
CLog(CLogTypes.info, 'isBluetoothEnabled', 'central manager not ready, waiting for it to start');
775774
}
776-
await new Promise((resolve) => setTimeout(resolve, 500));
777-
this.readyToAskForEnabled = true;
775+
return new Promise<boolean>((resolve) => {
776+
this.once(BluetoothCommon.bluetooth_status_event, ()=> {
777+
resolve(this._isEnabled());
778+
});
779+
});
778780
}
779781
return this._isEnabled();
780782
}

0 commit comments

Comments
 (0)