Skip to content

Commit

Permalink
fix: android MTU error in example
Browse files Browse the repository at this point in the history
  • Loading branch information
intent-kacper-cyranowski authored and aliberski committed Dec 20, 2024
1 parent f0d2e0f commit e874991
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ export function DeviceConnectDisconnectTestScreen(_props: DeviceConnectDisconnec
setTimeoutTestState('IN_PROGRESS')

try {
await BLEService.connectToDevice(device.id, CONNECTION_TIMEOUT)
await BLEService.connectToDevice(device.id, CONNECTION_TIMEOUT, true)
setTimeoutTestState('ERROR')
setTimeoutTestLabel('Device was able to connect')
} catch (error) {
if (
error instanceof Error &&
(error.message === 'Operation was cancelled' || error.message === 'Operation timed out')
) {
console.info('Timeout test successful: Connection timed out as expected')
setTimeoutTestState('DONE')
setTimeoutTestLabel('Success')
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, type Dispatch } from 'react'
import type { NativeStackScreenProps } from '@react-navigation/native-stack'
import { Device, type Base64 } from 'react-native-ble-plx'
import { Platform, ScrollView } from 'react-native'
import { Platform, ScrollView, Alert } from 'react-native'
import base64 from 'react-native-base64'
import type { TestStateType } from '../../../types'
import { BLEService, usePersistentDeviceName } from '../../../services'
Expand All @@ -18,6 +18,7 @@ import {
writeWithResponseBase64Time,
writeWithoutResponseBase64Time
} from '../../../consts/nRFDeviceConsts'
import { isAndroidSdkAbove34 } from '../../../utils/isAndroidAbove14'

type DevicenRFTestScreenProps = NativeStackScreenProps<MainStackParamList, 'DEVICE_NRF_TEST_SCREEN'>

Expand Down Expand Up @@ -240,10 +241,23 @@ export function DevicenRFTestScreen(_props: DevicenRFTestScreenProps) {
new Promise<void>((resolve, reject) => {
startTestInfo('startTestMonitorCurrentTimeCharacteristicForDevice')
setTestMonitorCurrentTimeCharacteristicForDevice('IN_PROGRESS')
Alert.alert(
'Monitor Current Time Characteristic',
`Please send the following message to the device: "${monitorExpectedMessage}"`,
[
{
text: 'OK'
}
]
)
BLEService.setupMonitor(
deviceTimeService,
currentTimeCharacteristic,
async characteristic => {
console.info(
'startTestMonitorCurrentTimeCharacteristicForDevice',
`received value: ${characteristic.value && base64.decode(characteristic.value)}, expected value: ${monitorExpectedMessage}`
)
if (characteristic.value && base64.decode(characteristic.value) === monitorExpectedMessage) {
setTestMonitorCurrentTimeCharacteristicForDevice('DONE')
await BLEService.finishMonitor()
Expand Down Expand Up @@ -421,7 +435,8 @@ export function DevicenRFTestScreen(_props: DevicenRFTestScreenProps) {
runTest(getConnectedDevices, setTestConnectedDevicesState, 'startGetConnectedDevices')

const startRequestMTUForDevice = () => {
const expectedMTU = 40
const expectedMTU = isAndroidSdkAbove34 ? 517 : 40

return runTest(
() =>
BLEService.requestMTUForDevice(expectedMTU).then(device => {
Expand Down
6 changes: 4 additions & 2 deletions example/src/services/BLEService/BLEService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class BLEServiceInstance {
this.manager.stopDeviceScan()
}

connectToDevice = (deviceId: DeviceId, timeout?: number) =>
connectToDevice = (deviceId: DeviceId, timeout?: number, ignoreError = false) =>
new Promise<Device>((resolve, reject) => {
this.manager.stopDeviceScan()
this.manager
Expand All @@ -133,7 +133,9 @@ class BLEServiceInstance {
if (error.errorCode === BleErrorCode.DeviceAlreadyConnected && this.device) {
resolve(this.device)
} else {
this.onError(error)
if (!ignoreError) {
this.onError(error)
}
reject(error)
}
})
Expand Down
3 changes: 3 additions & 0 deletions example/src/utils/isAndroidAbove14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Platform } from 'react-native'

export const isAndroidSdkAbove34 = Platform.OS === 'android' && Platform.Version >= 34

0 comments on commit e874991

Please sign in to comment.