detect iBeacon using altbeacon library - android

I am developing project uses iBeacons library
Is this the code that recognize my Beacon ?
beaconManager.startMonitoringBeaconsInRegion(new Region("D1B024CB-A02D-4650-9C6A-BAEDA8A31F0E", null, null, null));
i have UUID Major and Minor, how to use them ?

Hope so this will help you
public class RangingActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "RangingActivity";
private static final String UUID = "XXXXXXXXXXXXXXXXXXXX";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager.bind(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
#Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", Identifier.parse(UUID), null, null));
} catch (RemoteException e) { }
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
}
}
in AndroidManifest.xml
permissions
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register
<service
android:name="org.altbeacon.beacon.service.BeaconService"
android:enabled="true"
android:exported="true"
android:isolatedProcess="false"
android:label="beacon" >
</service>
<service
android:name="org.altbeacon.beacon.BeaconIntentProcessor"
android:enabled="true" >
</service>
<receiver android:name="org.altbeacon.beacon.startup.StartupBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>

pass major and minor in place of null
Region region = new Region("myRangingUniqueId", uUID, major, minor);

Related

How to make an application start on factory reset/first startup?

I am looking to have some basic forms run on my android device after it is reset or on the first boot like most smartphones ask you to connect to Wi-Fi, register the device etc. I haven't worked with native android development previously so what are my options and what areas should I look into?
Try this
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MsgPushService.class);
context.startService(service);
}
}
MyService.java
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), MyService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".MyService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

What is the best method to run applets in the background - Android

I am developing an application that sends location data to a database. As I need this task to be executed in the background, I used the Service type to send the data, but when the application goes to the background, the service stops executing after a few seconds. I researched the subject and found the JobScheduler class, but this class performs services in the background with a very long time interval (15min) and I needed the service to be performed at least every 2 min and to be canceled only by the user. Is there a way to do this? I thank anyone who can help.
https://developer.android.com/reference/android/app/job/JobScheduler
My service:
public class GPSservice extends Service {
private String TAG = "INICIAR SERVIÇO GPS";
private Context context;
private HandlerThread handlerThread;
private Handler handler;
private final int TEMPO_ENTRE_NOTIFICAÇOES_SEGUNDOS = 60; //tempo de sessenta segundos
#Override
public void onCreate() {
handlerThread = new HandlerThread("HandlerThread");
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
//Previne que seja executado em subsequentes chamadas a onStartCommand
if(!handlerThread.isAlive()) {
Log.d("NotifyService","Notificações iniciadas");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
Runnable runnable = new Runnable() {
#Override
public void run() {
AtualizarLocalizacaoBanco();
handler.postDelayed(this, 1000 * TEMPO_ENTRE_NOTIFICAÇOES_SEGUNDOS);
}
};
handler.post(runnable);
}
return Service.START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("NotifyService","Notificações Finalizadas");
Intent broadcastIntent = new Intent(this, BroadCastReceiver.class);
sendBroadcast(broadcastIntent);
//handlerThread.quit();
}
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
}
public void AtualizarLocalizacaoBanco(){
context = this;
ModeloAtualizacao model = new ModeloAtualizacao();
SharedPreferences preferences = this.context.getSharedPreferences("user_preferences", MODE_PRIVATE);
model.setId(preferences.getString("usuario_id", ""));
model.setLati(preferences.getString("user_latitude", ""));
model.setLongi(preferences.getString("user_longitude", ""));
model.setEmpresa(preferences.getString("usuario_empresa", ""));
model.setEscola(preferences.getString("usuario_escola", ""));
Call<String> call = new RetrofitConfig().postAtualizacao().atualizarLocalizacao(model);
call.enqueue(new Callback<String>() {
#Override
public void onResponse(Call<String> call, Response<String> response) {
Log.d("Sucesso: ", "ok");
}
#Override
public void onFailure(Call<String> call, Throwable t) {
Log.d("Erro atualizar", t.getMessage());
}
});
}
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".ActivityGPS"></activity>
<activity android:name=".Activity_Lista_Aluno" />
<activity
android:name=".ProfessorActivity"
android:label="#string/title_activity_professor" />
<service
android:name=".GPSservice"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar" />
<activity android:name=".Activity_Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Lock Screen Volume Control with CCL and without CCL

I can listen volume level in my VolumeService. This service listen to volume level in background.
My problem is: I can't control volume in lock screen.
I tried two different ways for volume control
First Way with CCL
Application Class:
public class CastApplication extends Application {
private static String APPLICATION_ID;
public static final double VOLUME_INCREMENT = 0.03;
#Override
public void onCreate() {
super.onCreate();
APPLICATION_ID = getString(R.string.app_id);
// initialize VideoCastManager
VideoCastManager.initialize(this, APPLICATION_ID, VideoCastControllerActivity.class, null).
setVolumeStep(VOLUME_INCREMENT).enableFeatures(VideoCastManager.FEATURE_NOTIFICATION |VideoCastManager.FEATURE_LOCKSCREEN |
VideoCastManager.FEATURE_WIFI_RECONNECT | VideoCastManager.FEATURE_CAPTIONS_PREFERENCE |VideoCastManager.FEATURE_DEBUGGING);
}
}
MainActivity:
public class MainActivity extends FragmentActivity {
private VideoCastManager mCastManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoCastManager.checkGooglePlayServices(this);
mCastManager = VideoCastManager.getInstance();
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mCastManager.onDispatchVolumeKeyEvent(event, CastApplication.VOLUME_INCREMENT)) {
return true;
}
return super.dispatchKeyEvent(event);
}
}
Second way without CCL (with RRC)
MainActivity:
public class MainActivity extends FragmentActivity {
ContentObserver mSettingsContentObserver;
RemoteControlClient remoteControlClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_RING,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
RemoteControlClientCompat mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,mRemoteControlClientCompat);
remoteControlClient = new RemoteControlClient((PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0)));
MediaRouter.getInstance(this.getApplicationContext()).addRemoteControlClient(remoteControlClient);
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
}
Service and ContentObserver are same for both way
VolumeService:
public class VolumeService extends Service {
ContentObserver mSettingsContentObserver;
#Override
public void onCreate() {
super.onCreate();
mSettingsContentObserver = new SettingsContentObserver(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,mSettingsContentObserver);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
}
}
ContentObserver:
public class SettingsContentObserver extends ContentObserver {
int previousVolume;
Context context;
public SettingsContentObserver(Context c, Handler handler) {
super(handler);
context = c;
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
previousVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int delta = previousVolume - currentVolume;
if (delta > 0) {
Log.i("MyService", "Volume is down");
previousVolume = currentVolume;
} else if (delta < 0) {
Log.i("MyService", "Volume is up");
previousVolume = currentVolume;
}
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:name="com.yns.volumelock.CastApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" />
</intent-filter>
</service>
<service android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService" />
<activity
android:name="com.google.android.libraries.cast.companionlibrary.cast.tracks.CaptionsPreferenceActivity"
android:label="#string/action_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service
android:name="com.yns.volumelock.VolumeService"
android:enabled="true" >
</service>
</application>

Android Studio Estimote Beacon Application

im trying to make basic beacon application in android studio. I just want to scan beacons and list them into the screen. Here are my codes. I took them from somewhere.
public class MainActivity extends ActionBarActivity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);
private BeaconManager beaconManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
#Override public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
Log.d("TAG", "Ranged beacons: " + beacons);
}
});
}
#Override
protected void onStart() {
super.onStart();
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG","Cannot start ranging", e);
}
}
});
}
#Override
protected void onStop() {
super.onStop();
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e("TAG", "Cannot stop but it does not matter now", e);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
beaconManager.disconnect();
}
}
Here is my manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oem.estimote_ibeacon_app" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.estimote.sdk.service.BeaconService"
android:exported="false"/>
</application>
</manifest>
When i opened application it says "stopped" please help. I have beacons for test. Where is my mistake? Thank you.
it seems that ALL_ESTIMOTE_BEACONS in
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
is null.
enter a valid regionid instead of "regionid" in
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", null, null, null);

how to bind HCE hostapduservice to main activity on android

hy..i have a task to make my kitkat-nexus to act as a tag. I have ACS 122U as reader. i have read the program example in this site http://blog.opendatalab.de/hack/2013/11/07/android-host-card-emulation-with-acr122/. then i tryed the code on my own eclipse.
main activity :
public class MainActivity extends Activity implements OnMessageReceived, ReaderCallback {
private NfcAdapter nfcAdapter;
private ListView listView;
private IsoDepAdapter isoDepAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
isoDepAdapter = new IsoDepAdapter(getLayoutInflater());
listView.setAdapter(isoDepAdapter);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
Log.i("end of onCreate-----","onCreate HCE");
}
#Override
public void onResume() {
super.onResume();
//nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
// null);
//nfcAdapter.disableReaderMode(this); //tambahan poipo
Log.i("onResume---", "onResume");
}
#Override
public void onPause() {
super.onPause();
nfcAdapter.disableReaderMode(this);
Log.i("onPause---", "onPause");
}
#Override
public void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
Thread thread = new Thread(transceiver);
Log.i("dibawah thread", "ontagdiscovered");
thread.start();
}
#Override
public void onMessage(final byte[] message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
isoDepAdapter.addMessage(new String(message));
Log.i("didlmrun---", "onMessage");
}
});
Log.i("diluarrun---", "onMessage");
}
#Override
public void onError(Exception exception) {
onMessage(exception.getMessage().getBytes());
}
}
hostapduservice :
...
...
...
#Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i("HCEDEMO====", "Application selected====");
return getWelcomeMessage();
}
else {
Log.i("HCEDEMO======", "Received: =====" + new String(apdu));
return getNextMessage();
}
}
...
...
...
then in the manifest file :
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="FEATURE_NFC_HOST_CARD_EMULATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MyHostApduService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
<activity
android:name="de.grundid.hcedemo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
ok,,when i ran the above source code,, i saw my acs122u blinking continously when i tapped my nexus near to it. but i didn't see the log.i(....) from hostapdu service. In the eclipse log cat, there were just some log.i from main activity. what should i do to bind that hostapdu service to main activity, so my nexus can act as a tag...???
thanks in advance... :-)

Categories

Resources