2014-04-23 2 views
0

내 팀과 나는 현재 우리의 안드로이드 애플 리케이션을 우리의 arduino에 블루투스 껍질과 신호를 보내려고 노력하고 있습니다. 신호는 arduino가 신호를 보낸 것을 알기 만하면 의미가있을 필요는 없습니다. 나는 이것에 온라인 물자의 할당을 보았다, 그러나 그것의 아무도는 우연히 보지 않으며 그것의 아무도는 저를 위해 작동하지 않는 것처럼 보인다.안드로이드 신호를 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()"가 "00000000-0000-0000-0000-000000000000"형식이 아닌 14 자리 숫자를 반환한다는 것입니다. 모든 제안, 꾸중 또는 조언이 가장 환영받을 것입니다.

+0

"쉴드"가 아니라 "쉘" –

답변

0

이 :

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

은 당신이 원하는 무엇을 가장 확실하지 않습니다.

휴대 전화의 "기기 ID"(?)가 다른 기기의 특정 블루투스 서비스를 식별하는 데 사용되는 UUID와 어떤 관련이 있다고 생각합니까?

Btw, the docs을 읽었습니까?

대상 장치가 BT 인터페이스에서 제공하는 특정 서비스에 연결하는 데 사용해야하는 UUID를 확실히 알아야합니다. 잘 알려진 표준 UUID 목록은 예를 들어 here입니다.

많은 장치는 기본 스트림 지향 데이터 교환을 위해 "직렬 포트 프로필"(SPP)을 제공합니다. 먼저 시도해보십시오.

Here's 도움이 될만한 다른 출처입니다.

관련 문제