connectionStatus.setText("Searching");
connectionStatus.setTextColor(Color.YELLOW);
deviceScanner.scanForDevices(new ConfigurableDevicesScanner.ScannerCallback() {
@Override public void onDevicesFound(List<ConfigurableDevicesScanner.ScanResultItem> devices) {
item = devices.get(0);
// just take the first scanned item
connection = connectionProvider.getConnection(item.device);
// establish connection to scanned device
connection.connect(new DeviceConnectionCallback() {
@Override
public void onConnected() {
connectionStatus.setText("Connected");
connectionStatus.setTextColor(Color.GREEN);
// update the connection status on UI
startCollection.setClickable(true);
// update the clickable for start collection button
beaconID.setText(item.device.deviceId.toString().toCharArray(), 0, item.device.deviceId.toString().toCharArray().length);
// update the ID of connected beacon
}
@Override
public void onDisconnected() {
connectionStatus.setText("Disconnected");
connectionStatus.setTextColor(Color.RED);
// update the connection status on UI
startCollection.setClickable(false);
// update the clickable for start collection button
}
@Override
public void onConnectionFailed(DeviceConnectionException e) {
connectionStatus.setText("Connection Fail");
connectionStatus.setTextColor(Color.RED);
// update the connection status on UI
startCollection.setClickable(false);
// update the clickable for start collection button
}
});
// scan and build connection to the node
}
});
This is the main part of code I get from the tutorial and apply to my app. However, during the test, my app never find the beacon right beside it and the status stuck at the “Searching”. I’m not sure if there are any problem about my code. Could anyone give me some help?
public class MainActivity extends AppCompatActivity {
private BeaconManager beaconManager;
private String scanId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("------ Start Test ------");
beaconManager = new BeaconManager(this);
beaconManager.setNearableListener(new BeaconManager.NearableListener() {
@Override
public void onNearablesDiscovered(List<Nearable> nearables) {
for (Nearable item : nearables) {
System.out.println(item.identifier);
}
}
});
}
@Override
protected void onStart() {
super.onStart();
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
scanId = beaconManager.startNearableDiscovery();
}
});
}
@Override
protected void onStop() {
super.onStop();
beaconManager.stopNearableDiscovery(scanId);
}
}
This is my second try to read the broadcasting package. Still, there are nothing happened.