Skip to content

Instantly share code, notes, and snippets.

@ifvanagustrianto
Created May 22, 2015 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifvanagustrianto/a07b8d2783047c4e089f to your computer and use it in GitHub Desktop.
Save ifvanagustrianto/a07b8d2783047c4e089f to your computer and use it in GitHub Desktop.
private BluetoothAdapter mBluetoothAdapter;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 10000;
@Override
public void onCreate() {
if (beaconManager == null)
{
Log.d(APP_TAG,"create new beaconmanager");
try{
beaconManager = new BeaconManager(getBaseContext());
}
catch (Exception e)
{
Log.d(APP_TAG,e.toString());
}
}
mHandler = new Handler();
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
// finish();
stopSelf();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
stopSelf();
return;
}
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
Log.d(APP_TAG,"Call beacon Scan");
scanLeDevice(true);
}
}, 0, 35, TimeUnit.SECONDS);
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("Device Found", "Device : " + device.getName());
Log.d("Device Found", "Device : " + device.getAddress());
}
});
}
};
private void runOnUiThread(Runnable runnable) {
mHandler.post(runnable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment