title: Connect BLE device to Android an Send Data (nUART)
body: <p>I'm trying to connect a BLE device (Adafruit Feather M0 Bluefruit LE) to a Android App. The Feather has a Sensor an sends UART texts on special sensor events.</p>
  
  <p>I want to</p>
  
  <ul>
  <li>connect to the device (static MAC-address)</li>
  <li>connect to the nordic-UART-service</li>
  <li>receive texts from the Feather</li>
  <li>implement surveillance of the connection</li>
  </ul>
  
  <p>Right now I'm trying to understand how BLE works. I understand it as far as to connect to a device but I don't get it after that point.</p>
  
  <ol>
  <li>How do I connect to the service or does it connect to the service automatically?
  
  <ol start="2">
  <li>What do I need to get Data or Notifications of incoming data?</li>
  </ol></li>
  </ol>
  
  <p>This is the code right now. There are now checks, so it will crash when something doesn't happen as planned. But I'm trying to understand the BLE thing. Also it is not a Service, only an Activity. I wasn't able to test it, as the Smartphone delivers no sooner than the end of the week.</p>
  
  <pre><code>package de.example.bluetoothletest;
  
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.bluetooth.BluetoothGatt;
  import android.bluetooth.BluetoothGattCallback;
  import android.bluetooth.BluetoothGattCharacteristic;
  import android.bluetooth.BluetoothManager;
  import android.bluetooth.BluetoothProfile;
  import android.content.Context;
  import android.content.Intent;
  import android.support.v4.content.LocalBroadcastManager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import java.util.UUID;
  
  public class MainActivity extends AppCompatActivity {
  
  private BluetoothAdapter mBluetoothAdapter;
  private BluetoothGatt mBluetoothGatt;
  private String deviceaddress=("FC:57:1A:B7:22:AD");
  public final static String ACTION_DATA_AVAILABLE = "de.example.BluetoothLETest.ACTION_DATA_AVAILABLE";
  public static final UUID RX_SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
  public static final UUID RX_CHAR_UUID = UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
  public static final UUID TX_CHAR_UUID = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
  public static String EXTRA_DATA="de.example.BluetoothLETest.EXTRA_DATA";
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
  
      //get the BluetoothAdapter
      final BluetoothManager mbluetoothManager=(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
      mBluetoothAdapter=mbluetoothManager.getAdapter();
  
  }
  public void connect(){
      //connect to the given deviceaddress
      BluetoothDevice device=mBluetoothAdapter.getRemoteDevice(deviceaddress);
      mBluetoothGatt=device.connectGatt(this, false, mGattCallback);
      //now we get callbacks on mGattCallback
  }
  
  //get callbacks when something changes
  private final BluetoothGattCallback mGattCallback=new BluetoothGattCallback() {
      @Override
      public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          if (newState== BluetoothProfile.STATE_CONNECTED){
              //device connected
          }
      }
  
      @Override
      public void onServicesDiscovered(BluetoothGatt gatt, int status) {
          if (status==BluetoothGatt.GATT_SUCCESS){
              //all Services have been discovered
          }
      }
  
      @Override
      public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
          //we are still connected to the service
          if (status==BluetoothGatt.GATT_SUCCESS){
              //send the characteristic to broadcastupdate
              broadcastupdate(ACTION_DATA_AVAILABLE, characteristic);
          }
      }
  
      @Override
      public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
          //send the characteristic to broadcastupdate
          broadcastupdate(ACTION_DATA_AVAILABLE, characteristic);
      }
  };
  
  //Get the 'real' data out of characteristic
  private void broadcastupdate(final String action,final BluetoothGattCharacteristic characteristic){
      final Intent intent= new Intent(action);
      //only  when it is the right characteristic?/service?
      if (TX_CHAR_UUID.equals(characteristic.getUuid())){
          //get the 'real' data from the stream
          intent.putExtra(EXTRA_DATA, characteristic.getValue());
          //send the extracted data via LocalBroadcastManager
          LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  
      }
  
  }
  }
  </code></pre>
  
  <p>Does this code work an what does the smartphone receive? How do I connect to a specific Service or is it done automatically as the device only offers one service?</p>