1. Are you ready for the Galaxy S20? Here is everything we know so far!

Bluetooth Alarm Clock App

Discussion in 'Android Development' started by johnholley123, Apr 17, 2019.

  1. johnholley123

    johnholley123 Lurker
    Thread Starter

    I am trying to create a Bluetooth alarm clock and I'm using Arduino as my receiver to make what I need to happen when the alarm goes off, but I'm having trouble getting it to work. I have tried merging an Android alarm clock app and an Arduino Bluetooth LED Controller app together and I have tried to find an already completed Bluetooth Alarm Clock app for Arduino, but have had no luck so far. I found a complete bluetooth alarm clock app here but can't figure out how to use it and I don't know enough about how it works to figure it out. Please let me know if you know of any existing apps or what I'm doing wrong. I can upload what I have for the first solution if I need to.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. johnholley123

    johnholley123 Lurker
    Thread Starter

    Code (Java):
    1. public class MainActivity extends AppCompatActivity {
    2.  
    3.  
    4.  
    5.     public static final java.lang.String arduinoModuleName = "HC-05";
    6.     private int toastDuration = Toast.LENGTH_LONG;
    7.  
    8.     public BluetoothDevice queryDevices(java.lang.String deviceToFind, BluetoothAdapter btAdapter){
    9.         Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    10.  
    11.         boolean isAMatchingDevice = false;
    12.         BluetoothDevice matchingDevice = null;
    13.  
    14.         Context context = getApplicationContext(); //for Toast
    15.         Toast toast = Toast.makeText(context,"Found Arduino Module in Paired Devices", toastDuration);
    16.  
    17.         if (pairedDevices.size() > 0) {
    18.             // There are paired devices. Get the name and address of each paired device.
    19.  
    20.             for (BluetoothDevice device : pairedDevices) {
    21.                 String deviceName = device.getName();
    22.                 String deviceHardwareAddress = device.getAddress(); // MAC address
    23.  
    24.                 if (deviceName.equals(deviceToFind)) {
    25.                     isAMatchingDevice = true;
    26.                     matchingDevice = device;
    27.                 }
    28.             }
    29.  
    30.         }
    31.  
    32.         if(!isAMatchingDevice){
    33.             toast = Toast.makeText(context, "No paired devices found", toastDuration);
    34.             toast.show();
    35.         }
    36.  
    37.         return matchingDevice;
    38.  
    39.     }
    40.  
    41.  
    42.     @Override
    43.     protected void onCreate(Bundle savedInstanceState) {
    44.         super.onCreate(savedInstanceState);
    45.         setContentView(R.layout.activity_main);
    46.  
    47.  
    48.         BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    49.         BluetoothDevice arduinoModule;
    50.         final byte[] testBytes = {6};
    51.  
    52.         if (!mBluetoothAdapter.isEnabled()) { //if bluetooth isn't enabled, enable it
    53.             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    54.             startActivityForResult(enableBtIntent, 1);
    55.         }
    56.  
    57.         arduinoModule = queryDevices(arduinoModuleName, mBluetoothAdapter); //set arduinoModule device to device with name "HC-05"
    58.         ParcelUuid [] uuid = arduinoModule.getUuids();
    59.  
    60.         Log.d("Eren-onCreate", "Found arduino module: " + arduinoModule + "\n");
    61.         Log.d("Eren-onCreate", "Device name: " + arduinoModule.getName());
    62. //        Log.d("Eren-onCreate", "UUID of remote device: " + uuid[0].getUuid());
    63.         Log.d("Eren-onCreate", "UUIDs: " + uuid);
    64.         Log.d("Eren-onCreate", "Bluetooth Class: " + arduinoModule.getBluetoothClass());
    65.         Log.d("Eren-onCreate", "Device bond state: " + arduinoModule.getBondState());
    66.         Log.d("Eren-onCreate", "");
    67.  
    68.         Log.d("Eren-onCreate", "Creating new socket to connect");
    69.         final ConnectThread newThread = new ConnectThread(arduinoModule);
    70.         Log.d("Eren-onCreate", "Connecting Socket");
    71.         newThread.run();
    72.         final ConnectedThread btThread = newThread.getConnection();
    73.  
    74.         Context context = getApplicationContext(); //for Toast
    75.         Toast toast = Toast.makeText(context,"Connected to Bluetooth Module...", toastDuration);
    76.         toast.show();
    77.  
    78.         //Button control to manipulate thread connection
    79.  
    80.         final Button btn = (Button)findViewById(R.id.btn1);
    81.         btn.setOnClickListener(new View.OnClickListener() {
    82.             public void onClick(View v) {
    83.                 btThread.write(testBytes);
    84.             }
    85.         });
    86.  
    87.  
    88.  
    89.     }
    90.  
    91. }
    92.  
     
  3. Deleted User

    Deleted User Guest

    Your question is quite broad to answer. Maybe you could break it down into more specific issues?
    What happened when you tried to build the Github alarm clock project?
     
  4. johnholley123

    johnholley123 Lurker
    Thread Starter

    I have contacted the developer of this app and discovered that this app was not really what I was looking for, which is why it wasn't working for me. I have figured out the issue with my other try to solve the problem and the app is now working.
     
Loading...

Share This Page

Loading...