0

My team and I are currently trying to get our android app to send a signal to our arduino with a bluetooth shell on it. The signal doesn't need to be meaningful in anyway only that the arduino knows a signal has been sent. I have seen allot of online material on this, but none of it seems to coincide and none of it seems to work for me.

My current code: (we only want to send a signal when onRecieve() is called)

package com.example.alarmquiz2;

import android.provider.Settings.Secure;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.UUID;
import android.telephony.TelephonyManager;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.bluetooth.BluetoothClass.Device;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.widget.Toast;
import android.content.Intent;
import android.content.BroadcastReceiver;

public class AlarmReceiver
    extends BroadcastReceiver
{
    Sound                    s        = new Sound();
    private BluetoothAdapter blue;
    private Context          contexxt;
    private Device           arduino;
    private BluetoothSocket  btSocket;
    private TelephonyManager tManager;
    private UUID             uuid;
    private OutputStream     outStream;
    private InputStream      inStream;
    private static String    address  = "00:14:03:18:42:19";


    public void onReceive(Context context, Intent intent)
     {
         TelephonyManager tManager =
            (TelephonyManager)context
                 .getSystemService(Context.TELEPHONY_SERVICE);
         uuid = UUID.fromString(tmanager.getDeviceID()); 
         contexxt = context;
        this.CheckBt();
        this.Connect();
         this.writeData("meh");
        if (!s.isPlaying())
        {
            s.setSound(context);
            s.startSound();

            Intent i = new Intent(context, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
        else if (s.isPlaying())
        {

        s.stopSound();
        Intent i = new Intent(context, SecondscreenActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
        }
     }


     private void CheckBt()
     {
        blue = BluetoothAdapter.getDefaultAdapter();

         if (!blue.isEnabled())
         {
             Toast
               .makeText(contexxt, "Bluetooth Disabled !", Toast.LENGTH_SHORT)
                .show();
        /*
         * It tests if the bluetooth is enabled or not, if not the app will
         * show a message.
         */
        }

        if (blue == null)
        {
             Toast.makeText(contexxt, "Bluetooth null !", Toast.LENGTH_SHORT)
                 .show();
        }
    }


     public void Connect()
     {
         BluetoothDevice device = blue.getRemoteDevice(address);
         Log.d("", "Connecting to ... " + device);
         blue.cancelDiscovery();

        try
        {
             btSocket = device.createRfcommSocketToServiceRecord(uuid);
/*
 * Here is the part the connection is made, by asking the device to create a
 * RfcommSocket (Unsecure socket I guess), It map a port for us or something
 * like that
 */


       btSocket.connect();
            Log.d("", "Connection made.");
        }
        catch (IOException e)
        {
            try
            {
                btSocket.close();
            }
            catch (IOException e2)
            {
                Log.d("", "Unable to end the connection");
            }
            Log.d("", "Socket creation failed");
        }

        /*
         * this is a method used to read what the Arduino says for example when
         * you write Serial.print("Hello world.") in your Arduino code
         */
    }


    private void writeData(String data)
    {
        try
        {
            outStream = btSocket.getOutputStream();
        }
        catch (IOException e)
        {
            Log.d("", "Bug BEFORE Sending stuff", e);
        }

        String message = data;
/* In my example, I put a button that invoke this method and send a string to it */
        byte[] msgBuffer = message.getBytes();

        try
        {
            outStream.write(msgBuffer);
        }
        catch (IOException e)
        {
            Log.d("", "Bug while sending stuff", e);
        }
    }

}

Ive also give myself all the required permissions in my manifest. The problem I am presently getting on my friends phone is that the "getDeviceID()" is returning a 14 digit number as opposed to the "00000000-0000-0000-0000-000000000000" format. Any suggestions, scoldings, or advice would be most welcome.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

This:

uuid = UUID.fromString(tmanager.getDeviceID()); 
...
btSocket = device.createRfcommSocketToServiceRecord(uuid);

is most certainly not what you want to do.

What makes you think that the "device ID" of the phone(?) is in some way related to the UUID which is used to identify a certain bluetooth service at the other device?

Btw, did you read the docs?

You definitely need to find out which UUID you have to use to connect to the specific service the destination device provides on its BT interface. A list of well-known, standard UUIDs can be found here for example.

Many devices provide the "serial port profile" (SPP) for basic, stream-oriented data exchange. You may want to try that first.

Here's another source which may help.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44