Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* updates alann-maulana#101

* improve null check for event sink and fix typo
  • Loading branch information
anthonyhunter authored Oct 26, 2021
1 parent ce0a061 commit 0c63b2d
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions android/src/main/java/com/flutterbeacon/FlutterBeaconScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.RemoteException;
import android.os.Looper;
import android.os.Handler;
import android.util.Log;

import org.altbeacon.beacon.Beacon;
Expand All @@ -27,6 +29,8 @@ class FlutterBeaconScanner {
private final FlutterBeaconPlugin plugin;
private final WeakReference<Activity> activity;

private Handler handler;

private EventChannel.EventSink eventSinkRanging;
private EventChannel.EventSink eventSinkMonitoring;
private List<Region> regionRanging;
Expand All @@ -35,6 +39,7 @@ class FlutterBeaconScanner {
public FlutterBeaconScanner(FlutterBeaconPlugin plugin, Activity activity) {
this.plugin = plugin;
this.activity = new WeakReference<>(activity);
handler = new Handler(Looper.getMainLooper());
}

final EventChannel.StreamHandler rangingStreamHandler = new EventChannel.StreamHandler() {
Expand Down Expand Up @@ -120,10 +125,17 @@ void stopRanging() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
if (eventSinkRanging != null) {
Map<String, Object> map = new HashMap<>();
final Map<String, Object> map = new HashMap<>();
map.put("region", FlutterBeaconUtils.regionToMap(region));
map.put("beacons", FlutterBeaconUtils.beaconsToArray(new ArrayList<>(collection)));
eventSinkRanging.success(map);
handler.post(new Runnable(){
@Override
public void run() {
if(eventSinkRanging != null){
eventSinkRanging.success(map);
}
}
});
}
}
};
Expand Down Expand Up @@ -205,31 +217,52 @@ void stopMonitoring() {
@Override
public void didEnterRegion(Region region) {
if (eventSinkMonitoring != null) {
Map<String, Object> map = new HashMap<>();
final Map<String, Object> map = new HashMap<>();
map.put("event", "didEnterRegion");
map.put("region", FlutterBeaconUtils.regionToMap(region));
eventSinkMonitoring.success(map);
handler.post(new Runnable(){
@Override
public void run() {
if(eventSinkMonitoring != null){
eventSinkMonitoring.success(map);
}
}
});
}
}

@Override
public void didExitRegion(Region region) {
if (eventSinkMonitoring != null) {
Map<String, Object> map = new HashMap<>();
final Map<String, Object> map = new HashMap<>();
map.put("event", "didExitRegion");
map.put("region", FlutterBeaconUtils.regionToMap(region));
eventSinkMonitoring.success(map);
handler.post(new Runnable(){
@Override
public void run() {
if(eventSinkMonitoring != null){
eventSinkMonitoring.success(map);
}
}
});
}
}

@Override
public void didDetermineStateForRegion(int state, Region region) {
if (eventSinkMonitoring != null) {
Map<String, Object> map = new HashMap<>();
final Map<String, Object> map = new HashMap<>();
map.put("event", "didDetermineStateForRegion");
map.put("state", FlutterBeaconUtils.parseState(state));
map.put("region", FlutterBeaconUtils.regionToMap(region));
eventSinkMonitoring.success(map);
handler.post(new Runnable(){
@Override
public void run() {
if(eventSinkMonitoring != null){
eventSinkMonitoring.success(map);
}
}
});
}
}
};
Expand Down

0 comments on commit 0c63b2d

Please sign in to comment.