Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth connection examples #15

Closed
tarang9211 opened this issue Jul 19, 2016 · 6 comments
Closed

Bluetooth connection examples #15

tarang9211 opened this issue Jul 19, 2016 · 6 comments
Labels

Comments

@tarang9211
Copy link

Could you post some more examples on how to connect to a device?

I tried using the examples from the README.md to connect my iphone to my mac using the UUID and MAC address, but none of them worked.

Thanks!

@marcosinigaglia
Copy link
Member

Hi, you have to scan for the peripherals and then use the id of the peripheral to connect.

@tarang9211
Copy link
Author

So first I would call BleManage.scan() and then connect right? So will the scan() method return a list of UUID or MAC addresses of active bluetooth devices?

@marcosinigaglia
Copy link
Member

First you have to handle BleManagerDiscoverPeripheral event, then start the scan and you receive the peripheral finded.

@tarang9211
Copy link
Author

tarang9211 commented Jul 22, 2016

Hi Marco, I tried that. Does this code look right?

class bluetooth extends Component {
  render() {
    return (
        <View style = { styles.container } >
            <TouchableHighlight onPress = { this.btnPress }>
                <Text>Bluetooth test</Text>
            </TouchableHighlight>
        </View>
    );
  }

  btnPress() {

    console.log(typeof BleManager === 'undefined');

    console.log(DeviceEventEmitter.addListener());

    DeviceEventEmitter.addListener('BleManagerDiscoverPeripheral', (args) => {
        console.log(args)
        BleManager.scan(args.id, 5)
        .then(() => {
            console.log('here');
        })
    });

  }
}

The code for console.log(args) does not seem to be called at all.

@marcosinigaglia
Copy link
Member

Try this:

componentDidMount() {
  this.handleDiscoverPeripheral = this.handleDiscoverPeripheral.bind(this);

  NativeAppEventEmitter.addListener(
    'BleManagerDiscoverPeripheral',
    this.handleDiscoverPeripheral
  ); 
}

btnPress() {
  BleManager.scan([], 5);
}

handleDiscoverPeripheral(data){
  console.log(data);
}

@DarrylD
Copy link
Contributor

DarrylD commented Aug 26, 2016

That example would be awesome for documentation example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants