Guest User

Untitled

a guest
Jul 10th, 2020
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.flic2;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7.  
  8. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  9. import com.google.android.material.snackbar.Snackbar;
  10.  
  11. import androidx.annotation.NonNull;
  12. import androidx.appcompat.app.AppCompatActivity;
  13. import androidx.appcompat.widget.Toolbar;
  14. import androidx.core.content.ContextCompat;
  15.  
  16. import android.os.Handler;
  17. import android.view.View;
  18.  
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.widget.Toast;
  22.  
  23. import io.flic.flic2libandroid.Flic2Button;
  24. import io.flic.flic2libandroid.Flic2Manager;
  25. import io.flic.flic2libandroid.Flic2ScanCallback;
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. Toolbar toolbar = findViewById(R.id.toolbar);
  34. setSupportActionBar(toolbar);
  35.  
  36.  
  37.  
  38. // Initialize the Flic2 manager to run on the same thread as the current thread (the main thread)
  39. Flic2Manager manager = Flic2Manager.initAndGetInstance(getApplicationContext(), new Handler());
  40.  
  41. FloatingActionButton fab = findViewById(R.id.fab);
  42. fab.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View view) {
  45. scanFlicButton();
  46. }
  47. });
  48. }
  49.  
  50. @Override
  51. public boolean onCreateOptionsMenu(Menu menu) {
  52. // Inflate the menu; this adds items to the action bar if it is present.
  53. getMenuInflater().inflate(R.menu.menu_main, menu);
  54. return true;
  55. }
  56.  
  57. @Override
  58. public boolean onOptionsItemSelected(MenuItem item) {
  59. // Handle action bar item clicks here. The action bar will
  60. // automatically handle clicks on the Home/Up button, so long
  61. // as you specify a parent activity in AndroidManifest.xml.
  62. int id = item.getItemId();
  63.  
  64. //noinspection SimplifiableIfStatement
  65. if (id == R.id.action_settings) {
  66. return true;
  67. }
  68.  
  69. return super.onOptionsItemSelected(item);
  70. }
  71.  
  72. public void scanFlicButton(){
  73. int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
  74.  
  75. if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
  76. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  77. requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
  78. }
  79. return;
  80. }
  81.  
  82.  
  83.  
  84. System.out.println("Searching....");
  85. Flic2Manager.getInstance().startScan(new Flic2ScanCallback() {
  86. @Override
  87. public void onDiscoveredAlreadyPairedButton(Flic2Button button) {
  88. System.out.println("FOUND");
  89. // Found an already paired button. Try another button.
  90. }
  91.  
  92. @Override
  93. public void onDiscovered(String bdAddr) {
  94. // Found Flic2, now connecting...
  95. System.out.println("DISCOVER");
  96. }
  97.  
  98. @Override
  99. public void onConnected() {
  100. // Connected. Now pairing...
  101. System.out.println("Connected");
  102. }
  103.  
  104. @Override
  105. public void onComplete(int result, int subCode, Flic2Button button) {
  106. if (result == Flic2ScanCallback.RESULT_SUCCESS) {
  107. // Success!
  108. // The button object can now be used
  109. System.out.println("Complete");
  110. } else {
  111. // Failed
  112. System.out.println("Fail: " + result + " " + subCode);
  113. }
  114. }
  115. });
  116. }
  117.  
  118.  
  119. @Override
  120. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  121. if (requestCode == 1) {
  122. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  123. // now startScan can be safely called
  124. } else {
  125. Toast.makeText(getApplicationContext(), "Scanning needs Location permission, which you have rejected", Toast.LENGTH_SHORT).show();
  126. }
  127. }
  128. }
  129. }
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.

×