Open peixin opened 6 years ago
Solved by waiting for the promise returned by Beacons.addIBeaconsDetection()
.
My code:
async componentDidMount() {
await Beacons.addIBeaconsDetection();
Beacons.startMonitoringForRegion(REGION);
Beacons.startRangingBeaconsInRegion(REGION);
this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
'beaconsDidRange',
data => this.processBeacons(data.beacons),
);
this.regionDidEnterEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidEnter',
data => {
console.log('didEnter', data);
},
);
this.regionDidExitEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidExit',
data => {
console.log('didExit', data);
},
);
}
See https://github.com/MacKentoch/react-native-beacons-manager/blob/master/examples/BeaconsDemo/index.android.js for an example working on Android
@vfaramond i have the same issue i have updated the library and same error getting when ranging start. As you can see the first log Server is connected.
Please check the log :
2019-01-16 18:27:54.414 22574-22628/com.ibeaconapp I/ReactNativeJS: service connected
2019-01-16 18:27:54.401 22574-22629/com.ibeaconapp E/BeaconsAndroidModule: startRanging, error: android.os.RemoteException: The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect() at org.altbeacon.beacon.BeaconManager.applyChangesToServices(BeaconManager.java:966) at org.altbeacon.beacon.BeaconManager.startRangingBeaconsInRegion(BeaconManager.java:814) at com.mackentoch.beaconsandroid.BeaconsAndroidModule.startRanging(BeaconsAndroidModule.java:330) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29) at android.os.Looper.loop(Looper.java:154) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192) at java.lang.Thread.run(Thread.java:761)
here is the code:
//Beacons.detectIBeacons(); //Beacons.addIBeaconsDetection() Beacons.addIBeaconsDetection() .then(() => Beacons.addEddystoneUIDDetection()) .then(() => Beacons.addEddystoneURLDetection()) .then(() => Beacons.addEddystoneTLMDetection()) .then(() => Beacons.addAltBeaconsDetection()) .then(() => Beacons.addEstimotesDetection()) .catch(error => console.log('something went wrong during initialization: ${error}'), );
// we need to wait for service connection to ensure synchronization: this.beaconsServiceDidConnect = Beacons.BeaconsEventEmitter.addListener( 'beaconServiceConnected', () => { console.log('service connected'); this.startRangingAndMonitoring(); }, ); // Ranging: Listen for beacon changes this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener( 'beaconsDidRange', (response: { beacons: Array<{ distance: number, proximity: string, rssi: string, uuid: string, }>, uuid: string, indetifier: string, }) => { console.log('BEACONS: ', response);
},
);
// monitoring:
this.regionDidEnterEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidEnter',
({ identifier, uuid, minor, major }) => {
console.log('regionDidEnter: ', { identifier, uuid, minor, major });
},
);
this.regionDidExitEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidExit',
({ identifier, uuid, minor, major }) => {
console.log('regionDidExit: ', { identifier, uuid, minor, major });
},
);
startRangingAndMonitoring = async () => { const { identifier, uuid } = this.state; const region = { identifier, uuid }; // minor and major are null here
try {
await Beacons.startRangingBeaconsInRegion(region);
console.log('Beacons ranging started successfully');
await Beacons.startMonitoringForRegion(region);
console.log('Beacons monitoring started successfully');
} catch (error) {
throw error;
}
};
Please review the code and point out if i'm doing anything wrong.
Thanks
@MacKentoch SDK link
"react-native-beacons-manager": "github:MacKentoch/react-native-beacons-manager",
iOS app is working fine. ios App can detect the beacons but on android i'm getting
2019-01-16 18:41:53.352 22574-24789/com.ibeaconapp D/BeaconsAndroidModule: rangingConsumer didRangeBeaconsInRegion, region: id1: fda50693-a4e2-4fb1-afcf-c6eb07647825 id2: null id3: null 2019-01-16 18:41:53.358 22574-22628/com.ibeaconapp I/ReactNativeJS: 'BEACONS: ', { beacons: [], uuid: 'fda50693-a4e2-4fb1-afcf-c6eb07647825', identifier: 'welcore BLE' }
And i'm new to React native this is my first app :)
@MacKentoch iOS is working fine, in Android, there are two cases:
The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()
Sometimes the service gets started but then the application crashes.
Version
latest commit: 87a10aa
React Native Version
0.53.3
Platform
Android
OS version
android 5.0
Steps to reproduce
and not working. logcat show errors:
but when I build debug version and debug remote, it working.