Skip to content

Commit

Permalink
Merge branch 'master' into improvement/Handle-not-valid-bluetooth-add…
Browse files Browse the repository at this point in the history
…ress-native-error
  • Loading branch information
dominik-czupryna-withintent authored Oct 16, 2023
2 parents 9fdee09 + 3e84359 commit eb95dce
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 36 deletions.
16 changes: 15 additions & 1 deletion android/src/main/java/com/bleplx/BlePlxModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,36 @@
import com.bleplx.converter.ServiceToJsObjectConverter;
import com.bleplx.utils.ReadableArrayConverter;
import com.bleplx.utils.SafePromise;
import com.polidea.rxandroidble2.internal.RxBleLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.app.Activity;

import io.reactivex.exceptions.UndeliverableException;
import io.reactivex.plugins.RxJavaPlugins;

@ReactModule(name = BlePlxModule.NAME)
public class BlePlxModule extends ReactContextBaseJavaModule {
public static final String NAME = "BlePlx";

public BlePlxModule(ReactApplicationContext reactContext) {
super(reactContext);
RxJavaPlugins.setErrorHandler(throwable -> {
if (throwable instanceof UndeliverableException) {
RxBleLog.e("Handle all unhandled exceptions from RxJava: " + throwable.getMessage());
} else {
Thread currentThread = Thread.currentThread();
Thread.UncaughtExceptionHandler errorHandler = currentThread.getUncaughtExceptionHandler();
if (errorHandler != null) {
errorHandler.uncaughtException(currentThread, throwable);
}
}
});
}

@Override
Expand Down
1 change: 0 additions & 1 deletion android/src/main/java/com/bleplx/adapter/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.UUID;

/** @noinspection unused*/
public class Device {

private String id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.bleplx.adapter.utils.mapper;

import static com.polidea.rxandroidble2.scan.IsConnectable.CONNECTABLE;

import com.bleplx.adapter.AdvertisementData;
import com.bleplx.adapter.ScanResult;
import com.bleplx.adapter.utils.Constants;

public class RxScanResultToScanResultMapper {

public ScanResult map(com.polidea.rxandroidble2.scan.ScanResult rxScanResult) {
return new ScanResult(
rxScanResult.getBleDevice().getMacAddress(),
rxScanResult.getBleDevice().getName(),
rxScanResult.getRssi(),
Constants.MINIMUM_MTU,
false, //isConnectable flag is not available on Android
null, //overflowServiceUUIDs are not available on Android
AdvertisementData.parseScanResponseData(rxScanResult.getScanRecord().getBytes())
);
}
public ScanResult map(com.polidea.rxandroidble2.scan.ScanResult rxScanResult) {
return new ScanResult(
rxScanResult.getBleDevice().getMacAddress(),
rxScanResult.getBleDevice().getName(),
rxScanResult.getRssi(),
Constants.MINIMUM_MTU,
rxScanResult.isConnectable() == CONNECTABLE,
null, //overflowServiceUUIDs are not available on Android
AdvertisementData.parseScanResponseData(rxScanResult.getScanRecord().getBytes())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public WritableMap toJSObject(@NonNull ScanResult scanResult) {
result.putString(Metadata.NAME, scanResult.getDeviceName());
result.putInt(Metadata.RSSI, scanResult.getRssi());
result.putInt(Metadata.MTU, scanResult.getMtu());
result.putBoolean(Metadata.IS_CONNECTABLE, scanResult.isConnectable());

AdvertisementData advData = scanResult.getAdvertisementData();
result.putString(Metadata.MANUFACTURER_DATA,
Expand Down Expand Up @@ -88,7 +89,6 @@ public WritableMap toJSObject(@NonNull ScanResult scanResult) {
}

// Attributes which are not accessible on Android
result.putNull(Metadata.IS_CONNECTABLE);
result.putNull(Metadata.OVERFLOW_SERVICE_UUIDS);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import type { TestStateType } from 'example/types'
import type { TestStateType } from '../../../types'
import { AppText } from '../AppText/AppText'
import { Container, Header, Label } from './TestStateDisplay.styled'

export type TestStateDisplayProps = {
label?: string
state: TestStateType
state?: TestStateType
value?: string
}

Expand All @@ -21,9 +21,9 @@ export function TestStateDisplay({ label, state, value }: TestStateDisplayProps)
<Container>
<Header>
<Label>{label}</Label>
<AppText>{marks[state]}</AppText>
{!!state && <AppText>{marks[state]}</AppText>}
</Header>
{value && <AppText>{value}</AppText>}
{!!value && <AppText>{value}</AppText>}
</Container>
)
}
13 changes: 13 additions & 0 deletions example/src/consts/nRFDeviceConsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fullUUID } from 'react-native-ble-plx'
import base64 from 'react-native-base64'
import { getDateAsBase64 } from '../utils/getDateAsBase64'

export const deviceTimeService = fullUUID('1847')
export const currentTimeCharacteristic = fullUUID('2A2B')
export const deviceTimeCharacteristic = fullUUID('2B90')
export const currentTimeCharacteristicTimeTriggerDescriptor = fullUUID('290E')

export const writeWithResponseBase64Time = getDateAsBase64(new Date('2022-08-11T08:17:19Z'))
export const writeWithoutResponseBase64Time = getDateAsBase64(new Date('2023-09-12T10:12:16Z'))
export const monitorExpectedMessage = 'Hi, it works!'
export const currentTimeCharacteristicTimeTriggerDescriptorValue = base64.encode('BLE-PLX')
8 changes: 8 additions & 0 deletions example/src/navigation/navigators/MainStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type MainStackParamList = {
DASHBOARD_SCREEN: undefined
DEVICE_DETAILS_SCREEN: undefined
DEVICE_NRF_TEST_SCREEN: undefined
DEVICE_CONNECT_DISCONNECT_TEST_SCREEN: undefined
}

const MainStack = createNativeStackNavigator<MainStackParamList>()
Expand Down Expand Up @@ -37,6 +38,13 @@ export function MainStackComponent() {
headerTitle: 'nRF device test'
}}
/>
<MainStack.Screen
name="DEVICE_CONNECT_DISCONNECT_TEST_SCREEN"
component={screenComponents.DeviceConnectDisconnectTestScreen}
options={{
headerTitle: 'Connect/disconnect'
}}
/>
</MainStack.Navigator>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export function DashboardScreen({ navigation }: DashboardScreenProps) {
<AppButton label="Ask for permissions" onPress={BLEService.requestBluetoothPermission} />
<AppButton label="Go to nRF test" onPress={() => navigation.navigate('DEVICE_NRF_TEST_SCREEN')} />
<AppButton label="Call disconnect with wrong id" onPress={() => BLEService.isDeviceWithIdConnected('asd')} />
<AppButton
label="Connect/disconnect test"
onPress={() => navigation.navigate('DEVICE_CONNECT_DISCONNECT_TEST_SCREEN')}
/>
<FlatList
style={{ flex: 1 }}
data={foundDevices}
Expand Down
Loading

0 comments on commit eb95dce

Please sign in to comment.