1. Download our Official Android App: Forums for Android!

Apps BLE Application (Display data from characteristic in a textView)

Discussion in 'Android Development' started by djboi2011, Apr 12, 2016.

  1. djboi2011

    djboi2011 Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    43
    Posts:
    194
    Joined:
    Sep 14, 2011

    Sep 14, 2011
    194
    35
    43
    college student -_-
    I used the BluetoothLeGatt example code to write an app that automatically connects to a bonded BLE peripheral upon launching the app. Now i am trying to display the data from one of the peripheral's characteristic in a textView. The BluetoothLeGatt example code only demonstrates this using ExpandableListView.OnChildClickListener, my app should require no user input and simply get he data from the characteristic. This is what i have so far:

    Code (Text):
    1.  private TextView mConnectionState;
    2. private TextView mDataField;
    3. private String mDeviceName;
    4. private String mDeviceAddress;
    5. private ExpandableListView mGattServicesList;
    6. private BluetoothLeService mBluetoothLeService;
    7. private boolean mConnected = false;
    8. private BluetoothGattCharacteristic mNotifyCharacteristic;
    9.  
    10. private final String LIST_NAME = "NAME";
    11. private final String LIST_UUID = "UUID";
    12.  
    13. // Code to manage Service lifecycle.
    14. private final ServiceConnection mServiceConnection = new ServiceConnection() {
    15.  
    16.     @Override
    17.     public void onServiceConnected(ComponentName componentName, IBinder service) {
    18.         mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
    19.         if (!mBluetoothLeService.initialize()) {
    20.             Log.e(TAG, "Unable to initialize Bluetooth");
    21.             finish();
    22.         }
    23.         // Automatically connects to the device upon successful start-up initialization.
    24.         mBluetoothLeService.connect(mDeviceAddress);
    25.     }
    26.  
    27.     @Override
    28.     public void onServiceDisconnected(ComponentName componentName) {
    29.         mBluetoothLeService = null;
    30.     }
    31. };
    32.  
    33. // Handles various events fired by the Service.
    34. // ACTION_GATT_CONNECTED: connected to a GATT server.
    35. // ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
    36. // ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
    37. // ACTION_DATA_AVAILABLE: received data from the device.  This can be a result of read
    38. //                        or notification operations.
    39. private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    40.     @Override
    41.     public void onReceive(Context context, Intent intent) {
    42.         final String action = intent.getAction();
    43.         if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
    44.             mConnected = true;
    45.             updateConnectionState(R.string.connected);
    46.             mConnectionState.setTextColor(Color.parseColor("#FF17AA00"));
    47.             invalidateOptionsMenu();
    48.         } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
    49.             mConnected = false;
    50.             updateConnectionState(R.string.disconnected);
    51.             invalidateOptionsMenu();
    52.             clearUI();
    53.         } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
    54. *edit*
    55.            UUID chara = UUID.fromString("c97433f0-be8f-4dc8-b6f0-5343e6100eb4");
    56.             List<BluetoothGattService> servs = mBluetoothLeService.getSupportedGattServices();
    57.                 for (int i = 0; servs.size() > i; i++) {
    58.  
    59.                     List<BluetoothGattCharacteristic> charac = servs.get(i).getCharacteristics();
    60.                     for (int j = 0; charac.size() > i; i++) {
    61.                          BluetoothGattCharacteristic ch = charac.get(i);
    62.                         if (ch.getUuid() == chara) {
    63.                             mBluetoothLeService.readCharacteristic(ch);
    64.                             mBluetoothLeService.setCharacteristicNotification(ch, true);
    65.  
    66.                         }
    67.                     }
    68.                 }
    69.         } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
    70.             displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
    71.         }
    72.     }
    73. };
    74.  
    75. @Override
    76. protected void onCreate(Bundle savedInstanceState) {
    77.     super.onCreate(savedInstanceState);
    78.     setContentView(R.layout.activity_device_control);
    79.  
    80.     final Intent intent = getIntent();
    81.     mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
    82.     mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
    83.     mConnectionState = (TextView) findViewById(R.id.connection_state);
    84.     mDataField = (TextView) findViewById(R.id.data);
    85.  
    86.     Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
    87.     bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    88.     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95. }
    96. @Override
    97. protected void onResume() {
    98.     super.onResume();
    99.     registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
    100.     if (mBluetoothLeService != null) {
    101.         final boolean result = mBluetoothLeService.connect(mDeviceAddress);
    102.         Log.d(TAG, "Connect request result=" + result);
    103.     }
    104.  
    105. }
    106.  
    107. @Override
    108. protected void onPause() {
    109.     super.onPause();
    110.     unregisterReceiver(mGattUpdateReceiver);
    111. }
    112.  
    113. @Override
    114. protected void onDestroy() {
    115.     super.onDestroy();
    116.     unbindService(mServiceConnection);
    117.     mBluetoothLeService = null;
    118. }
    119.  
    120. private void updateConnectionState(final int resourceId) {
    121.     runOnUiThread(new Runnable() {
    122.         @Override
    123.         public void run() {
    124.             mConnectionState.setText(resourceId);
    125.         }
    126.     });
    127. }
    128.  
    129. private void displayData(String data) {
    130.     if (data != null) {
    131.         mDataField.setText(data);
    132.     }
    133. }
    134.  
    135. private void clearUI() {
    136.     mGattServicesList.setAdapter((SimpleExpandableListAdapter) null);
    137.     mDataField.setText(R.string.no_data);
    138. }
    139.  
    140.  
    141.  
    142.  
    143. private static IntentFilter makeGattUpdateIntentFilter() {
    144.     final IntentFilter intentFilter = new IntentFilter();
    145.     intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
    146.     intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
    147.     intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
    148.     intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
    149.     return intentFilter;
    150. }
    151.  
    I've successfully connected to an already bonded device, but now im trying to get the data from a characteristic using its uuid and display it in a textView. The BluetoothLeGatt example shows how a characteristic is selected by a user using an expandable list view onclick listener displaying the supported characteristics. I want to bypass all that and just get the data from the characteristic with the known uuid.

    -EDIT-
    figured it out
     

    Advertisement

    #1 djboi2011, Apr 12, 2016
    Last edited: Apr 12, 2016
  2. merc07

    merc07 Lurker
    Rank:
    None
    Points:
    15
    Posts:
    1
    Joined:
    Dec 26, 2017

    Dec 26, 2017
    1
    0
    15
    Did you ever finish this code, I am looking to write something very similar.
    I have the BluetoothLEGatt example working with my data but would like to have it automated to the bonded device....
     

Share This Page

Loading...