2014-04-23 94 views
0

我和我的团队正在尝试让我们的android应用程序向我们的arduino发送一个信号,并带有蓝牙外壳。无论如何,信号并不需要有意义,只要arduino知道已经发送了一个信号。我已经看到了许多关于这方面的在线资料,但似乎没有一个似乎是一致的,它似乎并没有为我工作。Android信号到Arduino蓝牙外壳

我当前的代码:(我们只需要发送一个信号时,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); 
     } 
    } 

} 

香港专业教育学院也给我自己都在我的清单中所需的权限。我现在正在接触我的朋友电话的问题是,“getDeviceID()”返回一个14位数字而不是“00000000-0000-0000-0000-000000000000”格式。任何建议,训斥或建议将是最受欢迎的。

+0

“盾”不是“壳” –

回答

0

此:

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

肯定不是你想要做什么。

是什么让你认为手机(?)的“设备ID”在某种程度上与UUID相关,UUID用于识别其他设备上的某个蓝牙服务?

顺便说一句,你看过the docs吗?

您一定需要知道您必须使用哪个UUID连接到目标设备在其BT接口上提供的特定服务。例如,可以找到众所周知的标准UUID列表here

许多设备为基本的面向流的数据交换提供“串行端口配置文件”(SPP)。你可能想先尝试一下。

Here's另一个可能有用的来源。