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.
Why do you wrap this.scanAndConnect(); in onStateChange?
Is the idea that scan should start when you turn on your Bluetooth? Cold you just add onClick trigger for the scan, make sure it works? Because scanAndConnect looks good and should return console logs.
@michalkow i tried scanning device on onClick also.i haven't added the view part here..but i have called scanAndConnect with onPress
It should work then I think... Are you using it for Android or iOS? Did you set up all Bluetooth permissions? Did you successfully run react-native-ble-plx example app Sniffator?
i am not sure about the Sniffator..but i turned on my bluetooth other bluetooth device and my test device connected to my computer through usb and its bluetooth is also turned on..and started scanning the device..but it couldn't find my other device whose bluetooth is on.
Try to download and use Sniffator so you can be sure it is problem with your code, and not with the device
what if sniffator is able to scan the devices..i have been trying to resolve this issue for 3-4 days..no luck at all.
Please send me a minimal javascript file which is responsible for scanning. I'll test it.
Hi @Cierpliwy below is the code..i have reduced my code
`import React, { Component } from 'react'; import {BleManager} from 'react-native-ble-plx';
class Settings extends Component { // eslint-disable-line
constructor(props) { super(props); this.manager = new BleManager(); 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); }
render() {
return (
<View >
<Button style={styles.scannerButton} onPress={this.scan}>
<Icon name='bluetooth' style={{color: 'white', fontSize: 20, fontWeight: '800'}}/>
<Text style={{color: 'white', fontSize: 20}}>Scan</Text>
<Spinner color="#fff"/>
</Button>
</View>
</View>
);
} }
export default connect(null, null)(Settings); `
@Cierpliwy @michalkow i downloaded Sniffator and started scanning..but it doesn't find bluetooth device..the other device is ios..I suspect that it couldn't find ios bluetooth device? or scanning device from android to ios or vice-versa doesn't work?
I am not sure what you are trying to do, but it sounds like this: https://stackoverflow.com/questions/18884705/transfer-data-between-ios-and-android-via-bluetooth
I have simplified your code a bit:
// @flow
import React, { Component } from "react";
import { Button, View, Text } from "react-native";
import { BleManager } from "react-native-ble-plx";
export default class TestComponent extends Component {
constructor(props) {
super(props);
this.manager = new BleManager();
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);
}
render() {
return (
<View style={{flex: 1}}>
<Button onPress={this.scanAndConnect} title={"CLICK!"}>
<Text style={{ color: "white", fontSize: 20 }}>Scan</Text>
</Button>
</View>
);
}
}
It works fine on Android. I need to wait for my iPod to be charged before I can verify it on iOS.
@Cierpliwy so that means it works in andorid to android.i was checking from android to ios.Please verify it and let me know..very much thanks
On iOS it works fine. Please add log level to your code:
import { BleManager, LogLevel } from "react-native-ble-plx";
...
this.manager.setLogLevel(LogLevel.Verbose)
...
and check logs in XCode/Android Studio. On iOS it looks like this:
[RxBLEKit|DEBG|16:44:07.452]: CentralManager(401912704) didDiscover(peripheral: Peripheral(uuid: EA22F5F6-7EB0-0C8B-57DB-855A6650AEAE, name: Optional("BLE Name")), rssi: -94)
[RxBLEKit|DEBG|16:44:07.469]: CentralManager(401912704) didDiscover(peripheral: Peripheral(uuid: 21BAC7C4-34EE-AA5B-FFD5-AD99CCB05FAF, name: nil), rssi: -90)
[RxBLEKit|DEBG|16:44:07.471]: CentralManager(401912704) didDiscover(peripheral: Peripheral(uuid: 5450896F-2654-34AC-ACED-E485FF307422, name: nil), rssi: -82)
@Cierpliwy thank you very much..let me try.
@Cierpliwy Could you pls try scanning from android to search ios ble..
@Cierpliwy Have you checked the scanning between android and ios?
What do you mean by "scanning between android and ios"? Did you create iOS application which is advertising in peripheral mode and want to scan it with Android?
@Cierpliwy can scanning through android device find the ios ble(uuid) and vice-versa? I also like to inform you that i am trying to scan through my android device in debugging mode which is connected to my computer through usb and app ran by react-native run-android.its running in my actual device and i have opened my iphone bluetooth..but it doesn't find it.thank you
Well you can't normally scan iOS device. You have to have specific application which will advertise in Peripheral mode to do so.
@Cierpliwy what actually advertise implies to?i am not sure about it.could you pls explain this advertising peripheral mode?