vitorpamplona / react-native-ble-advertiser

Bluetooth Advertiser/Scanner for React Native.
60 stars 38 forks source link

Not able to advertise #33

Closed pranavmadhani closed 2 years ago

pranavmadhani commented 2 years ago

this is my manifest.xml file and i have copied the code from sample app. Scanning is working but cannot broadcast need help.

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
  <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

 <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
 <uses-feature android:name="android.hardware.bluetooth" android:required="false"/>

import React, {Component } from 'react'; import BeaconBroadcast from './screen/BeaconBroadcast'; import {check, checkMultiple, PERMISSIONS, RESULTS} from 'react-native-permissions';

import { SafeAreaView, StyleSheet, ScrollView, View, Text, TouchableOpacity, FlatList, } from 'react-native';

import { Alert, Platform } from 'react-native'; import { NativeEventEmitter, NativeModules } from 'react-native';

import update from 'immutability-helper'; import BLEAdvertiser from 'react-native-ble-advertiser' import UUIDGenerator from 'react-native-uuid-generator'; import { PermissionsAndroid } from 'react-native'; import BTScanner from './screen/BTScanner'; import BTBroadcast from './screen/BTBroadcast';

// Uses the Apple code to pick up iPhones const APPLE_ID = 0x4C; const MANUF_DATA = [0,1];

BLEAdvertiser.setCompanyId(APPLE_ID);

export async function requestLocationPermission() { try { if (Platform.OS === 'android') { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, { 'title': 'BLE Avertiser Example App', 'message': 'Example App access to your location ' } ) if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log('[Permissions]', 'Location Permission granted') } else { console.log('[Permissions]', 'Location Permission denied') } }

const blueoothActive = await BLEAdvertiser.getAdapterState().then(result => {
  console.log('[Bluetooth]', 'Bluetooth Status', result)
  return result === "STATE_ON";
}).catch(error => { 
  console.log('[Bluetooth]', 'Bluetooth Not Enabled')
  return false;
});

if (!blueoothActive) {
  await Alert.alert(
    'Example requires bluetooth to be enabled',
    'Would you like to enable Bluetooth?',
    [
      {
        text: 'Yes',
        onPress: () => BLEAdvertiser.enableAdapter(),
      },
      {
        text: 'No',
        onPress: () => console.log('Do Not Enable Bluetooth Pressed'),
        style: 'cancel',
      },
    ],
  )
}

} catch (err) { console.warn(err) } }

class Entry extends Component { constructor(props) { super(props); this.state = { uuid:'', isLogging: false, devicesFound:[] } }

addDevice(_uuid, _name, _mac, _rssi, _date) {
  const index = this.state.devicesFound.findIndex( ({ uuid }) => uuid == _uuid);
  if (index<0) {
    this.setState({
      devicesFound: update(this.state.devicesFound, 
        {$push: [{uuid:_uuid, name:_name, mac: _mac, rssi:_rssi, start:_date, end:_date}]}
      )
    });
  } else {
    this.setState({
      devicesFound: update(this.state.devicesFound, 
        {[index]: {end: {$set: _date}, rssi: {$set: _rssi || this.state.devicesFound[index].rssi }}}
      )
    });
  }
}

componentDidMount(){
  requestLocationPermission();

  checkMultiple([
    PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
    PERMISSIONS.ANDROID.INTERNET,
    PERMISSIONS.ANDROID.BLUETOOTH,
    PERMISSIONS.ANDROID.BLUETOOTH_ADMIN,
  ])

.then((result) => { console.log('Permission result', result); }) .catch((error) => { console.log(error) });

  UUIDGenerator.getRandomUUID((newUid) => {
    this.setState({
      uuid: newUid.slice(0, -2) + '00'
    });
  });
}

componentWillUnmount() {
  if (this.state.isLogging)
    this.stop();
}

start() {
  console.log(this.state.uuid, "Registering Listener");
  const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser);

  this.onDeviceFound = eventEmitter.addListener('onDeviceFound', (event) => {
    console.log('onDeviceFound', event);
    if (event.serviceUuids) {
      for(let i=0; i< event.serviceUuids.length; i++){
        if (event.serviceUuids[i] && event.serviceUuids[i].endsWith('00'))
          this.addDevice(event.serviceUuids[i], event.deviceName, event.deviceAddress, event.rssi, new Date())   
      }
    }
  });

  //ADVETISER
 console.log(this.state.uuid, "Starting Advertising");
  BLEAdvertiser.broadcast(this.state.uuid, MANUF_DATA, {
     })
    .then(sucess => console.log(this.state.uuid, "Adv Successful", sucess))
    .catch(error => console.log(this.state.uuid, "Adv Error", error));

    //SCANNER
  console.log(this.state.uuid, "Starting Scanner");
  BLEAdvertiser.scan(MANUF_DATA, {scanMode: BLEAdvertiser.SCAN_MODE_BALANCED})
  .then(sucess => console.log(this.state.uuid, "Scan Successful", sucess))
  .catch(error => console.log(this.state.uuid, "Scan Error", error));

  this.setState({
    isLogging: true,
  });
}

stop(){
  console.log(this.state.uuid, "Removing Listener");
  this.onDeviceFound.remove();
  delete this.onDeviceFound;

  console.log(this.state.uuid, "Stopping Broadcast");
  BLEAdvertiser.stopBroadcast()
    .then(sucess => console.log(this.state.uuid, "Stop Broadcast Successful", sucess))
    .catch(error => console.log(this.state.uuid, "Stop Broadcast Error", error));

  console.log(this.state.uuid, "Stopping Scanning");
  BLEAdvertiser.stopScan()
    .then(sucess => console.log(this.state.uuid, "Stop Scan Successful", sucess) )
    .catch(error => console.log(this.state.uuid, "Stop Scan Error", error) );

  this.setState({
    isLogging: false,
  });
}

short(str) {
  return (str.substring(0, 4) + " ... " + str.substring(str.length-4, str.length)).toUpperCase(); 
}

render() {
  return (
   // <BTScanner></BTScanner>
   // <BTBroadcast></BTBroadcast>
    <SafeAreaView>
      <View style={styles.body}>
        <View style={styles.sectionContainer}>
          <Text style={styles.sectionTitle}>BLE Advertiser Demo</Text>
          <Text style={styles.sectionDescription}>Broadcasting: <Text style={styles.highlight}>{ this.short(this.state.uuid) }</Text></Text>
        </View>

        <View style={styles.sectionContainer}>
          {this.state.isLogging ? (
          <TouchableOpacity
            onPress={() => this.stop()}
            style={styles.stopLoggingButtonTouchable}>
            <Text style={styles.stopLoggingButtonText}>
              Stop
            </Text>
          </TouchableOpacity>
            ) : (
          <TouchableOpacity
            onPress={() => this.start()}
            style={styles.startLoggingButtonTouchable}>
            <Text style={styles.startLoggingButtonText}>
              Start
            </Text>
          </TouchableOpacity>
          )}
        </View>

        <View style={styles.sectionContainerFlex}>
          <Text style={styles.sectionTitle}>Devices Around</Text>
          <FlatList
              data={ this.state.devicesFound }
              renderItem={({item}) => <Text style={styles.itemPastConnections}>{this.short(item.uuid)} {item.mac} {item.rssi}</Text>}
              keyExtractor={item => item.uuid}
              />
        </View>

        <View style={styles.sectionContainer}>
          <TouchableOpacity
            onPress={() => this.setState({ devicesFound: [] }) }
            style={styles.startLoggingButtonTouchable}>
            <Text style={styles.startLoggingButtonText}>
              Clear Devices
            </Text>
          </TouchableOpacity>
        </View>

      </View>
    </SafeAreaView>
  );
}

}

const styles = StyleSheet.create({ body: { height: "100%", }, sectionContainerFlex: { flex: 1, marginTop: 12, marginBottom: 12, paddingHorizontal: 24, }, sectionContainer: { flex: 0, marginTop: 12, marginBottom: 12, paddingHorizontal: 24, }, sectionTitle: { fontSize: 24, marginBottom: 8, fontWeight: '600', textAlign: 'center' }, sectionDescription: { fontSize: 18, fontWeight: '400', textAlign: 'center', }, highlight: { fontWeight: '700', }, startLoggingButtonTouchable: { borderRadius: 12, backgroundColor: '#665eff', height: 52, alignSelf: 'center', width: 300, justifyContent: 'center', }, startLoggingButtonText: { fontSize: 14, lineHeight: 19, letterSpacing: 0, textAlign: 'center', color: '#ffffff', }, stopLoggingButtonTouchable: { borderRadius: 12, backgroundColor: '#fd4a4a', height: 52, alignSelf: 'center', width: 300, justifyContent: 'center', }, stopLoggingButtonText: { fontSize: 14, lineHeight: 19, letterSpacing: 0, textAlign: 'center', color: '#ffffff', }, listPastConnections: { width: "80%", height: 200 }, itemPastConnections: { padding: 3, fontSize: 18, fontWeight: '400', }, });

export default Entry;