-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (core) [DSDK-440]: Connect to Ledger Ble Device (#221)
- Loading branch information
Showing
44 changed files
with
1,944 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ledgerhq/device-sdk-core": patch | ||
--- | ||
|
||
Add BLE support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
apps/sample/src/components/MainView/ConnectDeviceActions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React, { useCallback } from "react"; | ||
import { BuiltinTransports, SdkError } from "@ledgerhq/device-management-kit"; | ||
import { Button, Flex } from "@ledgerhq/react-ui"; | ||
import styled from "styled-components"; | ||
|
||
import { useSdk } from "@/providers/DeviceSdkProvider"; | ||
import { useDeviceSessionsContext } from "@/providers/DeviceSessionsProvider"; | ||
import { useSdkConfigContext } from "@/providers/SdkConfig"; | ||
|
||
type ConnectDeviceActionsProps = { | ||
onError: (error: SdkError | null) => void; | ||
}; | ||
|
||
const ConnectButton = styled(Button).attrs({ mx: 3 })``; | ||
|
||
export const ConnectDeviceActions = ({ | ||
onError, | ||
}: ConnectDeviceActionsProps) => { | ||
const { | ||
dispatch: dispatchSdkConfig, | ||
state: { transport }, | ||
} = useSdkConfigContext(); | ||
const { dispatch: dispatchDeviceSession } = useDeviceSessionsContext(); | ||
const sdk = useSdk(); | ||
|
||
const onSelectDeviceClicked = useCallback( | ||
(selectedTransport: BuiltinTransports) => { | ||
onError(null); | ||
dispatchSdkConfig({ | ||
type: "set_transport", | ||
payload: { transport: selectedTransport }, | ||
}); | ||
sdk.startDiscovering({ transport: selectedTransport }).subscribe({ | ||
next: (device) => { | ||
sdk | ||
.connect({ device }) | ||
.then((sessionId) => { | ||
console.log( | ||
`🦖 Response from connect: ${JSON.stringify(sessionId)} 🎉`, | ||
); | ||
dispatchDeviceSession({ | ||
type: "add_session", | ||
payload: { | ||
sessionId, | ||
connectedDevice: sdk.getConnectedDevice({ sessionId }), | ||
}, | ||
}); | ||
}) | ||
.catch((error) => { | ||
onError(error); | ||
console.error(`Error from connection or get-version`, error); | ||
}); | ||
}, | ||
error: (error) => { | ||
console.error(error); | ||
}, | ||
}); | ||
}, | ||
[sdk, transport], | ||
); | ||
|
||
return transport === BuiltinTransports.MOCK_SERVER ? ( | ||
<ConnectButton | ||
onClick={() => onSelectDeviceClicked(BuiltinTransports.MOCK_SERVER)} | ||
variant="main" | ||
backgroundColor="main" | ||
size="large" | ||
data-testid="CTA_select-device" | ||
> | ||
Select a device | ||
</ConnectButton> | ||
) : ( | ||
<Flex> | ||
<ConnectButton | ||
onClick={() => onSelectDeviceClicked(BuiltinTransports.USB)} | ||
variant="main" | ||
backgroundColor="main" | ||
size="large" | ||
> | ||
Select a USB device | ||
</ConnectButton> | ||
<ConnectButton | ||
onClick={() => onSelectDeviceClicked(BuiltinTransports.BLE)} | ||
variant="main" | ||
backgroundColor="main" | ||
size="large" | ||
> | ||
Select a BLE device | ||
</ConnectButton> | ||
</Flex> | ||
); | ||
}; |
Oops, something went wrong.