MainActivity.java

package com.dnoved1.www.smartkeys;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.util.UUID;
public class MainActivity extends ActionBarActivity {
    // UUIDs for UAT service and associated characteristics.
    public static UUID UART_UUID = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
    public static UUID TX_UUID = UUID.fromString("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
    public static UUID RX_UUID = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
    // UUID for the BTLE client characteristic which is necessary for notifications.
    public static UUID CLIENT_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    BluetoothGattCharacteristic tx, rx;
    BluetoothGatt btGatt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            Log.e("Piano Bluetooth", "Is not supported.");
        }
        if (!btAdapter.isEnabled()) {
            Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity/*ForResult*/(enableBt/*, REQUEST_ENABLE_BT*/);
            // TODO: should have onActivityResult to check the return code;
        }
        Log.i("BT Adapter", btAdapter.toString());
        btAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
                btAdapter.stopLeScan(this);
                Log.i("BT Devices", device.toString());
                btGatt = device.connectGatt(MainActivity.this, true, new BluetoothGattCallback() {
                    @Override
                    public void onConnectionStateChange(BluetoothGatt btGatt, int status, int newState) {
                        if (newState == BluetoothGatt.STATE_CONNECTED) {
                            Log.i("BT", "Connected");
                            if (!btGatt.discoverServices()) {
                                Log.e("BT", "Failed to discover services");
                            }
                        } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
                            Log.i("BT", "Disconnected");
                        }
                    }
                    @Override
                    public void onServicesDiscovered(BluetoothGatt btGatt, int status) {
                        Log.i("BT Services", btGatt.getServices().toString());
                        // Save reference to each characteristic.
                        tx = btGatt.getService(UART_UUID).getCharacteristic(TX_UUID);
                        rx = btGatt.getService(UART_UUID).getCharacteristic(RX_UUID);
                        // Setup notifications on RX characteristic changes (i.e. data received).
                        // First call setCharacteristicNotification to enable notification.
                        if (!btGatt.setCharacteristicNotification(rx, true)) {
                            Log.e("BT", "Couldn't set notifications for RX characteristic!");
                        }
                        // Next update the RX characteristic's client descriptor to enable notifications.
                        if (rx.getDescriptor(CLIENT_UUID) != null) {
                            BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID);
                            desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            if (!btGatt.writeDescriptor(desc)) {
                                Log.e("BT", "Couldn't write RX client descriptor value!");
                            }
                        }
                        else {
                            Log.e("BT", "Couldn't get RX client descriptor!");
                        }
                    }
                    @Override
                    public void onCharacteristicChanged(BluetoothGatt btGatt, BluetoothGattCharacteristic btGattChar) {
                        Log.i("BT Received", btGattChar.getStringValue(0));
                    }
                    @Override
                    public void onCharacteristicWrite(BluetoothGatt btGatt, BluetoothGattCharacteristic btGattChar, int status) {
                        if (status == BluetoothGatt.GATT_SUCCESS) {
                            Log.i("BT", "Successfully sent " + btGattChar.toString());
                        } else {
                            Log.e("BT", "Failed to send " + btGattChar.toString());
                        }
                    }
                });
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}