Skip to content

Device Connecting

Dariusz Seweryn edited this page Feb 2, 2018 · 6 revisions

To connect to a device one needs to first [scan it]https://github.com/Polidea/react-native-ble-plx/wiki/Bluetooth-Scanning).

When a Device is scanned it is possible to establish a connection by calling:

bleManager.connectToDevice(
  deviceIdentifier: DeviceId,
  options: ?ConnectionOptions,
): Promise<Device>
  • deviceIdentifier: DeviceId—is obtained from device.id
    • Android—it is the MAC address
    • iOS—it is an UUID
  • options: ?ConnectionOptions—Connection specific options to be passed before connection happen.
    • autoConnect?: boolean—Whether to directly connect to the remote device (false) or to automatically connect as soon as the remote device becomes available (true). [Android only]
    • requestMTU?: number—Whether MTU size will be negotiated to this value. It is not guaranteed to get it after connection is successful.

The promise resolves when the device get's connected

When the connection is no longer needed one should disconnect by either calling:

device.cancelConnection(): Promise<Device>

or

bleManager.cancelDeviceConnection(deviceIdentifier: DeviceId): Promise<Device>

In both situations the returned Promise will be resolved with the same (closed) Device

Note:

  • on some Android devices connecting when a scan is running may cause problems
  • Android's autoConnect flag is a bit peculiar.
    • When set to false the OS connects to a device on highest available duty cycle but if the device is not available in 30 seconds the connection fails.
    • When set to true the OS periodically checks if the device is available and connects to it as soon as it is scanned. There is no timeout involved but the scan window is quite small to preserve battery. Usually this takes longer than if called with autoConnect=false if the device is advertising nearby. There is one difference from the Android vanilla API: in the vanilla API if the device has been connected with autoConnect=true and later will get disconnected, a reconnect attempt will happen automatically without the user interaction—this is not the case when using this library.
Clone this wiki locally