2121import java .util .List ;
2222import java .util .Map ;
2323
24+ import uk .co .alt236 .bluetoothlelib .device .BluetoothLeDevice ;
2425import uk .co .alt236 .bluetoothlelib .resolvers .GattAttributeResolver ;
2526import uk .co .alt236 .bluetoothlelib .util .ByteUtils ;
2627import uk .co .alt236 .btlescan .R ;
5455 */
5556public class DeviceControlActivity extends Activity {
5657 private final static String TAG = DeviceControlActivity .class .getSimpleName ();
57-
58- public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME" ;
59- public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS" ;
58+ public static final String EXTRA_DEVICE = "extra_device" ;
6059
6160 private static final String LIST_NAME = "NAME" ;
6261 private static final String LIST_UUID = "UUID" ;
@@ -76,6 +75,8 @@ public class DeviceControlActivity extends Activity {
7675 @ InjectView (R .id .data_as_array ) TextView mDataAsArray ;
7776
7877 private boolean mConnected = false ;
78+ private String mExportString ;
79+ private BluetoothLeDevice mDevice ;
7980
8081 // Code to manage Service lifecycle.
8182 private final ServiceConnection mServiceConnection = new ServiceConnection () {
@@ -160,21 +161,70 @@ public boolean onChildClick(ExpandableListView parent, View v, int groupPosition
160161 }
161162 };
162163
163-
164164 private void clearUI () {
165165 mGattServicesList .setAdapter ((SimpleExpandableListAdapter ) null );
166166 mGattUUID .setText (R .string .no_data );
167167 mGattUUIDDesc .setText (R .string .no_data );
168168 mDataAsArray .setText (R .string .no_data );
169169 mDataAsString .setText (R .string .no_data );
170+ }
171+
172+ private void generateExportString (List <BluetoothGattService > gattServices ){
173+ final String unknownServiceString = getResources ().getString (R .string .unknown_service );
174+ final String unknownCharaString = getResources ().getString (R .string .unknown_characteristic );
175+ final StringBuilder exportBuilder = new StringBuilder ();
176+
177+ exportBuilder .append ("Device Name: " );
178+ exportBuilder .append (mDeviceName );
179+ exportBuilder .append ('\n' );
180+ exportBuilder .append ("Device Address: " );
181+ exportBuilder .append (mDeviceAddress );
182+ exportBuilder .append ('\n' );
183+ exportBuilder .append ('\n' );
184+
185+ exportBuilder .append ("Services:" );
186+ exportBuilder .append ("--------------------------" );
187+ exportBuilder .append ('\n' );
188+
189+ String uuid = null ;
190+ for (BluetoothGattService gattService : gattServices ) {
191+ uuid = gattService .getUuid ().toString ();
192+
193+ exportBuilder .append (GattAttributeResolver .getAttributeName (uuid , unknownServiceString ));
194+ exportBuilder .append (" (" );
195+ exportBuilder .append (uuid );
196+ exportBuilder .append (')' );
197+ exportBuilder .append ('\n' );
198+
199+ final List <BluetoothGattCharacteristic > gattCharacteristics = gattService .getCharacteristics ();
200+ for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics ) {
201+ uuid = gattCharacteristic .getUuid ().toString ();
170202
203+ exportBuilder .append ('\t' );
204+ exportBuilder .append (GattAttributeResolver .getAttributeName (uuid , unknownCharaString ));
205+ exportBuilder .append (" (" );
206+ exportBuilder .append (uuid );
207+ exportBuilder .append (')' );
208+ exportBuilder .append ('\n' );
209+ }
210+
211+ exportBuilder .append ('\n' );
212+ exportBuilder .append ('\n' );
213+ }
214+
215+ exportBuilder .append ("--------------------------" );
216+ exportBuilder .append ('\n' );
217+
218+ mExportString = exportBuilder .toString ();
171219 }
172220
173221 // Demonstrates how to iterate through the supported GATT Services/Characteristics.
174222 // In this sample, we populate the data structure that is bound to the ExpandableListView
175223 // on the UI.
176224 private void displayGattServices (List <BluetoothGattService > gattServices ) {
177225 if (gattServices == null ) return ;
226+ generateExportString (gattServices );
227+
178228 String uuid = null ;
179229 final String unknownServiceString = getResources ().getString (R .string .unknown_service );
180230 final String unknownCharaString = getResources ().getString (R .string .unknown_characteristic );
@@ -203,6 +253,7 @@ private void displayGattServices(List<BluetoothGattService> gattServices) {
203253 currentCharaData .put (LIST_UUID , uuid );
204254 gattCharacteristicGroupData .add (currentCharaData );
205255 }
256+
206257 mGattCharacteristics .add (charas );
207258 gattCharacteristicData .add (gattCharacteristicGroupData );
208259 }
@@ -218,7 +269,9 @@ private void displayGattServices(List<BluetoothGattService> gattServices) {
218269 new String [] {LIST_NAME , LIST_UUID },
219270 new int [] { android .R .id .text1 , android .R .id .text2 }
220271 );
272+
221273 mGattServicesList .setAdapter (gattServiceAdapter );
274+ invalidateOptionsMenu ();
222275 }
223276
224277 @ Override
@@ -227,8 +280,9 @@ public void onCreate(Bundle savedInstanceState) {
227280 setContentView (R .layout .activity_gatt_services );
228281
229282 final Intent intent = getIntent ();
230- mDeviceName = intent .getStringExtra (EXTRAS_DEVICE_NAME );
231- mDeviceAddress = intent .getStringExtra (EXTRAS_DEVICE_ADDRESS );
283+ mDevice = intent .getParcelableExtra (EXTRA_DEVICE );
284+ mDeviceName = mDevice .getName ();
285+ mDeviceAddress = mDevice .getAddress ();
232286
233287 ButterKnife .inject (this );
234288
@@ -253,6 +307,13 @@ public boolean onCreateOptionsMenu(Menu menu) {
253307 menu .findItem (R .id .menu_connect ).setVisible (true );
254308 menu .findItem (R .id .menu_disconnect ).setVisible (false );
255309 }
310+
311+ if (mExportString == null ){
312+ menu .findItem (R .id .menu_share ).setVisible (false );
313+ } else {
314+ menu .findItem (R .id .menu_share ).setVisible (true );
315+ }
316+
256317 return true ;
257318 }
258319
@@ -275,6 +336,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
275336 case android .R .id .home :
276337 onBackPressed ();
277338 return true ;
339+ case R .id .menu_share :
340+ final Intent intent = new Intent (android .content .Intent .ACTION_SEND );
341+ final String subject = getString (R .string .exporter_email_device_services_subject , mDeviceName , mDeviceAddress );
342+
343+ intent .setType ("text/plain" );
344+ intent .putExtra (android .content .Intent .EXTRA_SUBJECT , subject );
345+ intent .putExtra (android .content .Intent .EXTRA_TEXT , mExportString );
346+
347+ startActivity (Intent .createChooser (
348+ intent ,
349+ getString (R .string .exporter_email_device_list_picker_text )));
350+
351+ return true ;
278352 }
279353 return super .onOptionsItemSelected (item );
280354 }
0 commit comments