Guest User

Untitled

a guest
Dec 3rd, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package com.sobag.beaconplayground;
  2.  
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.bluetooth.BluetoothManager;
  6. import android.content.Context;
  7. import android.os.Handler;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13.  
  14. import java.util.Date;
  15. import java.util.HashMap;
  16.  
  17.  
  18. public class MainActivity extends ActionBarActivity
  19. {
  20.  
  21. // ------------------------------------------------------------------------
  22. // members
  23. // ------------------------------------------------------------------------
  24.  
  25. private static final String LOG_TAG = "MainActivity";
  26.  
  27. private BluetoothManager btManager;
  28. private BluetoothAdapter btAdapter;
  29. private Handler scanHandler = new Handler();
  30. private int scan_interval_ms = 5000;
  31. private boolean isScanning = false;
  32.  
  33. // ------------------------------------------------------------------------
  34. // default stuff...
  35. // ------------------------------------------------------------------------
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState)
  39. {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42.  
  43. // init BLE
  44. btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
  45. btAdapter = btManager.getAdapter();
  46.  
  47. scanHandler.post(scanRunnable);
  48. }
  49.  
  50. @Override
  51. public boolean onCreateOptionsMenu(Menu menu)
  52. {
  53. // Inflate the menu; this adds items to the action bar if it is present.
  54. getMenuInflater().inflate(R.menu.menu_main, menu);
  55. return true;
  56. }
  57.  
  58. @Override
  59. public boolean onOptionsItemSelected(MenuItem item)
  60. {
  61. // Handle action bar item clicks here. The action bar will
  62. // automatically handle clicks on the Home/Up button, so long
  63. // as you specify a parent activity in AndroidManifest.xml.
  64. int id = item.getItemId();
  65.  
  66. //noinspection SimplifiableIfStatement
  67. if (id == R.id.action_settings)
  68. {
  69. return true;
  70. }
  71.  
  72. return super.onOptionsItemSelected(item);
  73. }
  74.  
  75. // ------------------------------------------------------------------------
  76. // public usage
  77. // ------------------------------------------------------------------------
  78.  
  79. private Runnable scanRunnable = new Runnable()
  80. {
  81. @Override
  82. public void run() {
  83.  
  84. if (isScanning)
  85. {
  86. if (btAdapter != null)
  87. {
  88. btAdapter.stopLeScan(leScanCallback);
  89. }
  90. }
  91. else
  92. {
  93. if (btAdapter != null)
  94. {
  95. btAdapter.startLeScan(leScanCallback);
  96. }
  97. }
  98.  
  99. isScanning = !isScanning;
  100.  
  101. scanHandler.postDelayed(this, scan_interval_ms);
  102. }
  103. };
  104.  
  105. // ------------------------------------------------------------------------
  106. // Inner classes
  107. // ------------------------------------------------------------------------
  108.  
  109. private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback()
  110. {
  111. @Override
  112. public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord)
  113. {
  114. int startByte = 2;
  115. boolean patternFound = false;
  116. while (startByte <= 5)
  117. {
  118. if ( ((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
  119. ((int) scanRecord[startByte + 3] & 0xff) == 0x15)
  120. { //Identifies correct data length
  121. patternFound = true;
  122. break;
  123. }
  124. startByte++;
  125. }
  126.  
  127. if (patternFound)
  128. {
  129. //Convert to hex String
  130. byte[] uuidBytes = new byte[16];
  131. System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
  132. String hexString = bytesToHex(uuidBytes);
  133.  
  134. //UUID detection
  135. String uuid = hexString.substring(0,8) + "-" +
  136. hexString.substring(8,12) + "-" +
  137. hexString.substring(12,16) + "-" +
  138. hexString.substring(16,20) + "-" +
  139. hexString.substring(20,32);
  140.  
  141. // major
  142. final int major = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);
  143.  
  144. // minor
  145. final int minor = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);
  146.  
  147. Log.i(LOG_TAG,"UUID: " +uuid + "\nmajor: " +major +"\nminor" +minor);
  148. }
  149.  
  150. }
  151. };
  152.  
  153. /**
  154. * bytesToHex method
  155. */
  156. static final char[] hexArray = "0123456789ABCDEF".toCharArray();
  157. private static String bytesToHex(byte[] bytes)
  158. {
  159. char[] hexChars = new char[bytes.length * 2];
  160. for ( int j = 0; j < bytes.length; j++ )
  161. {
  162. int v = bytes[j] & 0xFF;
  163. hexChars[j * 2] = hexArray[v >>> 4];
  164. hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  165. }
  166. return new String(hexChars);
  167. }
  168.  
  169.  
  170. }
Add Comment
Please, Sign In to add comment