0

我使用 react-native-ble-plx 库与我的 arduino nano 33 ble 感应设备建立了连接,我可以通过特征查看数据,但过了一会儿,连接丢失并且出现此错误。

WARN     Possible Unhandled Promise Rejection (id: 4):
BleError: Characteristic 11 not found
construct@[native code]
Wrapper@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:6622:64
construct@[native code]
_createSuperInternal@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164178:322
BleError@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164191:26
parseBleError@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164229:30
_callPromise$@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164477:86
tryCatch@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7486:23
invoke@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7656:32
tryCatch@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7486:23
invoke@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7558:30
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7570:21
tryCallOne@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24591:16
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24692:27
_callTimer@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28217:17
_callImmediatesPass@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28256:17
callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28474:33
__callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2555:35
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2340:34
__guard@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2538:15
flushedQueue@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2339:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]
info Reloading app...
[Tue Oct 26 2021 02:58:28.370]  BUNDLE  ./index.js

[Tue Oct 26 2021 02:58:29.857]  LOG      Running "healthyrace" with {"rootTag":101}
[Tue Oct 26 2021 02:58:46.353]  LOG      {"additionalUserInfo": {"isNewUser": false}, "user": {"displayName": null, "email": "cans.b@outlook.com", "emailVerified": false, "isAnonymous": false, "metadata": [Object], "phoneNumber": null, "photoURL": null, "providerData": [Array], "providerId": "firebase", "tenantId": null, "uid": "MtAqBgfbCnMl6mecGFV4931BaA73"}}
[Tue Oct 26 2021 02:58:54.403]  WARN     Possible Unhandled Promise Rejection (id: 0):
BleError: Device CD:53:73:22:F1:D0 was disconnected
construct@[native code]
_construct@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:6669:28
Wrapper@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:6622:64
construct@[native code]
_createSuperInternal@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164178:322
BleError@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164191:26
parseBleError@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164229:30
_callPromise$@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:164477:86
tryCatch@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7486:23
invoke@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7656:32
tryCatch@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7486:23
invoke@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7558:30
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:7570:21
tryCallOne@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24591:16
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:24692:27
_callTimer@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28217:17
_callImmediatesPass@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28256:17
callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:28474:33
__callImmediates@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2555:35
http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2340:34
__guard@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2538:15
flushedQueue@http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2339:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]

设备.tsx

import { StackScreenProps } from '@react-navigation/stack';
import React, { useCallback, useEffect, useState } from 'react';
import { Text, ScrollView, Button, View, StyleSheet } from 'react-native';
import { Service } from 'react-native-ble-plx';
import { ServiceCard } from '../components/ServiceCard';
import { RootStackParamList } from '../Navigator';

import { LogBox } from 'react-native';


LogBox.ignoreLogs([
 'Non-serializable values were found in the navigation state',
 'Each child in a list should have a unique "key" prop.'
]);

function DeviceScreen({
  route,
  navigation,
  
}: StackScreenProps<RootStackParamList, 'Device'>) {
  // get the device object which was given through navigation params
  const { device } = route.params;

  const [isConnected, setIsConnected] = useState(false);
  const [services, setServices] = useState<Service[]>([]);

  // handle the device disconnection
  let disconnectDevice = useCallback(async () => {
    navigation.goBack();
    const isDeviceConnected = await device.isConnected();
    if (isDeviceConnected) {
      await device.cancelConnection();
    }
  }, [device, navigation]);

  useEffect(() => {
    const getDeviceInformations = async () => {
      // connect to the device
      const connectedDevice = await device.connect();
      setIsConnected(true);
    

      // discover all device services and characteristics
      const allServicesAndCharacteristics = await connectedDevice.discoverAllServicesAndCharacteristics();
      // get the services only
      const discoveredServices = await allServicesAndCharacteristics.services();
      setServices(discoveredServices);
    };

    getDeviceInformations();
    
  

    // give a callback to the useEffect to disconnect the device when we will leave the device screen
    return function () {
      disconnectDevice();
    };
  }, [device, disconnectDevice]);

  return (
    <ScrollView contentContainerStyle={styles.container}>
      <Button title="disconnect"  onPress={ (_disconnectDevice)=>navigation.navigate('Home')}/>
      <View>
        <View style={styles.header}>
        
          <Text>{`Name : ${device.name}`}</Text>
          <Text>{`Is connected : ${isConnected}`}</Text>
          <Text>{`UUIDS : ${device.serviceUUIDs}`}</Text>
        </View>
        {/* Display a list of all services */}
        {services &&
          services.map((service) => <ServiceCard service={service} />)}
      </View>
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  container: {
    padding: 12,
  },

  header: {
    backgroundColor: 'teal',
    marginBottom: 12,
    borderRadius: 16,
    shadowColor: 'rgba(60,64,67,0.3)',
    shadowOpacity: 0.4,
    shadowRadius: 10,
    elevation: 4,
    padding: 12,
  },
});

export { DeviceScreen };
  function stopDeviceScan(arg0: boolean) {
    throw new Error('Function not implemented.');
  }


我在 react-native-ble-plx github 存储库中不支持的功能列表中看到 *bonding 外围设备。可能与此有关吗?我们去化验了,一会儿就好了,后来又复发了。这个原因对我来说非常重要。

react-native-ble-plx 回购;

它不支持:

*蓝牙经典设备。

*使用BLE(外设支持)在手机之间进行通信

*绑定外围设备

4

0 回答 0