react-native-ble-plx icon indicating copy to clipboard operation
react-native-ble-plx copied to clipboard

couldn't detect nearby bluetooth device

Open biks152207 opened this issue 9 years ago • 27 comments

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.

biks152207 avatar Jun 05 '17 08:06 biks152207

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 avatar Jun 05 '17 15:06 michalkow

@michalkow i tried scanning device on onClick also.i haven't added the view part here..but i have called scanAndConnect with onPress

biks152207 avatar Jun 05 '17 15:06 biks152207

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?

michalkow avatar Jun 05 '17 15:06 michalkow

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.

biks152207 avatar Jun 05 '17 15:06 biks152207

Try to download and use Sniffator so you can be sure it is problem with your code, and not with the device

michalkow avatar Jun 05 '17 15:06 michalkow

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.

biks152207 avatar Jun 05 '17 15:06 biks152207

Please send me a minimal javascript file which is responsible for scanning. I'll test it.

Cierpliwy avatar Jun 05 '17 15:06 Cierpliwy

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 ( Scan nearby device

      <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); `

biks152207 avatar Jun 05 '17 16:06 biks152207

@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?

biks152207 avatar Jun 05 '17 16:06 biks152207

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

michalkow avatar Jun 05 '17 16:06 michalkow

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 avatar Jun 05 '17 16:06 Cierpliwy

@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

biks152207 avatar Jun 05 '17 16:06 biks152207

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 avatar Jun 05 '17 16:06 Cierpliwy

@Cierpliwy thank you very much..let me try.

biks152207 avatar Jun 05 '17 17:06 biks152207

@Cierpliwy Could you pls try scanning from android to search ios ble..

biks152207 avatar Jun 05 '17 18:06 biks152207

@Cierpliwy Have you checked the scanning between android and ios?

biks152207 avatar Jun 06 '17 02:06 biks152207

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 avatar Jun 06 '17 08:06 Cierpliwy

@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

biks152207 avatar Jun 06 '17 10:06 biks152207

Well you can't normally scan iOS device. You have to have specific application which will advertise in Peripheral mode to do so.

Cierpliwy avatar Jun 06 '17 12:06 Cierpliwy

@Cierpliwy what actually advertise implies to?i am not sure about it.could you pls explain this advertising peripheral mode?

biks152207 avatar Jun 07 '17 01:06 biks152207