मैंने हाल ही में एस्टिमोट बीकन का परीक्षण शुरू किया है, और मैं बीकन क्षेत्र में प्रवेश करते समय पृष्ठभूमि सेवा से अधिसूचना लॉन्च करने की कोशिश कर रहा हूं, लेकिन दुर्भाग्य से मेरा समाधान काम नहीं करता है। यह त्रुटियां नहीं देता है लेकिन एक बीकन की खोज होने पर अधिसूचना लॉन्च नहीं की जा रही है। मुझे नहीं पता कि यह कुछ कोड त्रुटि है या बस इसे करने का तरीका गलत है। मैं this अन्य सवाल पढ़ा है, लेकिन यह कुछ अलग लग रहा है कि मैं क्या उपयोग करने के बाद से एक गतिविधि के बजाय एक सेवा है, लेकिन शायद इस सवाल का जवाब समान है (एप्लिकेशन संदर्भ से संबंधित) ...पृष्ठभूमि में एक सेवा से एस्टिमोट बीकन क्षेत्र का पता लगाने
यहाँ मेरी सेवा कोड
हैpublic class BeaconsMonitoringService extends Service{
private BeaconManager beaconManager;
private String user;
@Override
public void onCreate() {
// Configure BeaconManager.
beaconManager = new BeaconManager(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Beacons monitoring service starting", Toast.LENGTH_SHORT).show();
user = intent.getStringExtra("user");
// Check if device supports Bluetooth Low Energy.
if (!beaconManager.hasBluetooth()||!beaconManager.isBluetoothEnabled()) {
Toast.makeText(this, "Device does not have Bluetooth Low Energy or it is not enabled", Toast.LENGTH_LONG).show();
this.stopSelf();
}
connectToService();
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Beacons monitoring service done", Toast.LENGTH_SHORT).show();
}
private void connectToService() {
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
notifyEnterRegion(0);
// try {
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);
Log.i("BEACOON ", "ANTES DE");
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
Log.i("BEACOON ", String.valueOf(beacons.get(1).getMinor()));
for (Beacon beacon: beacons){
Log.i("BEACOON ", String.valueOf(beacon.getMinor()));
if (beacon.getMinor() == 64444) {
notifyEnterRegion(6444);
} else if (beacon.getMinor() == 36328) {
notifyEnterRegion(36328);
} else if (beacon.getMinor() == 31394) {
notifyEnterRegion(31394);
}
}
}
@Override
public void onExitedRegion(Region region) {
notifyExitRegion();
}
});
}
});
}
public void notifyEnterRegion(int code) {
Toast.makeText(this, "Beacon "+code, Toast.LENGTH_SHORT).show();
Intent targetIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new Notification.Builder(this)
.setContentTitle("Bienvenido "+user+"!")
.setContentText("Sólo por estar aquí has ganado....")
.setSmallIcon(com.smt.beaconssmt.R.drawable.beacon_gray)
.setContentIntent(contentIntent)
.getNotification();
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(1, noti);
}
public void notifyExitRegion(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hasta pronto!")
.setTitle(user+", estás abandonando la zona de beacons");
builder.setPositiveButton("Ver web", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
Intent i = new Intent(BeaconsMonitoringService.this, WebViewActivity.class);
i.putExtra("web", "http://www.google.com/");
startActivity(i);
}
});
builder.setNegativeButton("Adios!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
मैं किसी भी तरह की मदद की सराहना करता हूं, अग्रिम धन्यवाद!
ठीक है, यह समाधान काम कर रहा है, धन्यवाद! लेकिन मुझे इसके बारे में एक संदेह है, आप "निकटता की गणना कैसे करते हैं"? एसडीके या आधिकारिक कोड में कुछ है या आपका स्वयं का समाधान है? तो इससे मैं समझता हूं कि BeaconManager.setMonitoringListener() इस मामले में प्रयोग योग्य नहीं है और हमें इसके बजाय setRangingListener() का उपयोग करना है ?? – Hugo
सटीकता की गणना करने के लिए कोड एसडीके में होना चाहिए। लेकिन मैं एक कस्टम एसडीके कार्यान्वयन का उपयोग कर रहा हूं इसलिए मैंने आपको कोड दिखाने के लिए उत्तर संपादित किया। SetMonitoringListener के बारे में मुझे यह काम नहीं मिल सका, इसलिए मैं इस समाधान का उपयोग करना समाप्त कर देता हूं। – jDur
मैं अपने ज़ैमरिन एप्लिकेशन के लिए एकीकृत कर रहा हूं, यहां एंटररेगियन और ExitRegion क्रमशः कई बार कॉल करता है, पता नहीं क्यों? मैंने इसके बारे में पढ़ा है, यह तब तभी ट्रिगर होना चाहिए जब डिवाइस क्षेत्र की सीमा के अंतर्गत आता है। लेकिन जब यह रेंज में आता है तो यह अनुक्रमिक रूप से ट्रिगर होता है जब तक कि डिवाइस बीकन की सीमा से बाहर नहीं जाता। कृपया इस –