Guest User

AndroidCode

a guest
Jul 3rd, 2020
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. MainActivity.java:
  12. import android.Manifest;
  13. import android.app.Activity;
  14. import android.bluetooth.BluetoothAdapter;
  15. import android.bluetooth.BluetoothDevice;
  16. import android.bluetooth.BluetoothManager;
  17. import android.bluetooth.le.BluetoothLeScanner;
  18. import android.bluetooth.le.ScanCallback;
  19. import android.bluetooth.le.ScanResult;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.content.pm.PackageManager;
  23. import android.location.LocationManager;
  24. import android.os.AsyncTask;
  25. import android.os.Bundle;
  26. import android.os.Handler;
  27. import android.util.Log;
  28. import android.view.View;
  29. import android.widget.Button;
  30. import android.widget.TextView;
  31.  
  32. import androidx.appcompat.app.AppCompatActivity;
  33. import androidx.core.app.ActivityCompat;
  34. import androidx.core.content.ContextCompat;
  35.  
  36. import java.util.HashMap;
  37. import java.util.Map;
  38.  
  39. public class MainActivity extends AppCompatActivity {
  40.  
  41. private static final String TAG = "BLE_TESTApp";
  42.  
  43. private Button searchBtn;
  44.  
  45. private TextView bleDevicesTextView;
  46.  
  47. private BluetoothManager bluetoothManager;
  48. private BluetoothAdapter bluetoothAdapter;
  49. private BluetoothLeScanner bluetoothLeScannerLeScanner;
  50.  
  51. private final static int REQUEST_ENABLE_BT = 1;
  52.  
  53. private Handler handler = new Handler();
  54. private static final long INTERVAL = 5000;
  55.  
  56. private Context mContext;
  57.  
  58. private int deviceCounter = 0;
  59. private Map devicesDescription = new HashMap();
  60. private Map devices = new HashMap();
  61.  
  62. private ScanCallback leScanCallback = new ScanCallback() {
  63. @Override
  64. public void onScanResult(int callbackType, ScanResult result) {
  65. BluetoothDevice device = result.getDevice();
  66.  
  67. Log.i(TAG, device.getAddress().toString());
  68. String text = String.format("%d - %s, %s\r\n", deviceCounter, device.getName(), device.getAddress());
  69. bleDevicesTextView.setText(bleDevicesTextView.getText().toString().concat(text));
  70.  
  71. devices.put(deviceCounter, device);
  72. deviceCounter++;
  73. }
  74. };
  75.  
  76. @Override
  77. protected void onCreate(Bundle savedInstanceState) {
  78. super.onCreate(savedInstanceState);
  79. setContentView(R.layout.activity_main);
  80.  
  81. searchBtn = findViewById(R.id.searchBtn);
  82.  
  83. bleDevicesTextView = findViewById(R.id.bleDevicesTextView);
  84.  
  85. mContext = this;
  86.  
  87. if ( !hasBlePermissions(this) ) {
  88. requestBlePermissions(this, 1);
  89. }
  90.  
  91. areLocationServicesEnabled(this);
  92.  
  93. bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  94. bluetoothAdapter = bluetoothManager.getAdapter();
  95. bluetoothLeScannerLeScanner = bluetoothAdapter.getBluetoothLeScanner();
  96.  
  97. if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
  98. Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  99. startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
  100. }
  101.  
  102. searchBtn.setOnClickListener(new View.OnClickListener() {
  103. @Override
  104. public void onClick(View v) {
  105. resetFoundDevices();
  106. Log.i(TAG, "Suche nach BLE Geräte > beginn");
  107. searchBtn.setEnabled(false);
  108.  
  109. AsyncTask.execute(new Runnable() {
  110. @Override
  111. public void run() {
  112. bluetoothLeScannerLeScanner.startScan(leScanCallback);
  113. }
  114. });
  115.  
  116. handler.postDelayed(new Runnable() {
  117. @Override
  118. public void run() {
  119. Log.i(TAG, "Suche nach BLE Geräte > ende");
  120. searchBtn.setEnabled(true);
  121. AsyncTask.execute(new Runnable() {
  122. @Override
  123. public void run() {
  124. bluetoothLeScannerLeScanner.stopScan(leScanCallback);
  125. }
  126. });
  127. }
  128. }, INTERVAL);
  129. }
  130. });
  131.  
  132. }
  133.  
  134. public boolean hasBlePermissions( Context mContext) {
  135. if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
  136. != PackageManager.PERMISSION_GRANTED ||
  137. ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)
  138. != PackageManager.PERMISSION_GRANTED) {
  139. return false;
  140. } else {
  141. return true;
  142. }
  143. }
  144.  
  145. public void requestBlePermissions(final Activity activity, int requestCode) {
  146. ActivityCompat.requestPermissions(activity,
  147. new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
  148. requestCode);
  149. }
  150.  
  151. public boolean areLocationServicesEnabled(Context context) {
  152. LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  153. try {
  154. return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
  155. locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  156. } catch (Exception e) {
  157. e.printStackTrace();
  158. return false;
  159. }
  160. }
  161.  
  162. private void resetFoundDevices() {
  163. bleDevicesTextView.setText("");
  164. devices.clear();
  165. deviceCounter = 0;
  166. }
  167. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×