Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions android/src/main/java/it/innove/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public void scan(ReadableMap scanningOptions,

@SuppressLint("NewApi") // NOTE: constructor checks the API version.
@ReactMethod
public void companionScan(ReadableArray serviceUUIDs, ReadableMap options, Callback callback) {
public void deviceSetupScan(ReadableArray serviceUUIDs, ReadableMap options, Callback callback) {
if (this.companionScanner == null) {
callback.invoke("not supported");
} else {
Expand All @@ -353,7 +353,7 @@ public void companionScan(ReadableArray serviceUUIDs, ReadableMap options, Callb
}

@ReactMethod
public void supportsCompanion(Callback callback) {
public void supportsDeviceSetup(Callback callback) {
callback.invoke(companionScanner != null);
}

Expand Down Expand Up @@ -885,7 +885,7 @@ public void requestMTU(String deviceUUID, double mtu, Callback callback) {
}

@ReactMethod
public void getAssociatedPeripherals(Callback callback) {
public void getAssociatedDevices(Callback callback) {
Log.d(LOG_TAG, "Get associated peripherals");
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
callback.invoke("Not supported");
Expand All @@ -901,7 +901,7 @@ public void getAssociatedPeripherals(Callback callback) {
}

@ReactMethod
public void removeAssociatedPeripheral(String address, Callback callback) {
public void removeAssociatedDevice(String address, Callback callback) {
Log.d(LOG_TAG, "Remove associated peripheral: " + address);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
callback.invoke("Not supported");
Expand Down
18 changes: 10 additions & 8 deletions android/src/main/java/it/innove/CompanionScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;

import java.util.Objects;

@RequiresApi(api = Build.VERSION_CODES.O)
public class CompanionScanner {

Expand Down Expand Up @@ -71,12 +73,12 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
if (peripheral != null && scanCallback != null) {
scanCallback.invoke(null, peripheral.asWritableMap());
scanCallback = null;
bleManager.emitOnCompanionPeripheral(peripheral.asWritableMap());
bleManager.emitOnDeviceSetupSelected(peripheral.asWritableMap());
}
} else {
scanCallback.invoke(null, null);
scanCallback = null;
bleManager.emitOnCompanionPeripheral(null);
bleManager.emitOnDeviceSetupSelected(null);
}
} else {
// No device, user cancelled?
Expand All @@ -88,7 +90,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
scanCallback.invoke(null, peripheral != null ? peripheral.asWritableMap() : null);
scanCallback = null;
}
bleManager.emitOnCompanionPeripheral(peripheral != null ? peripheral.asWritableMap() : null);
bleManager.emitOnDeviceSetupSelected(peripheral != null ? peripheral.asWritableMap() : null);
}
};

Expand All @@ -110,7 +112,7 @@ public void scan(ReadableArray serviceUUIDs, ReadableMap options, Callback callb
.setSingleDevice(options.hasKey("single") && options.getBoolean("single")) ;

for (int i = 0; i < serviceUUIDs.size(); i++) {
final ParcelUuid uuid = new ParcelUuid(UUIDHelper.uuidFromString(serviceUUIDs.getString(i)));
final ParcelUuid uuid = new ParcelUuid(UUIDHelper.uuidFromString(Objects.requireNonNull(serviceUUIDs.getString(i))));
Log.d(LOG_TAG, "Filter service: " + uuid);

builder = builder
Expand Down Expand Up @@ -144,15 +146,15 @@ public void onFailure(@Nullable CharSequence charSequence) {
}

WritableMap map = Arguments.createMap();
map.putString("error", charSequence.toString());
bleManager.emitOnCompanionFailure(map);
map.putString("error", charSequence != null ? charSequence.toString() : "");
bleManager.emitOnDeviceSetupFailure(map);
}

@Override
public void onDeviceFound(@NonNull IntentSender intentSender) {
Log.d(LOG_TAG, "companion device found");
try {
reactContext.getCurrentActivity().startIntentSenderForResult(
Objects.requireNonNull(reactContext.getCurrentActivity()).startIntentSenderForResult(
intentSender, SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0
);
} catch (IntentSender.SendIntentException e) {
Expand All @@ -166,7 +168,7 @@ public void onDeviceFound(@NonNull IntentSender intentSender) {

WritableMap map = Arguments.createMap();
map.putString("error", msg);
bleManager.emitOnCompanionFailure(map);
bleManager.emitOnDeviceSetupFailure(map);
}
}
}, null);
Expand Down
6 changes: 6 additions & 0 deletions example/app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export default ({ config }) => {
},
ios: {
bundleIdentifier: 'it.innove.example.ble',
infoPlist: {
NSAccessorySetupKitSupports: ['Bluetooth'],
NSAccessorySetupBluetoothServices: [
'C219DA19-A018-405F-AF8E-BC98AD9FFAEC',
],
},
},
plugins: [
[
Expand Down
98 changes: 46 additions & 52 deletions example/components/ScanDevicesScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { SafeAreaView } from 'react-native-safe-area-context';

import { RootStackParamList } from '../types/navigation';

const SECONDS_TO_SCAN_FOR = 3;
const SERVICE_UUIDS: string[] = [];
const SECONDS_TO_SCAN_FOR = 5;
const SERVICE_UUIDS: string[] = ['C219DA19-A018-405F-AF8E-BC98AD9FFAEC'];
const ALLOW_DUPLICATES = true;

declare module 'react-native-ble-manager' {
Expand All @@ -54,7 +54,20 @@ const ScanDevicesScreen = () => {

//console.debug('peripherals map updated', [...peripherals.entries()]);

const startScan = () => {
const startScan = async () => {
if (!(await BleManager.isStarted())) {
try {
BleManager.start({ showAlert: false })
.then(() => console.debug('BleManager started.'))
.catch((error: any) =>
console.error('BeManager could not be started.', error)
);
} catch (error) {
console.error('unexpected error starting BleManager.', error);
return;
}
}

if (!isScanning) {
// reset found peripherals before scan
setPeripherals(new Map<Peripheral['id'], Peripheral>());
Expand Down Expand Up @@ -82,14 +95,14 @@ const ScanDevicesScreen = () => {
}
};

const startCompanionScan = () => {
const deviceSetupScan = () => {
setPeripherals(new Map<Peripheral['id'], Peripheral>());
try {
console.debug('[startCompanionScan] starting companion scan...');
BleManager.companionScan(SERVICE_UUIDS, { single: false })
console.debug('[deviceSetupScan] starting device setup scan...');
BleManager.deviceSetupScan(SERVICE_UUIDS, { single: false, verboseLogging: true })
.then((peripheral: Peripheral | null) => {
console.debug(
'[startCompanionScan] scan promise returned successfully.',
'[deviceSetupScan] scan promise returned successfully.',
peripheral
);
if (peripheral != null) {
Expand All @@ -99,10 +112,10 @@ const ScanDevicesScreen = () => {
}
})
.catch((err: any) => {
console.debug('[startCompanionScan] ble scan cancel', err);
console.debug('[deviceSetupScan] ble scan cancel', err);
});
} catch (error) {
console.error('[startCompanionScan] ble scan error thrown', error);
console.error('[deviceSetupScan] ble scan error thrown', error);
}
};

Expand Down Expand Up @@ -254,7 +267,7 @@ const ScanDevicesScreen = () => {

const getAssociatedPeripherals = async () => {
try {
const associatedPeripherals = await BleManager.getAssociatedPeripherals();
const associatedPeripherals = await BleManager.getAssociatedDevices();
console.debug(
'[getAssociatedPeripherals] associatedPeripherals',
associatedPeripherals
Expand Down Expand Up @@ -399,17 +412,6 @@ const ScanDevicesScreen = () => {
}

useEffect(() => {
try {
BleManager.start({ showAlert: false })
.then(() => console.debug('BleManager started.'))
.catch((error: any) =>
console.error('BeManager could not be started.', error)
);
} catch (error) {
console.error('unexpected error starting BleManager.', error);
return;
}

const listeners: any[] = [
BleManager.onDiscoverPeripheral(handleDiscoverPeripheral),
BleManager.onStopScan(handleStopScan),
Expand Down Expand Up @@ -494,6 +496,8 @@ const ScanDevicesScreen = () => {
);
};

const supportEnableBluetooth = Platform.OS === 'android';

return (
<>
<StatusBar />
Expand Down Expand Up @@ -521,37 +525,27 @@ const ScanDevicesScreen = () => {
</Pressable>
</View>

{Platform.OS === 'android' && (
<>
<View style={styles.buttonGroup}>
<Pressable style={styles.scanButton} onPress={startCompanionScan}>
<Text style={styles.scanButtonText}>Scan Companion</Text>
</Pressable>

<Pressable
style={styles.scanButton}
onPress={getAssociatedPeripherals}
>
<Text style={styles.scanButtonText}>
Get Associated Peripherals
</Text>
</Pressable>
</View>

<View style={styles.buttonGroup}>
<Pressable
style={styles.scanButton}
onPress={removeAssociatedPeripherals}
>
<Text style={styles.scanButtonText}>
Remove Associated Peripherals
</Text>
</Pressable>
<Pressable style={styles.scanButton} onPress={enableBluetooth}>
<Text style={styles.scanButtonText}>Enable Bluetooth</Text>
</Pressable>
</View>
</>
<View style={styles.buttonGroup}>
<Pressable style={styles.scanButton} onPress={deviceSetupScan}>
<Text style={styles.scanButtonText}>Device Setup Scan</Text>
</Pressable>

<Pressable
style={styles.scanButton}
onPress={getAssociatedPeripherals}
>
<Text style={styles.scanButtonText}>
Get Associated Peripherals
</Text>
</Pressable>
</View>

{supportEnableBluetooth && (
<View style={styles.buttonGroup}>
<Pressable style={styles.scanButton} onPress={enableBluetooth}>
<Text style={styles.scanButtonText}>Enable Bluetooth</Text>
</Pressable>
</View>
)}

{Array.from(peripherals.values()).length === 0 && (
Expand Down
8 changes: 4 additions & 4 deletions ios/BleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
- (void)emitOnPeripheralDidBond:(NSDictionary *)value;
- (void)emitOnCentralManagerWillRestoreState:(NSDictionary *)value;
- (void)emitOnDidUpdateNotificationStateFor:(NSDictionary *)value;
- (void)emitOnCompanionPeripheral:(NSDictionary *)value;
- (void)emitOnCompanionFailure:(NSDictionary *)value;
- (void)emitOnDeviceSetupSelected:(NSDictionary *)value;
- (void)emitOnDeviceSetupFailure:(NSDictionary *)value;
+ (nullable CBCentralManager *)getCentralManager;
+ (nullable SwiftBleManager *)getInstance;
@end
Expand All @@ -34,8 +34,8 @@
- (void)emitOnPeripheralDidBond:(NSDictionary *)value;
- (void)emitOnCentralManagerWillRestoreState:(NSDictionary *)value;
- (void)emitOnDidUpdateNotificationStateFor:(NSDictionary *)value;
- (void)emitOnCompanionPeripheral:(NSDictionary *)value;
- (void)emitOnCompanionFailure:(NSDictionary *)value;
- (void)emitOnDeviceSetupSelected:(NSDictionary *)value;
- (void)emitOnDeviceSetupFailure:(NSDictionary *)value;
@end

#endif
Expand Down
16 changes: 8 additions & 8 deletions ios/BleManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ - (void)checkState:(RCTResponseSenderBlock)callback {
[_swBleManager checkState:callback];
}

- (void)companionScan:(NSArray *)serviceUUIDs
- (void)deviceSetupScan:(NSArray *)serviceUUIDs
option:(NSDictionary *)option
callback:(RCTResponseSenderBlock)callback {
[_swBleManager companionScan:serviceUUIDs option:option callback:callback];
[_swBleManager deviceSetupScan:serviceUUIDs option:option callback:callback];
}

- (void)connect:(NSString *)peripheralUUID
Expand All @@ -74,8 +74,8 @@ - (void)enableBluetooth:(RCTResponseSenderBlock)callback {
[_swBleManager enableBluetooth:callback];
}

- (void)getAssociatedPeripherals:(RCTResponseSenderBlock)callback {
[_swBleManager getAssociatedPeripherals:callback];
- (void)getAssociatedDevices:(RCTResponseSenderBlock)callback {
[_swBleManager getAssociatedDevices:callback];
}

- (void)getBondedPeripherals:(RCTResponseSenderBlock)callback {
Expand Down Expand Up @@ -147,9 +147,9 @@ - (void)refreshCache:(NSString *)peripheralUUID
[_swBleManager refreshCache:peripheralUUID callback:callback];
}

- (void)removeAssociatedPeripheral:(NSString *)peripheralUUID
- (void)removeAssociatedDevice:(NSString *)peripheralUUID
callback:(RCTResponseSenderBlock)callback {
[_swBleManager removeAssociatedPeripheral:peripheralUUID callback:callback];
[_swBleManager removeAssociatedDevice:peripheralUUID callback:callback];
}

- (void)removeBond:(NSString *)peripheralUUID
Expand Down Expand Up @@ -238,8 +238,8 @@ - (void)stopScan:(RCTResponseSenderBlock)callback {
[_swBleManager stopScan:callback];
}

- (void)supportsCompanion:(RCTResponseSenderBlock)callback {
[_swBleManager supportsCompanion:callback];
- (void)supportsDeviceSetup:(RCTResponseSenderBlock)callback {
[_swBleManager supportsDeviceSetup:callback];
}

- (void)write:(NSString *)peripheralUUID
Expand Down
Loading