【问题标题】:react native bluetooth app current time service反应原生蓝牙应用当前时间服务
【发布时间】:2018-11-01 02:26:53
【问题描述】:

我需要开发一个连接蓝牙 BLE 设备的 ReactNative 应用。 我正在使用 react-native-ble-plx 库。

其中一项关键工作是在 ReactNative 应用和蓝牙设备之间同步当前时间。

我意识到该应用程序应该具有 CTS,即当前时间服务。 如何实施 CTS 服务? 有没有提供CTS服务的图书馆?

【问题讨论】:

    标签: react-native bluetooth bluetooth-gatt


    【解决方案1】:

    我不确定您的问题是关于在设备上创建 CTS,还是与具有 CTS 的设备交互。我假设是后者,你的问题是这样的:“如何将当前时间写入外围 BLE 设备上的当前时间服务?”

    以下是我如何使用 react-native-ble-plx 库将时间写入外围设备上的 CTS。

    constructCT = (): string => {
        // Order:
        // Year_LSO (byte), Year_MSO (byte),  Month (byte), DayOfMonth (byte), Hours (byte), Minutes (byte), Seconds (byte), DayOfWeek (byte), Fractions (byte), AdjReason (byte)
    
        const fullDateTime = new Date();
        const year = fullDateTime.getFullYear();
        const month = fullDateTime.getMonth() + 1;
        const dayOfMonth = fullDateTime.getDate();
        const hours = fullDateTime.getHours();
        const minutes = fullDateTime.getMinutes();
        const seconds = fullDateTime.getSeconds();
        let dayOfWeek = fullDateTime.getDay();
        if (dayOfWeek === 0) {
            dayOfWeek = 7;
        }
        const fraction1000 = fullDateTime.getMilliseconds();
        const adjustFractionDenom = 256 / 1000;
        const fraction256 = Math.round(fraction1000 * adjustFractionDenom);
        // TODO: Set your adjust reasons accordingly, in my case they were unnecessary
        // Adjust Reasons: Manual Update, External Update, Time Zone Change, Daylight Savings Change
        const adjustReasons = [true, false, false, true];
        let adjustValue = 0;
        for (let i = 0; i < 4; i++) {
            if (adjustReasons[i]) {
                adjustValue = adjustValue | (1 << i);
            }
        }
    
        // console.log("Year:", year, " Month:", month, " Date:", dayOfMonth, " Hours:", hours, " Minutes:", minutes, " Seconds:", seconds, " DOW:", dayOfWeek, " MS:", fraction256, " Adjust Reason: ", adjustValue);
    
        const yearHex = this.correctLength(year.toString(16), 4);
        const yearHexMSO = yearHex.substr(0, 2);
        const yearHexLSO = yearHex.substr(2, 2);
        const monthHex = this.correctLength(month.toString(16), 2);
        const dayOfMonthHex = this.correctLength(dayOfMonth.toString(16), 2);
        const hoursHex = this.correctLength(hours.toString(16), 2);
        const minutesHex = this.correctLength(minutes.toString(16), 2);
        const secondsHex = this.correctLength(seconds.toString(16), 2);
        const dayOfWeekHex = this.correctLength(dayOfWeek.toString(16), 2);
        const fractionHex = this.correctLength(fraction256.toString(16), 2);
        const adjustValueHex = this.correctLength(adjustValue.toString(16), 2);
    
        const currentTime = yearHexLSO + yearHexMSO + monthHex + dayOfMonthHex + hoursHex + minutesHex + secondsHex + dayOfWeekHex + fractionHex + adjustValueHex;
        const currentTime64 = Buffer.from(currentTime, 'hex').toString('base64');
        return currentTime64;
    }
    
    correctLength = (str, dig): numFilled => {
        let result = str;
        while (result.length !== dig) {
            result = "0" + result;
        }
        return result;
    }
    
    pushTimeData = () => {
        const CTSValue = this.constructCT();
        this.state.manager.writeCharacteristicWithResponseForDevice(this.state.deviceName, time_service, time_charac, CTSValue)
            .then((charac) => {
                // console.log("CHARAC VALUE: ", charac.value);
            })
            .catch((error) => {
                console.log(JSON.stringify(error));
            })
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 2017-05-08
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多