Whenever I use BLEAdvertiser on IOS is returns null values. I am a n00b so I am would be very happy to hear I missed a permissions setting or something but I cannot for the life of me get this to work. It works perfectly in Android. Here is my code:
import {NativeEventEmitter, NativeModules} from 'react-native';
import {RCTBluetoothManager} 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';
// Uses the Apple code to pick up iPhones
const APPLE_ID = 0xFFFF;
const MANUF_DATA = [0X42];
// No scanner filters (finds all devices inc iPhone). Use UUID suffix to filter scans if using.
const SCAN_MANUF_DATA = Platform.OS === 'android' ? null : MANUF_DATA;
//const SCAN_MANUF_DATA = MANUF_DATA;
if (Platform.OS === 'android'){
BLEAdvertiser.setCompanyId(APPLE_ID);
}
Whenever I use BLEAdvertiser on IOS is returns null values. I am a n00b so I am would be very happy to hear I missed a permissions setting or something but I cannot for the life of me get this to work. It works perfectly in Android. Here is my code:
import React, {Component} from 'react';
import { Alert, SafeAreaView, StyleSheet, View, Text, TouchableOpacity, FlatList, Platform, } from 'react-native';
import {NativeEventEmitter, NativeModules} from 'react-native';
import {RCTBluetoothManager} 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';
// Uses the Apple code to pick up iPhones const APPLE_ID = 0xFFFF; const MANUF_DATA = [0X42]; // No scanner filters (finds all devices inc iPhone). Use UUID suffix to filter scans if using. const SCAN_MANUF_DATA = Platform.OS === 'android' ? null : MANUF_DATA; //const SCAN_MANUF_DATA = MANUF_DATA;
if (Platform.OS === 'android'){ BLEAdvertiser.setCompanyId(APPLE_ID); }
//BLEAdvertiser.setCompanyId(APPLE_ID);
export async function requestLocationPermission() { try { if (Platform.OS === 'android') { const fineLocationGranted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, ); const bleAdvertiseGranted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE, ); const bleScanGranted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN, ); const bluetoothConnectGranted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT, ); const finLocGrantMessage = fineLocationGranted == PermissionsAndroid.RESULTS.GRANTED ? 'Location Permission granted':'Location Permission denied'; const bleAdvGrantMessage = bleAdvertiseGranted == PermissionsAndroid.RESULTS.GRANTED ? 'BLE Advertise Permission granted':'BLE Advertise Permission denied'; const bleScanGrantMessage = bleScanGranted == PermissionsAndroid.RESULTS.GRANTED ? 'BLE Scan Permission granted':'BLE Scan Permission denied'; const bluetoothConGrantMessage = bluetoothConnectGranted == PermissionsAndroid.RESULTS.GRANTED ? 'Bluetooth Connect Permission granted':'Bluetooth Connect Permission Denied'; console.log(finLocGrantMessage); console.log(bleAdvGrantMessage); console.log(bleScanGrantMessage); console.log(bluetoothConGrantMessage); }
} catch (err) { console.warn(err); } }
class Entry extends Component { constructor(props) { const uuid = '123e4567-e89b-12d3-a456-426655440000'; super(props); this.state = { //uuid: '', uuid: 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, {
}
componentDidMount() { requestLocationPermission(); //const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser); //eventEmitter.addListener('onDeviceFound', (event) => { // BLEAdvertiser.setCompanyId(APPLE_ID); //}); console.log("Running start() in the componentDidMount function");
}
// componentDidMount() { // requestLocationPermission(); // UUIDGenerator.getRandomUUID((newUid) => { // this.setState({ // uuid: newUid.slice(0, -2) + '00', // }); // }); //}
componentWillUnmount() { if (this.state.isLogging) { this.stop(); } }
start() {
}
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));
}
short(str) { return ( str.substring(0, 4) + ' ... ' + str.substring(str.length - 4, str.length) ).toUpperCase(); }
render() { return (
} }
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;