如何使用react-native-ble-plx发现服务UUID和特征UUID?

kem*_*ica 2 javascript bluetooth-lowenergy react-native react-native-ble-plx

问题:

我正在尝试连接到我的蓝牙设备,但是在连接并发现所有服务和特征后,我无法继续前进,因为返回的信息不足。我没有收到任何服务 UUID 或任何特征 UUID。

我需要做什么才能访问 UUID,然后才能读取特征?

我尝试过的:

我的设备调用后device.discoverAllServicesAndCharacteristics();返回:

{ _40: 0, _65: 0, _55: null, _72: null }

当我登录时device我得到:

06-23 16:36:54.758  4397  4597 I ReactNativeJS: { isConnectable: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   rssi: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   serviceData: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   overflowServiceUUIDs: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   mtu: 23,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   localName: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   manufacturerData: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   txPowerLevel: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   id: '00:1E:C0:65:D7:DB',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   solicitedServiceUUIDs: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   name: 'RND7DB',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   serviceUUIDs: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:   _manager:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:    { _eventEmitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:       { _subscriber:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:          { _subscriptionsForType:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:             { appStateDidChange:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       isAvailable: true,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       _eventHandlers: { change: {}, memoryWarning: {} },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       currentState: 'active' },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: undefined,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'appStateDidChange',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 } ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               websocketMessage: [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,  ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               websocketOpen: [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,  ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               websocketClosed: [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,  ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               websocketFailed: [ , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,  ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               didUpdateDimensions:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       sharedSubscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       _currentSubscription: null },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: undefined,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'didUpdateDimensions',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 } ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               hardwareBackPress:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       sharedSubscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       _currentSubscription: null },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: undefined,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'hardwareBackPress',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 } ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               collectBugExtraData:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       sharedSubscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       _currentSubscription: null },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function: collectExtraData],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'collectBugExtraData',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 } ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               collectRedBoxExtraData:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       sharedSubscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       _currentSubscription: null },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function: collectExtraData],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: null,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'collectRedBoxExtraData',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 } ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               url:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter: { _subscriber: [Circular] },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: undefined,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'url',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                  { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter: { _subscriber: [Circular] },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: undefined,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'url',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 1 } ],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:               toggleElementInspector:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                [ { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       sharedSubscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       _currentSubscription: null },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    listener: [Function],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    context: undefined,
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    eventType: 'toggleElementInspector',
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    key: 0 },
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                  { subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                    emitter:
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                     { _subscriber: [Circular],
06-23 16:36:54.758  4397  4597 I ReactNativeJS:                       sharedSubscribe
Run Code Online (Sandbox Code Playgroud)

此时,我不知道该怎么办,因为我没有收到任何有价值的信息来进一步从特征中获取信息。

根据文档,我需要 UUID 才能从我的设备中读取。

device.readCharacteristicForService(
  serviceUUID: UUID,
  characteristicUUID: UUID,
  transactionId: ?TransactionId
): Promise<Characteristic>
Run Code Online (Sandbox Code Playgroud)

注意:当我在 Android 上使用 Ble Scanner 应用程序时,我能够通过其 UUID 接收所有服务和特征。

代码片段:

    @action connect = (deviceName) => {

        const device = this.devices.find(device=>device.name === deviceName);

        if(device === undefined){
            this.info = "Couldn't find device";
            return;
        }

        this.manager.stopDeviceScan();
        this.selectedDevice = device;
        this.selectedDevice
            .connect()
            .then((device) => {
                this.info = "Discovering services and characteristics";
                return device.discoverAllServicesAndCharacteristics();
            })
            .then((device) => {
                this.info = "Setting notifications";
                return this.setupNotifications(device)
            })
            .then(() => {
                this.info = "Listening..."
            }, (error) => {
                this.info = error.message;
            })
    };

 async setupNotifications(device) {
    console.log(device);
    console.log(this.manager.servicesForDevice(device.id));
}
Run Code Online (Sandbox Code Playgroud)

github 问题 #293

Hur*_*aki 5

这里\xe2\x80\x99是我如何使用它,希望它能有所帮助

\n\n
 const { updateCharacteristic } = this.props\n const connectedDevice = await manager.connectToDevice(device.id)\n const services = await connectedDevice.discoverAllServicesAndCharacteristics()\n const characteristic = await this.getServicesAndCharacteristics(services)\n\ngetServicesAndCharacteristics(device) {\n            return new Promise((resolve, reject) => {\n                device.services().then(services => {\n                    const characteristics = []\n\n                    services.forEach((service, i) => {\n                        service.characteristics().then(c => {\n                            characteristics.push(c)\n\n                            if (i === services.length - 1) {\n                                const temp = characteristics.reduce(\n                                    (acc, current) => {\n                                        return [...acc, ...current]\n                                    },\n                                    []\n                                )\n                                const dialog = temp.find(\n                                    characteristic =>\n                                        characteristic.isWritableWithoutResponse\n                                )\n                                if (!dialog) {\n                                    reject('No writable characteristic')\n                                }\n                                resolve(dialog)\n                            }\n                        })\n                    })\n                })\n            })\n        }\n
Run Code Online (Sandbox Code Playgroud)\n\n

此致。

\n