Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sobag.beaconplayground;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothManager;
- import android.content.Context;
- import android.os.Handler;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import java.util.Date;
- import java.util.HashMap;
- public class MainActivity extends ActionBarActivity
- {
- // ------------------------------------------------------------------------
- // members
- // ------------------------------------------------------------------------
- private static final String LOG_TAG = "MainActivity";
- private BluetoothManager btManager;
- private BluetoothAdapter btAdapter;
- private Handler scanHandler = new Handler();
- private int scan_interval_ms = 5000;
- private boolean isScanning = false;
- // ------------------------------------------------------------------------
- // default stuff...
- // ------------------------------------------------------------------------
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // init BLE
- btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
- btAdapter = btManager.getAdapter();
- scanHandler.post(scanRunnable);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings)
- {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- // ------------------------------------------------------------------------
- // public usage
- // ------------------------------------------------------------------------
- private Runnable scanRunnable = new Runnable()
- {
- @Override
- public void run() {
- if (isScanning)
- {
- if (btAdapter != null)
- {
- btAdapter.stopLeScan(leScanCallback);
- }
- }
- else
- {
- if (btAdapter != null)
- {
- btAdapter.startLeScan(leScanCallback);
- }
- }
- isScanning = !isScanning;
- scanHandler.postDelayed(this, scan_interval_ms);
- }
- };
- // ------------------------------------------------------------------------
- // Inner classes
- // ------------------------------------------------------------------------
- private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback()
- {
- @Override
- public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord)
- {
- int startByte = 2;
- boolean patternFound = false;
- while (startByte <= 5)
- {
- if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
- ((int) scanRecord[startByte + 3] & 0xff) == 0x15)
- { //Identifies correct data length
- patternFound = true;
- break;
- }
- startByte++;
- }
- if (patternFound)
- {
- //Convert to hex String
- byte[] uuidBytes = new byte[16];
- System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
- String hexString = bytesToHex(uuidBytes);
- //UUID detection
- String uuid = hexString.substring(0,8) + "-" +
- hexString.substring(8,12) + "-" +
- hexString.substring(12,16) + "-" +
- hexString.substring(16,20) + "-" +
- hexString.substring(20,32);
- // major
- final int major = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);
- // minor
- final int minor = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
- Log.i(LOG_TAG,"UUID: " +uuid + "\nmajor: " +major +"\nminor" +minor);
- }
- }
- };
- /**
- * bytesToHex method
- */
- static final char[] hexArray = "0123456789ABCDEF".toCharArray();
- private static String bytesToHex(byte[] bytes)
- {
- char[] hexChars = new char[bytes.length * 2];
- for ( int j = 0; j < bytes.length; j++ )
- {
- int v = bytes[j] & 0xFF;
- hexChars[j * 2] = hexArray[v >>> 4];
- hexChars[j * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
- }
- }
Add Comment
Please, Sign In to add comment