Skip to content

Commit

Permalink
merge main into feature/Add-android-guard-for-destroyed-instace
Browse files Browse the repository at this point in the history
  • Loading branch information
dimninik committed May 22, 2024
2 parents 030b491 + 28b5ccc commit 2d72c66
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 16 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [3.1.2] - 2023-10-26

### Added

- The rawScanRecord has been added to advertising data

### Fixed

- The onDisconnected event is nowDispatched
- The missing advertising data fields on iOS has been added

## [3.1.1] - 2023-10-26

### Fixed
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ It does NOT support:
For old RN versions (<0.60) please check [old README](./docs/README_V1.md) (1.x)
for the old instructions or [migration guide](./docs/MIGRATION_V1.md).

| React Native | 3.1.1 |
| React Native | 3.1.2 |
| ------------ | ------------------ |
| 0.72.4 | :white_check_mark: |
| 0.72.6 | :white_check_mark: |
| 0.71.13 | :white_check_mark: |
| 0.70.13 | :white_check_mark: |
| Expo 49 | :white_check_mark: |

## Recent Changes

**3.1.1**
**3.1.2**

- Fixed expo config plugin for prebuilding
- Added rawScanRecord to advertising data
- Fixed onDisconnected event
- Fixed missing advertising data fields on iOS

[Current version changes](CHANGELOG.md)
[All previous changes](CHANGELOG-pre-03.md)
[All previous changes](CHANGELOG-pre-3.0.0.md)

## Documentation & Support

Expand Down Expand Up @@ -108,7 +111,7 @@ The plugin provides props for extra customization. Every time you change the pro
"expo": {
"plugins": [
[
"@config-plugins/react-native-ble-plx",
"react-native-ble-plx",
{
"isBackgroundEnabled": true,
"modes": ["peripheral", "central"],
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/bleplx/adapter/BleModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ private void safeConnectToDevice(final RxBleDevice device,
Observable<RxBleConnection> connect = device
.establishConnection(autoConnect)
.doOnSubscribe(disposable -> onConnectionStateChangedCallback.onEvent(ConnectionState.CONNECTING))
.doOnDispose(() -> {
.doFinally(() -> {
safeExecutor.error(BleErrorUtils.cancelled());
onDeviceDisconnected(device);
onConnectionStateChangedCallback.onEvent(ConnectionState.DISCONNECTED);
Expand Down
65 changes: 63 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset='utf-8'>
<title>react-native-ble-plx 3.1.1 | Documentation</title>
<title>react-native-ble-plx 3.1.2 | Documentation</title>
<meta name='description' content='React Native Bluetooth Low Energy library'>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<link href='assets/bass.css' rel='stylesheet'>
Expand All @@ -15,7 +15,7 @@
<div id='split-left' class='overflow-auto fs0 height-viewport-100'>
<div class='py1 px2'>
<h3 class='mb0 no-anchor'>react-native-ble-plx</h3>
<div class='mb1'><code>3.1.1</code></div>
<div class='mb1'><code>3.1.2</code></div>
<input
placeholder='Filter'
id='filter-input'
Expand Down Expand Up @@ -287,6 +287,12 @@ <h3 class='mb0 no-anchor'>react-native-ble-plx</h3>
#manufacturerData
</a></li>

<li><a
href='#devicerawscanrecord'
class='regular pre-open'>
#rawScanRecord
</a></li>

<li><a
href='#deviceservicedata'
class='regular pre-open'>
Expand Down Expand Up @@ -5004,6 +5010,61 @@ <h3 class='fl m0' id='device'>












</section>

</div>
</div>

<div class='border-bottom' id='devicerawscanrecord'>
<div class="clearfix small pointer toggle-sibling">
<div class="py1 contain">
<a class='icon pin-right py1 dark-link caret-right'></a>
<span class='code strong strong truncate'>rawScanRecord</span>
</div>
</div>
<div class="clearfix display-none toggle-target">
<section class='p2 mb2 clearfix bg-white minishadow'>



<p>Raw device scan data. When you have specific advertiser data,
you can implement your own processing.</p>

<div class='pre p1 fill-light mt0'>rawScanRecord</div>

<p>
Type:
<a href="#base64">Base64</a>
</p>























Expand Down
2 changes: 1 addition & 1 deletion example/src/components/atoms/Button/Button.styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AppText } from '../AppText/AppText'
export const Container = styled(TouchableOpacity)`
${({ theme }) => css`
background-color: ${theme.colors.mainRed};
margin: 10px;
margin: 5px 0px;
padding: 12px;
align-items: center;
border-radius: 100px;
Expand Down
2 changes: 1 addition & 1 deletion example/src/components/molecules/BleDevice/BleDevice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type BleDeviceProps = {

export function BleDevice({ device, onPress }: BleDeviceProps) {
const isConnectableInfoValueIsUnavailable = typeof device.isConnectable !== 'boolean'
const isConnectableValue = device.isConnectable ? 'ture' : 'false'
const isConnectableValue = device.isConnectable ? 'true' : 'false'
const parsedIsConnectable = isConnectableInfoValueIsUnavailable ? '-' : isConnectableValue

return (
Expand Down
8 changes: 8 additions & 0 deletions example/src/navigation/navigators/MainStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type MainStackParamList = {
DEVICE_CONNECT_DISCONNECT_TEST_SCREEN: undefined
AUTODISCONNECT_SCREEN: undefined
INSTANCE_DESTROY_SCREEN: undefined
DEVICE_ON_DISCONNECT_TEST_SCREEN: undefined
}

const MainStack = createNativeStackNavigator<MainStackParamList>()
Expand Down Expand Up @@ -54,6 +55,13 @@ export function MainStackComponent() {
headerTitle: 'Instance Destroy Screen'
}}
/>
<MainStack.Screen
name="DEVICE_ON_DISCONNECT_TEST_SCREEN"
component={screenComponents.DeviceOnDisconnectTestScreen}
options={{
headerTitle: 'On disconnect test'
}}
/>
</MainStack.Navigator>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function DashboardScreen({ navigation }: DashboardScreenProps) {
onPress={() => navigation.navigate('DEVICE_CONNECT_DISCONNECT_TEST_SCREEN')}
/>
<AppButton label="instance destroy screen" onPress={() => navigation.navigate('INSTANCE_DESTROY_SCREEN')} />
<AppButton label="On disconnect test" onPress={() => navigation.navigate('DEVICE_ON_DISCONNECT_TEST_SCREEN')} />
<FlatList
style={{ flex: 1 }}
data={foundDevices}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'
import type { NativeStackScreenProps } from '@react-navigation/native-stack'
import { BleError, Device, type Subscription, type DeviceId } from 'react-native-ble-plx'
import { ScrollView } from 'react-native'
import Toast from 'react-native-toast-message'
import { wait } from '../../../utils/wait'
import type { TestStateType } from '../../../types'
import { BLEService } from '../../../services'
import type { MainStackParamList } from '../../../navigation/navigators'
import { AppButton, AppTextInput, ScreenDefaultContainer, TestStateDisplay } from '../../../components/atoms'
import { deviceTimeService } from '../../../consts/nRFDeviceConsts'

type DeviceOnDisconnectTestScreenProps = NativeStackScreenProps<MainStackParamList, 'DEVICE_ON_DISCONNECT_TEST_SCREEN'>

export function DeviceOnDisconnectTestScreen(_props: DeviceOnDisconnectTestScreenProps) {
const [expectedDeviceName, setExpectedDeviceName] = useState('')
const [testScanDevicesState, setTestScanDevicesState] = useState<TestStateType>('WAITING')
const [deviceId, setDeviceId] = useState('')
const [currentTest, setCurrentTest] = useState<null | 'disconnectByDevice' | 'disconnectByPLX'>(null)
const onDisconnectRef = useRef<Subscription | null>(null)

const checkDeviceName = (device: Device) =>
device.name?.toLocaleLowerCase() === expectedDeviceName.toLocaleLowerCase()

const connectAndDiscover = async () => {
setTestScanDevicesState('IN_PROGRESS')
await BLEService.initializeBLE()
await BLEService.scanDevices(
async (device: Device) => {
if (checkDeviceName(device)) {
console.info(`connecting to ${device.id}`)
await startConnectToDevice(device)
await BLEService.discoverAllServicesAndCharacteristicsForDevice()
setTestScanDevicesState('DONE')
setDeviceId(device.id)
}
},
[deviceTimeService]
)
}

const startConnectToDevice = (device: Device) => BLEService.connectToDevice(device.id)

const setupOnDeviceDisconnected = useCallback(
(directDeviceId?: DeviceId) => {
if (!deviceId && !directDeviceId) {
console.error('Device not ready')
return
}
if (onDisconnectRef.current?.remove) {
onDisconnectRef.current.remove()
onDisconnectRef.current = null
}
onDisconnectRef.current = BLEService.onDeviceDisconnectedCustom(directDeviceId || deviceId, disconnectedListener)
console.info('on device disconnected ready')
},
[deviceId]
)

const disconnectedListener = (error: BleError | null, device: Device | null) => {
console.warn('Disconnect listener called')
if (error) {
console.error('onDeviceDisconnected error')
}
if (device) {
console.info('onDeviceDisconnected device')
}
setDeviceId('')
setCurrentTest(null)
}

// https://github.com/dotintent/react-native-ble-plx/issues/1126
const start1126Test = () => connectAndDiscover().then(() => setCurrentTest('disconnectByPLX'))

// https://github.com/dotintent/react-native-ble-plx/issues/1126
const start1126DeviceTest = () => connectAndDiscover().then(() => setCurrentTest('disconnectByDevice'))

const disconnectByPLX = useCallback(async () => {
try {
setupOnDeviceDisconnected()
await wait(1000)
console.info('expected warn: "Disconnect listener called"')
BLEService.disconnectDevice()
console.info('Finished')
} catch (error) {
console.error(error)
}
}, [setupOnDeviceDisconnected])

const disconnectByDevice = useCallback(async () => {
try {
setupOnDeviceDisconnected()
wait(1000)
Toast.show({
type: 'info',
text1: 'Disconnect device',
text2: 'and expect warn: "Disconnect listener called"'
})
console.info('Disconnect device and expect warn: "Disconnect listener called"')
} catch (error) {
console.error(error)
}
}, [setupOnDeviceDisconnected])

useEffect(() => {
if (!deviceId) {
return
}
if (currentTest === 'disconnectByPLX') {
disconnectByPLX()
}
if (currentTest === 'disconnectByDevice') {
disconnectByDevice()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [deviceId])

return (
<ScreenDefaultContainer>
<ScrollView showsVerticalScrollIndicator={false}>
<AppTextInput
placeholder="Device name to connect"
value={expectedDeviceName}
onChangeText={setExpectedDeviceName}
/>
<AppButton label="Connect and discover" onPress={connectAndDiscover} />
<AppButton label="Setup on device disconnected" onPress={() => setupOnDeviceDisconnected()} />
<TestStateDisplay label="Looking for device" state={testScanDevicesState} />
<AppButton label="Start 1126 test (trigger by ble-plx)" onPress={() => start1126Test()} />
<AppButton label="Start 1126 test (trigger by device)" onPress={() => start1126DeviceTest()} />
</ScrollView>
</ScreenDefaultContainer>
)
}
1 change: 1 addition & 0 deletions example/src/screens/MainStack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './DeviceDetailsScreen/DeviceDetailsScreen'
export * from './DevicenRFTestScreen/DevicenRFTestScreen'
export * from './DeviceConnectDisconnectTestScreen/DeviceConnectDisconnectTestScreen'
export * from './InstanceDestroyScreen/InstanceDestroyScreen'
export * from './DeviceOnDisconnectTestScreen/DeviceOnDisconnectTestScreen'
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-ble-plx",
"version": "3.1.1",
"version": "3.1.2",
"description": "React Native Bluetooth Low Energy library",
"main": "src/index",
"module": "src/index",
Expand Down Expand Up @@ -45,7 +45,7 @@
"test:ios": "cd test_project/ios && xcodebuild -workspace BlePlxExample.xcworkspace -scheme BlePlxExample -configuration Debug -sdk iphonesimulator ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO",
"test:expo": "cd test_project && npx expo prebuild",
"bootstrap": "yarn example && yarn install && yarn example pods",
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build",
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build plugin/build lib",
"build:plugin": "tsc --build plugin",
"clean:plugin": "expo-module clean plugin",
"test:plugin": "jest --config plugin/jest.config.js",
Expand Down Expand Up @@ -117,7 +117,7 @@
"@types/react": "^18.2.44"
},
"dependencies": {
"@expo/config-plugins": "^8.0.4"
"@expo/config-plugins": "^7.2.5 || ^8.0.0"
},
"peerDependencies": {
"react": "*",
Expand Down
1 change: 0 additions & 1 deletion src/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class Device implements NativeDevice {
/**
* Raw device scan data. When you have specific advertiser data,
* you can implement your own processing.
* @private
*/
rawScanRecord: Base64

Expand Down
1 change: 0 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,6 @@ declare module 'react-native-ble-plx' {
/**
* Raw device scan data. When you have specific advertiser data,
* you can implement your own processing.
* @private
*/
rawScanRecord: Base64

Expand Down

0 comments on commit 2d72c66

Please sign in to comment.