2017-02-12 2 views
0

Ich habe versucht, eine Bluetooth-Verbindung zwischen einem Android Smartphone und einem Mindstorms NXT herzustellen. Das Smartphone ist ein Sony Xperia Zl mit Android 5.1.1. Die App sollte dem NXT eine Nummer schicken und das NXT sollte es mit einem laufenden Programm erhalten. Dies ist mein aktueller Source Code:Bluetooth IOException: Bluetooth ist aus

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(); 


    } 
     } 
    } 

} 

Bluetooth erfolgreich aktiviert ist, aber das Errorhandler wirft ein IO.Exception: Bluetooth ausgeschaltet ist. Die MAC-Adresse ist nachgewiesen. Die Bluetooth Verbindung kann mit einer kommerziellen App erfolgreich hergestellt werden.

Irgendwelche Ideen?

Antwort

0

Haben Sie in Manifest eine Bluetooth-Berechtigung hinzugefügt?

+0

Ja, ich habe sowohl die Berechtigungen als auch den Admin hinzugefügt. – Mnchen875