I'm working on an Android
app that detects an iBeacon
. Now my problem is that in the LogCat
I can see the correct name of the beacon together with the IP-address
BtGatt.btif btif_gatc_update_properties BLE device name=.. BtGatt GattService onScanResult() IP-address
But the onBeaconServiceConnect()
method still enters the else
part as the collection ( Size
is 0). I have already read the thread concerning this topic in here and searched the internt but couln't find an answer.
My Code:
public class RangingActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "RangingActivity";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
private BluetoothAdapter btAdapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager.setBackgroundScanPeriod(1000);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
logToDisplay("Number of beacons detected: "+beacons.size());
if (beacons.size() > 0) {
EditText editText = (EditText)RangingActivity.this
.findViewById(R.id.rangingText);
Beacon firstBeacon = beacons.iterator().next();
logToDisplay("The first beacon "+firstBeacon.toString());
}else{
logToDisplay("No beacon");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
EditText editText = (EditText)RangingActivity.this
.findViewById(R.id.rangingText);
editText.append(line+"\n");
}
});
}
}