MohGovIL / rn-contact-tracing

React Native Library For Contact Tracing Over BLE (Bluetooth Low Energy) To Fight COVID-19 Pandemic
MIT License
59 stars 16 forks source link

getAllDevices() results in null #65

Open mhr opened 4 years ago

mhr commented 4 years ago

Hi,

I tried to create a sample app (below). I tried to write the minimal thing that wouldn't result in exceptions and might work. Unfortunately, when I load my code on two devices with both Location and Bluetooth permissions enabled, neither device picks up on the presence of the other device. There are no exceptions from this code initially, but after a few minutes, the app crashes for no apparent reason.

import React, { useState, useEffect } from "react";
import { Text } from "react-native";
import SpecialBle, { requestLocationPermission, checktLocationPermission } from "rn-contact-tracing";

const SERVICE_UUID = "00000000-0000-1000-8000-00805F9B34FB";

/*
const ScanMatchMode = [
    {value: 1, label: 'MATCH_MODE_AGGRESSIVE'},
    {value: 2, label: 'SCAN_MODE_LOW_LATENCY'},
];

const AdvertiseMode = [
    {value: 0, label: 'ADVERTISE_MODE_LOW_POWER'},
    {value: 1, label: 'ADVERTISE_MODE_BALANCED'},
    {value: 2, label: 'ADVERTISE_MODE_LOW_LATENCY'},
];

const AdvertiseTXPower = [
    {value: 0, label: 'ADVERTISE_TX_POWER_ULTRA_LOW'},
    {value: 1, label: 'ADVERTISE_TX_POWER_LOW'},
    {value: 2, label: 'ADVERTISE_TX_POWER_MEDIUM'},
    {value: 3, label: 'ADVERTISE_TX_POWER_HIGH'},
];
*/

const BluetoothScreen = () => {
    const [permissions, setPermissions] = useState({location: false, ignoreBatteryOpt: false});
    const [config, setConfig] = useState({
        serviceUUID: SERVICE_UUID,
        scanDuration: 10000,
        scanInterval: 10000,
        advertiseInterval: 10000,
        advertiseDuration: 10000,
        scanMatchMode: 1,
        advertiseMode: 2,
        advertiseTXPowerLevel: 1,
        notificationTitle: "Bluetooth",
        notificationContent: "Bluetooth",
        notificationLargeIconPath: "",
        notificationSmallIconPath: "",
        token: "default_token"
    });

    async function checkPermissions() {
        let isGranted = await checktLocationPermission();
        let ignoreBatteryOpt = await SpecialBle.isBatteryOptimizationDeactivated();
        setPermissions({...permissions, ...{location: isGranted, ignoreBatteryOpt: ignoreBatteryOpt}});
    }

    useEffect(() => {
        (async () => {
            console.log("module:", SpecialBle);

            console.log("permissions");
            await checkPermissions();

            console.log("database");
            await SpecialBle.writeContactsToDB(null);

            console.log("set config");
            await SpecialBle.setConfig(config);

            console.log("start ble service");
            await SpecialBle.startBLEService();

            console.log("advertise");
            await SpecialBle.advertise();

            console.log("start scan");
            await SpecialBle.startBLEScan();

            console.log("get all devices");
            await SpecialBle.getAllDevices(devices => {
                console.log(devices);
            });
        })();
    }, []);

    return (<Text>Bluetooth</Text>);
};

export default BluetoothScreen;
 LOG  module: {"advertise": [Function fn], "cleanDevicesDB": [Function fn], "cleanScansDB": [Function fn], "deleteDatabase": [Function fn], "exportAdvertiseAsCSV": [Function fn], "exportAllContactsAsCsv": [Function fn], "exportAllDevicesCsv": [Function fn], "exportAllScansCsv": [Function fn], "exportScansByKeyAsCSV": [Function fn], "exportScansDataAsCSV": [Function fn], "fetchInfectionDataByConsent": [Function fn], "getAllDevices": [Function fn], "getAllScans": [Function fn], "getConfig": [Function fn], "getConstants": [Function anonymous], "getScansByKey": [Function fn], "isBatteryOptimizationDeactivated": [Function fn], "match": [Function fn], "requestToDisableBatteryOptimization": [Function fn], "setConfig": [Function fn], "setPublicKeys": [Function fn], "startBLEScan": [Function fn], "startBLEService": [Function fn], "stopAdvertise": [Function fn], "stopBLEScan": [Function fn], "stopBLEService": [Function fn], "writeContactsToDB": [Function fn]}
 LOG  permissions
 LOG  database
 LOG  set config
 LOG  start ble service
 LOG  advertise
 LOG  start scan
 LOG  get all devices
 LOG  null
Xeceryn commented 4 years ago

is there a solution for the "null" getDevice () function?

Xeceryn commented 4 years ago

Change to this.

SpecialBle.getAllDevices((err, devices) => {
       setDevices(devices)
})

it work for me.