简体   繁体   English

Android信号到Arduino蓝牙外壳

[英]Android signal to Arduino bluetooth shell

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. 我和我的团队目前正在尝试获取我们的android应用,以将信号发送到带有蓝牙外壳的arduino。 The signal doesn't need to be meaningful in anyway only that the arduino knows a signal has been sent. 无论如何,只要arduino知道已发送信号,该信号就不需要有意义。 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) 我当前的代码:(我们只想在调用onRecieve()时发送信号)

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. Ive还会在清单中给自己所有必需的权限。 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. 我目前在朋友手机上遇到的问题是,“ getDeviceID()”返回的是14位数字,而不是“ 00000000-0000-0000-0000-000000000000”格式。 Any suggestions, scoldings, or advice would be most welcome. 任何建议,责骂或建议都将受到欢迎。

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? 是什么让您认为电话(?)的“设备ID”在某种程度上与用于识别另一台设备上某个蓝牙服务的UUID有关?

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. 您绝对需要找出必须使用哪个UUID才能连接到目标设备在其BT接口上提供的特定服务。 A list of well-known, standard UUIDs can be found here for example. 例如,可以在此处找到众所周知的标准UUID列表。

Many devices provide the "serial port profile" (SPP) for basic, stream-oriented data exchange. 许多设备为基本的,面向流的数据交换提供“串行端口配置文件”(SPP)。 You may want to try that first. 您可能想先尝试一下。

Here's another source which may help. 这是另一个可能有用的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM