2017-02-12 7 views
0

Android 스마트 폰과 Mindstorms NXT간에 블루투스 연결을 시도했습니다. 스마트 폰은 안드로이드 5.1.1과 소니 Xperia Zl입니다. 앱은 NXT에 번호를 보내야하며 NXT는 실행중인 프로그램으로 번호를 받아야합니다. 이 내 현재의 소스 코드입니다 :블루투스 IOException : 블루투스가 꺼져 있음

package com.mona.projektkurs; 

import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.util.UUID; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 


public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    enableBt(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 



private BluetoothAdapter adapter; 
    private BluetoothDevice device = null; 
    private BluetoothSocket BTsocket=null; 

public void enableBt(){ 
    adapter = BluetoothAdapter.getDefaultAdapter(); 
    if(!adapter.isEnabled()){ 
     adapter.enable(); 
     connection(); 
    }else{ 
     Toast.makeText(getApplicationContext(),"Error1" , Toast.LENGTH_LONG).show(); 
    } 
} 

public void connection(){ 
    device = adapter.getRemoteDevice("00:16:53:1B:1D:C5"); 
    try{ 
     BTsocket = device.createRfcommSocketToServiceRecord(UUID 
        .fromString("00001101-0000-1000-8000-00805F9B34FB")); 
     BTsocket.connect(); 
     messageWrite(); 
    }catch(IOException e){ 
     Toast.makeText(getApplicationContext(), "Error2"+e.toString(), Toast.LENGTH_LONG).show(); 
    } 
} 

public void messageWrite(){ 
    if(BTsocket != null){ 
     try{ 
      OutputStreamWriter out = new OutputStreamWriter(BTsocket.getOutputStream()); 
      out.write((byte)0x80); // no respond 
      out.write((byte)0x09); // MessageWrite 
      out.write((byte)0x03); // Inbox (0-9) 
      out.write((byte)0x02); // MessageSize 
      out.write((byte)0x49); // Data ("1" == 0x49 ASCII Code) 
      out.write((byte)0x00); // Null termination (/0) 
      BTsocket.close(); 
     }catch(IOException f){ 
      Toast.makeText(getApplicationContext(), "Error3"+f.toString(), Toast.LENGTH_LONG).show(); 


    } 
     } 
    } 

} 

블루투스가 성공적으로 활성화되어 있지만, ErrorHandler로는 IO.Exception 발생 : 블루투스가 꺼져 있습니다. MAC 주소가 입증되었습니다. 블루투스 연결은 상용 응용 프로그램으로 성공적으로 설정할 수 있습니다.

어떤 아이디어?

답변

0

매니페스트에 블루투스 권한을 추가 했습니까?

+0

예, 일반 및 관리자 권한을 모두 추가했습니다. – Mnchen875