title: Using NFC tags to start BLE connection between Android smartphone and Raspberry Pi
body: <p>Basically, I am working on an Android app that will need to establish BLE connection with a Raspberry Pi whenever the user tap an NFC tag on the phone. The purpose of the app is to play video on a TV via Raspberry Pi when user access the video in the app itself or via NFC tags. I plan to store the MAC address of the RPi in one of the NFC tags and when tapped, the app will pass that address into a bluetooth service in the app to start ble connection. Note that I must be able to start BLE connection using this method. Is it possible? </p>
  
  <p>Currently, here is what i got </p>
  
  <p>Bluetooth Service class</p>
  
  <pre><code>public int onStartCommand (Intent intent, int flags, int startId){
      tag = intent.getStringExtra("condition");
      if(tag != null &amp;&amp; tag.equals("start")){
          //start ble connection
          tag2 = intent.getStringExtra("macaddress");
          String ADDRESS = tag2;
          bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
          mBluetoothAdapter = bluetoothManager.getAdapter();
          device = mBluetoothAdapter.getRemoteDevice(ADDRESS);
          Log.d("Capstone", device.getAddress());
  
          bluetoothGatt = device.connectGatt(bluetooth.this, true, new BluetoothGattCallback() {
              @Override
              public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                  super.onConnectionStateChange(gatt, status, newState);
                  Log.d("Capstone", "BLE connected");
                  if (newState == BluetoothProfile.STATE_CONNECTED) {
                      gatt.discoverServices();
                      sendstatus("1");
                  }
              }
              @Override
              public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                  super.onServicesDiscovered(gatt, status);
                  Log.d("Capstone", "Services discovered");
                  charCapstone = gatt.getService(UUID.fromString(CAPSTONE_SERVICE_UUID))
                          .getCharacteristic(UUID.fromString(CAPSTONE_CHARACTERISTICS_UUID));
                  bluetoothGatt = gatt;
  
              }});
      }
      else if(tag != null &amp;&amp; tag.equals("started")){
          tag3 = intent.getStringExtra("command");
          Log.d("Capstone", tag3);
          if(!tag3.equals("0")){
          charCapstone.setValue(new byte[]{(byte)Integer.parseInt(tag3)});
          bluetoothGatt.writeCharacteristic(charCapstone);
          }
      }
      return START_NOT_STICKY;
  }
  </code></pre>
  
  <p>How I read NFC</p>
  
  <pre><code>private void handleIntent(Intent intent) {
          Parcelable[] messages = intent.getParcelableArrayExtra(
                  NfcAdapter.EXTRA_NDEF_MESSAGES);
          for(int i = 0;i &lt; messages.length;i++) {
              NdefMessage message = (NdefMessage) messages[i];
              NdefRecord[] records = message.getRecords();
              NdefRecord record = records[0];
              if(new String(record.getType()).equals("T")) {
                  byte[] original = record.getPayload();
                  byte[] value = Arrays.copyOfRange(original, 3, original.length);
                  String payload = new String(value);
                  if(payload.equals(ble)){
                      NdefRecord record2 = records[1];
                      if(new String(record2.getType()).equals("T")) {
                          byte[] original2 = record2.getPayload();
                          byte[] value2 = Arrays.copyOfRange(original2, 3, original2.length);
                          String payload2 = new String(value2);
                          startBluetooth(payload2);
                      }
                  }
                  else if(a.equals(def)){
                      activityfilter(payload);}
                  else {
                      bluetooth(payload);
                  }
              }
          }
      }
  </code></pre>
  
  <p>And the two bluetooth method i used to pass NFC data to the bluetooth service class.</p>
  
  <pre><code>public void startBluetooth(String c){
          Intent serviceIntent = new Intent(this, bluetooth.class);
          serviceIntent.putExtra("condition", "start");
          serviceIntent.putExtra("macaddress", c);
          this.startService(serviceIntent);
  
   public void bluetooth(String b)
      {
          String s1="1";
          String s2="2";
  
          Intent serviceIntent1 = new Intent(this, bluetooth.class);
          serviceIntent1.putExtra("condition", "started");
          serviceIntent1.putExtra("command", b);
          Log.d("Capstone", "bluetooth: " + b);
          this.startService(serviceIntent1);
  
          if(b.equals(s1)){
              Intent videocontrol1 = new Intent(ModeChoose.this, videocontroller.class);
              startActivity(videocontrol1);}
  
          if(b.equals(s2)){
              Intent videocontrol2 = new Intent(ModeChoose.this, videocontroller.class);
              startActivity(videocontrol2);}
      }
  
  
  
      private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              a = intent.getStringExtra("status");
              String b="1";
              if(a.equals(b))
              {
                  Toast.makeText(ModeChoose.this,
                          "Bluetooth1 Connected", Toast.LENGTH_LONG).show();
              }
  
          }
      };
  </code></pre>
  
  <p>I am able to read NFC tags. However I cant seem to establish BLE connection with the Raspberry Pi. I am a total novice in Android. Any help would be appreciated.</p>