Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.78 KB | None | 0 0
  1. package com.example.bandoo.opportunisticbledemo;
  2.  
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.bluetooth.BluetoothManager;
  6. import android.bluetooth.le.AdvertiseCallback;
  7. import android.bluetooth.le.AdvertiseData;
  8. import android.bluetooth.le.AdvertiseSettings;
  9. import android.bluetooth.le.BluetoothLeAdvertiser;
  10. import android.bluetooth.le.BluetoothLeScanner;
  11. import android.bluetooth.le.ScanCallback;
  12. import android.bluetooth.le.ScanFilter;
  13. import android.bluetooth.le.ScanRecord;
  14. import android.bluetooth.le.ScanResult;
  15. import android.bluetooth.le.ScanSettings;
  16. import android.content.Context;
  17. import android.content.Intent;
  18. import android.content.pm.PackageManager;
  19. import android.os.Handler;
  20. import android.support.v7.app.AppCompatActivity;
  21. import android.os.Bundle;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.List;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31. // Appcontext
  32. Context ctx = null;
  33.  
  34. // Logging
  35. private String logString = "";
  36. private TextView logView;
  37.  
  38. private void initLog() {
  39. logView = (TextView)findViewById(R.id.AppLog);
  40. log("Actvity Created");
  41. }
  42. private void log(String str) {
  43. logString += str + "\n";
  44. logView.setText(logString);
  45. }
  46.  
  47. // BT specific stuff
  48. protected Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  49. private static final int REQUEST_ENABLE_BT = 1;
  50. private BluetoothAdapter mBluetoothAdapter = null;
  51.  
  52. // Activity life-cycle
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.activity_main);
  57.  
  58. ctx = getApplicationContext();
  59.  
  60. initLog();
  61.  
  62. // Use this check to determine whether BLE is supported on the device. Then
  63. // you can selectively disable BLE-related features.
  64. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  65. Toast.makeText(this, "BLE is not supported on this device", Toast.LENGTH_LONG).show();
  66. finish();
  67. } else {
  68. log("BLE hardware available");
  69. }
  70.  
  71. // Initializes Bluetooth adapter.
  72. final BluetoothManager bluetoothManager =
  73. (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  74.  
  75. mBluetoothAdapter = bluetoothManager.getAdapter();
  76.  
  77. // Ensures Bluetooth is available on the device and it is enabled. If not,
  78. // displays a dialog requesting user permission to enable Bluetooth.
  79. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
  80. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  81. } else {
  82. startDemo();
  83. }
  84. }
  85.  
  86. @Override
  87. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  88. // Check which request we're responding to
  89. if (requestCode == REQUEST_ENABLE_BT) {
  90. // Make sure the request was successful
  91. if (resultCode == RESULT_OK) {
  92. startDemo();
  93. } else {
  94. Toast.makeText(this, "Bluetooth must be turned on for this demo", Toast.LENGTH_LONG).show();
  95. finish();
  96. }
  97. }
  98. }
  99.  
  100. @Override
  101. protected void onDestroy() {
  102. // PART II.
  103. mHandler.removeCallbacksAndMessages(null);
  104. if (mScanner != null) {
  105. enableScanning(false);
  106. }
  107. // Part IV.
  108. if (mAdvertiser != null) {
  109. enableAdvertising(false);
  110. }
  111. if (mBluetoothAdapter.isEnabled()) {
  112. mBluetoothAdapter.disable();
  113. }
  114. super.onDestroy();
  115. }
  116.  
  117. // Demo specific functions
  118. private void startDemo() {
  119. log("BLE adapter enabled\n - MAC_ADDR: " + mBluetoothAdapter.getAddress());
  120.  
  121. //Part II.
  122. mHandler = new Handler();
  123. mScanner = mBluetoothAdapter.getBluetoothLeScanner();
  124. mScanParams = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
  125. enableScanning(true);
  126.  
  127. // Part III. meg kell írni !
  128. //updateNeptunCodesToForward("VALAKI");
  129.  
  130. byte forward_test[] = new byte[] {0,0,0,0,0,0,8,78,8,8,22,1,2,3,4,5,6,7,8,9,8,7,6,5};
  131. updateData(forward_test);
  132. //showStats();
  133.  
  134. // Part IV.
  135. mAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
  136. mAdvParams = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY).build();
  137. enableAdvertising(true);
  138. }
  139.  
  140. //Part II.
  141.  
  142. private Handler mHandler;
  143.  
  144. protected BluetoothLeScanner mScanner = null;
  145. private ScanSettings mScanParams;
  146. private int scanError = 0;
  147. // Stops scanning after 10 seconds.
  148. private static final long SCAN_PERIOD = 10000;
  149.  
  150. // Device scan callback.
  151. private ScanCallback mScanCallback = new ScanCallback() {
  152. @Override
  153. public void onScanResult(int callbackType, ScanResult result) {
  154. super.onScanResult(callbackType, result);
  155. BluetoothDevice device = result.getDevice();
  156. log("New Device Found: "+device.getAddress());
  157. processAllData(result.getScanRecord());
  158. }
  159.  
  160. @Override
  161. public void onBatchScanResults(List<ScanResult> results) {
  162. super.onBatchScanResults(results);
  163. for (int i = 0; i < results.size(); i++) {
  164. BluetoothDevice device = results.get(i).getDevice();
  165. log("New Device Found: "+device.getAddress());
  166. processAllData(results.get(i).getScanRecord());
  167. }
  168. }
  169.  
  170. @Override
  171. public void onScanFailed(int errorCode) {
  172. super.onScanFailed(errorCode);
  173. scanError = errorCode;
  174. }
  175. };
  176.  
  177. private void enableScanning(final boolean enable) {
  178. if (enable) {
  179. if ( scanError == 0) {
  180. // Stops scanning after a pre-defined scan period.
  181. mHandler.postDelayed(new Runnable() {
  182. @Override
  183. public void run() {
  184. mScanner.stopScan(mScanCallback);
  185. log("Scanning Stopped");
  186. enableScanning(true);
  187. }
  188. }, SCAN_PERIOD);
  189. mScanner.startScan(new ArrayList<ScanFilter>(), mScanParams, mScanCallback);
  190. } else {
  191. Toast.makeText(this, "Scanning Failed - errorCode="+scanError, Toast.LENGTH_LONG).show();
  192. }
  193. log("Scanning Started");
  194. } else {
  195. mScanner.stopScan(mScanCallback);
  196. log("Scanning Stopped");
  197. }
  198.  
  199. }
  200.  
  201. // Part III.
  202. // Demo app specific datasets
  203.  
  204.  
  205. //teszt
  206. // private ArrayList<String> NeptunCodes = new ArrayList<String>();
  207. //mine
  208. private byte[] AllData = new byte[24];
  209.  
  210. private void showStats() {
  211. //String statString = "Neptun\n";
  212. /* for (int i = 0; i < NeptunCodes.size(); i++) {
  213. statString += NeptunCodes.get(i);
  214. }
  215. logView.setText(statString);*/
  216. String write_out= Arrays.toString(AllData);
  217.  
  218. logView.setText(write_out);
  219.  
  220. }
  221.  
  222. /* private void updateNeptunCodesToForward(String NeptunCode) {
  223. NeptunCodes.add(NeptunCode);
  224. showStats();
  225. }
  226. */
  227. private void updateData(byte[] Data)
  228. {
  229.  
  230. for (int i = 0; i<23; i++)
  231. {
  232.  
  233. AllData[i]=Data[i];
  234. }
  235. showStats();
  236.  
  237. }
  238.  
  239.  
  240. // Manufacturer Id.
  241. private static final int MANUFACTURER_ID = 0x01DC; // iParking Ltd.
  242.  
  243. // BLE-specific functions
  244. /* private void processScanData(ScanRecord record) {
  245. byte mData[] = record.getManufacturerSpecificData(MANUFACTURER_ID);
  246. if ( mData == null) {
  247. // No MSData for us
  248. }
  249. else {
  250. String neptunCode = "";
  251. for (int i = 0; i < 6; i++) {
  252. neptunCode+=(char)mData[i];
  253. }
  254. updateNeptunCodesToForward(neptunCode);
  255. //showStats();
  256. }
  257. }
  258. */
  259. private void processAllData(ScanRecord record)
  260. {
  261. byte mData[] = record.getManufacturerSpecificData(MANUFACTURER_ID);
  262. if ( mData == null) {
  263. // No MSData for us
  264.  
  265. }
  266. else
  267. {
  268. for(int i = 0; i<23; i++)
  269. {
  270. AllData[i] = mData[i];
  271.  
  272. }
  273. }
  274. }
  275.  
  276.  
  277.  
  278. // Part IV.
  279.  
  280. // private String currentNeptunCode= "Valami";
  281.  
  282.  
  283. // ezt kell átírni
  284. /* private String getNextNeptunCodeToForward() {
  285.  
  286.  
  287. return currentNeptunCode;
  288.  
  289. }
  290. */
  291. private byte[] currentData = new byte[24];
  292.  
  293. protected BluetoothLeAdvertiser mAdvertiser = null;
  294. private AdvertiseSettings mAdvParams;
  295. private int advError = 0;
  296. // Restarts advertising after 0,5 seconds
  297. private static final long ADV_PERIOD = 500;
  298. // Periodic start of advertising could freeze the stack
  299. private static final long RESTART_AFTER_ADV_COUNT = 1000;
  300. private int advCount = 0;
  301.  
  302.  
  303.  
  304. /* private AdvertiseData getNextAdvertisingData() {
  305. currentNeptunCode = getNextNeptunCodeToForward();
  306. byte msData[] = new byte[6]; // Neptun Code
  307. for (int i = 0; i < 6; i++) {
  308. msData[i] = (byte)currentNeptunCode.charAt(i);
  309. }
  310. return new AdvertiseData.Builder().addManufacturerData(MANUFACTURER_ID, msData).build();
  311. }
  312. */
  313. private AdvertiseData getNextAdvData()
  314. {
  315.  
  316. byte msData[] = new byte[24];
  317. for (int i = 0; i < 23; i++) {
  318.  
  319. msData[i] = AllData[i];
  320. }
  321. return new AdvertiseData.Builder().addManufacturerData(MANUFACTURER_ID, msData).build();
  322.  
  323. }
  324.  
  325.  
  326. // Advertising Callback
  327. private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
  328. @Override
  329. public void onStartSuccess(AdvertiseSettings settingsInEffect) {
  330. super.onStartSuccess(settingsInEffect);
  331. advCount++;
  332. if ( advCount == RESTART_AFTER_ADV_COUNT ) {
  333. advCount = 0;
  334. mScanner.stopScan(mScanCallback);
  335. mAdvertiser.stopAdvertising(mAdvertiseCallback);
  336. mHandler.removeCallbacksAndMessages(null);
  337. mBluetoothAdapter.disable();
  338. mAdvertiser = null;
  339. mScanner = null;
  340. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  341. } else {
  342. // Restarts advertising after a pre-defined advertising period.
  343. mHandler.postDelayed(new Runnable() {
  344. @Override
  345. public void run() {
  346. mAdvertiser.stopAdvertising(mAdvertiseCallback);
  347. // updateNeptunCodesToForward(currentNeptunCode);
  348. updateData(AllData);
  349. enableAdvertising(true);
  350. }
  351. }, ADV_PERIOD);
  352. }
  353. }
  354.  
  355. @Override
  356. public void onStartFailure(int errorCode) {
  357. super.onStartFailure(errorCode);
  358. advError = errorCode;
  359. Toast.makeText(ctx, "Advertising Failed - errorCode="+advError, Toast.LENGTH_LONG).show();
  360. }
  361. };
  362.  
  363. private void enableAdvertising(final boolean enable) {
  364. if (enable) {
  365. mAdvertiser.startAdvertising(mAdvParams, getNextAdvData(), mAdvertiseCallback);
  366. } else {
  367. mAdvertiser.stopAdvertising(mAdvertiseCallback);
  368. }
  369.  
  370. }
  371.  
  372.  
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement