-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bluetooth/Usb automatic reconnection #516
Bluetooth/Usb automatic reconnection #516
Conversation
Update reader status while reconnecting fail.
….com/EricLin-BBpos/stripe-terminal-react-native into Bluetooth-USB-automatic-reconnection
src/types/index.ts
Outdated
@@ -35,11 +35,13 @@ export type GetLocationsParams = { | |||
export type ConnectBluetoothReaderParams = { | |||
reader: Reader.Type; | |||
locationId?: string; | |||
autoReconnect: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this param is named autoReconnectOnUnexpectedDisconnect
on the native SDKs, can we stick to that here as well?
src/types/index.ts
Outdated
}; | ||
|
||
export type ConnectUsbReaderParams = { | ||
reader: Reader.Type; | ||
locationId?: string; | ||
autoReconnect: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
src/hooks/useStripeTerminal.tsx
Outdated
}, [onDidStartReaderReconnect]); | ||
|
||
const terminalDidSucceedReaderReconnect = useCallback(() => { | ||
console.log('Eric userStripeTerminal terminalDidSucceedReaderReconnect'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
src/hooks/useStripeTerminal.tsx
Outdated
}, [onTerminalDidSucceedReaderReconnect]); | ||
|
||
const terminalDidFailReaderReconnect = useCallback(() => { | ||
console.log('Eric userStripeTerminal terminalDidFailReaderReconnect'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
const didStartReaderReconnect = useCallback(() => { | ||
log('didStartReaderReconnect'); | ||
emitter?.emit(START_READER_RECONNECT); | ||
}, [log]); | ||
|
||
const terminalDidSucceedReaderReconnect = useCallback(() => { | ||
log('terminalDidSucceedReaderReconnect'); | ||
emitter?.emit(TERMINAL_SUCCEED_READER_RECONNECT); | ||
}, [log]); | ||
|
||
const terminalDidFailReaderReconnect = useCallback(() => { | ||
log('terminalDidFailReaderReconnect'); | ||
emitter?.emit(TERMINAL_FAIL_READER_RECONNECT); | ||
}, [log]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should there be results emitted here?
dev-app/src/util/merchantStorage.ts
Outdated
export const setEnableAutoReconnect = async (autoReconnection: boolean) => | ||
await AsyncStorage.setItem( | ||
AUTOMATIC_RECONNECTION_KEY, | ||
JSON.stringify(autoReconnection) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asyncStorage seems like a bit of an overkill to persist this. maybe we can leverage AppContext instead https://github.com/stripe/stripe-terminal-react-native/blob/4bfa8622/dev-app/src/AppContext.ts#L8-L14
ios/StripeTerminalReactNative.swift
Outdated
var connectionConfig: BluetoothConnectionConfiguration | ||
if autoReconnectOnUnexpectedDisconnect { | ||
connectionConfig = BluetoothConnectionConfiguration( | ||
locationId: locationId ?? selectedReader.locationId ?? "", | ||
autoReconnectOnUnexpectedDisconnect: true, | ||
autoReconnectionDelegate: self | ||
) | ||
} else { | ||
connectionConfig = BluetoothConnectionConfiguration( | ||
locationId: locationId ?? selectedReader.locationId ?? "") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be simplified to a single BluetoothConnectionConfiguration if you use the autoReconnetOnUnexpectedDisconnect
variable as the value in the init, so:
let connectionConfig = BluetoothConnectionConfiguration(
locationId: locationId ?? selectedReader.locationId ?? "",
autoReconnectOnUnexpectedDisconnect: autoReconnectOnUnexpectedDisconnect,
autoReconnectionDelegate: autoReconnectOnUnexpectedDisconnect ? self : nil
TERMINAL_SUCCEED_READER_RECONNECT("terminalDidSucceedReaderReconnect"), | ||
TERMINAL_FAIL_READER_RECONNECT("terminalDidFailReaderReconnect"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should drop TERMINAL from these. "Terminal" is implied (for the native iOS callbacks it was added for consistency with other Terminal delegates but in a future version of the SDK its switching to be a reader delegate and will include the instance of the reader that succeeded or failed).
SUCCEED_READER_RECONNECT("didSucceedReaderReconnect"),
FAIL_READER_RECONNECT("didFailReaderReconnect"),
override fun onReaderReconnectFailed(reader: Reader) { | ||
context.sendEvent(TERMINAL_FAIL_READER_RECONNECT.listenerName) { | ||
putMap("reader", mapFromReader(reader)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to put a failure message here similar to
Lines 20 to 24 in 1870117
putMap("error", nativeMapOf { | |
putString("code", TerminalErrorCode.UNEXPECTED_SDK_ERROR.toString()) | |
putString("message", "Reader has been disconnected unexpectedly") | |
}) | |
} |
cc @chr-stripe
ios/StripeTerminalReactNative.swift
Outdated
func terminalDidFailReaderReconnect(_ terminal: Terminal) { | ||
sendEvent(withName: ReactNativeConstants.TERMINAL_FAIL_READER_RECONNECT.rawValue, body: nil) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as Android comment above, do we want to send an error here like https://github.com/stripe/stripe-terminal-react-native/blob/1870117c/ios/StripeTerminalReactNative.swift#L331-L334
cc @bric-stripe
ios/StripeTerminalReactNative.swift
Outdated
func terminal(_ terminal: Terminal, didStartReaderReconnect cancelable: Cancelable) { | ||
sendEvent(withName: ReactNativeConstants.START_READER_RECONNECT.rawValue, body: nil) | ||
} | ||
|
||
func terminalDidSucceedReaderReconnect(_ terminal: Terminal) { | ||
sendEvent(withName: ReactNativeConstants.TERMINAL_SUCCEED_READER_RECONNECT.rawValue, body: nil) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the Android listener we use mapFromReader to send the reader in body, should that here as well instead of sending nil
@@ -513,6 +525,18 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe | |||
sendEvent(withName: ReactNativeConstants.CHANGE_CONNECTION_STATUS.rawValue, body: ["result": result]) | |||
} | |||
|
|||
func terminal(_ terminal: Terminal, didStartReaderReconnect cancelable: Cancelable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like we are not doing much with the cancelable here. We should save that to var like cancelReaderConnectionCancellable
and then expose a cancelReaderReconnection
method for users to call
cc @bric-stripe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, good catch. Yeah that should be made available to the integrations so it can be used 👍
} | ||
} | ||
|
||
override fun onReaderReconnectStarted(reader: Reader, cancelReconnect: Cancelable) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to deal with the Cancelable
here and expose a cancelReaderReconnect method similar to Android. Existing example: https://github.com/stripe/stripe-terminal-react-native/blob/1870117c/android/src/main/java/com/stripeterminalreactnative/listener/RNBluetoothReaderListener.kt#L35-L37
cc @chr-stripe
@@ -0,0 +1,32 @@ | |||
package com.stripeterminalreactnative.listener |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test for RNBluetoothReaderListener;
const didFailReaderReconnect = useCallback(() => { | ||
log('didFailReaderReconnect'); | ||
}, [log]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should emit the error message here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const didStartReaderReconnect = useCallback(() => { | ||
log('didStartReaderReconnect'); | ||
}, [log]); | ||
|
||
const didSucceedReaderReconnect = useCallback(() => { | ||
log('didSucceedReaderReconnect'); | ||
}, [log]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should emit the connection reader here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
Motivation
Testing
Documentation
Select one: