react-native-ble-plx: couldn't detect nearby bluetooth device
Hi i couldn’t get nearby device even the bluetooth is turn on in other device. `constructor(props) { super(props); this.state = { text: ‘’}; this.manager = new BleManager(); this.scan = this.scan.bind(this); this.error = this.error.bind(this); this.scanAndConnect = this.scanAndConnect.bind(this); }
scanAndConnect() { this.manager.startDeviceScan(null, null, (error, device) => { console.log(error); console.log(device); }) }
componentWillMount() { const subscription = this.manager.onStateChange((state) => { if (state === ‘PoweredOn’) { this.scanAndConnect(); subscription.remove(); } }, true); }`
Scan and connect should provide me the nearby device.but doesn’t show anything…i tried multiple times.no luck.Also i am testing in real device.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 27 (6 by maintainers)
const scanDevices = async () => { const btState = await manager.state() console.log(btState,“btstate scandevices”) if (btState!==“PoweredOn”) { alert(“Bluetooth is not powered on”); return (false); } const permission = await requestPermission(); console.log(permission,“permission”) if (permission) { manager.startDeviceScan(null, null, async (error, device) => { if (error) { console.log(error); return } if (device) { console.log(device) // console.log(
${device.name} (${device.id})}); const newScannedDevices = scannedDevices; newScannedDevices[device.id] = device; await setDeviceCount(Object.keys(newScannedDevices).length); await setScannedDevices(scannedDevices); } }) } }using react-native-ble-plx not detecting all nearby devices ?
Devices using BLE work in one of two modes: Central (which this library allows) and Peripheral. In most of use cases applications on your phone implement Central mode which allows them to properly scan, connect to and communicate with devices. This is normal flow.
There is also possibility to create and application in Peripheral mode on iOS and newest Androids. This is not supported by this library, your iPhone/Android by itself won’t start working as Peripheral. You need to create/install application which does just that. When it is running, it advertises itself as a specific BLE device and may allow one Central to connect to it.
I recommend that your read more about how BLE works.
On iOS it works fine. Please add log level to your code:
and check logs in XCode/Android Studio. On iOS it looks like this: