Skip to content

Commit 4e17365

Browse files
committed
We can now list supported bluetooth services
1 parent eae8e9c commit 4e17365

5 files changed

Lines changed: 83 additions & 0 deletions

File tree

library/src/main/java/uk/co/alt236/bluetoothlelib/device/BluetoothLeDevice.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
import java.io.Serializable;
99
import java.util.Arrays;
10+
import java.util.Collections;
11+
import java.util.HashSet;
1012
import java.util.Map;
13+
import java.util.Set;
1114

1215
import uk.co.alt236.bluetoothlelib.device.adrecord.AdRecordStore;
1316
import uk.co.alt236.bluetoothlelib.resolvers.BluetoothClassResolver;
@@ -55,6 +58,7 @@ public BluetoothLeDevice[] newArray(final int size) {
5558
private final long mFirstTimestamp;
5659
private int mCurrentRssi;
5760
private long mCurrentTimestamp;
61+
private transient Set<BluetoothService> mServiceSet;
5862

5963
/**
6064
* Instantiates a new Bluetooth LE device.
@@ -221,6 +225,25 @@ public String getBluetoothDeviceMajorClassName() {
221225
return BluetoothClassResolver.resolveMajorDeviceClass(mDevice.getBluetoothClass().getMajorDeviceClass());
222226
}
223227

228+
public Set<BluetoothService> getBluetoothDeviceKnownSupportedServices() {
229+
if (mServiceSet == null) {
230+
synchronized (this) {
231+
if (mServiceSet == null) {
232+
final Set<BluetoothService> serviceSet = new HashSet<>();
233+
for (final BluetoothService service : BluetoothService.values()) {
234+
235+
if (mDevice.getBluetoothClass().hasService(service.getAndroidConstant())) {
236+
serviceSet.add(service);
237+
}
238+
}
239+
mServiceSet = Collections.unmodifiableSet(serviceSet);
240+
}
241+
}
242+
}
243+
244+
return mServiceSet;
245+
}
246+
224247
/**
225248
* Gets the device.
226249
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package uk.co.alt236.bluetoothlelib.device;
2+
3+
import android.bluetooth.BluetoothClass;
4+
5+
/**
6+
*
7+
*/
8+
public enum BluetoothService {
9+
AUDIO(BluetoothClass.Service.AUDIO),
10+
CAPTURE(BluetoothClass.Service.CAPTURE),
11+
INFORMATION(BluetoothClass.Service.INFORMATION),
12+
LIMITED_DISCOVERABILITY(BluetoothClass.Service.LIMITED_DISCOVERABILITY),
13+
NETWORKING(BluetoothClass.Service.NETWORKING),
14+
OBJECT_TRANSFER(BluetoothClass.Service.OBJECT_TRANSFER),
15+
POSITIONING(BluetoothClass.Service.POSITIONING),
16+
RENDER(BluetoothClass.Service.RENDER),
17+
TELEPHONY(BluetoothClass.Service.TELEPHONY);
18+
19+
private final int mAndroidConstant;
20+
21+
BluetoothService(final int androidCode){
22+
mAndroidConstant = androidCode;
23+
}
24+
25+
public int getAndroidConstant(){
26+
return mAndroidConstant;
27+
}
28+
}

sample_app/src/main/java/uk/co/alt236/btlescan/activities/DeviceDetailsActivity.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import butterknife.Bind;
2020
import butterknife.ButterKnife;
2121
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
22+
import uk.co.alt236.bluetoothlelib.device.BluetoothService;
2223
import uk.co.alt236.bluetoothlelib.device.adrecord.AdRecord;
2324
import uk.co.alt236.bluetoothlelib.device.mfdata.IBeaconManufacturerData;
2425
import uk.co.alt236.bluetoothlelib.resolvers.CompanyIdentifierResolver;
@@ -56,6 +57,7 @@ private void appendDeviceInfo(final MergeAdapter adapter, final BluetoothLeDevic
5657
final TextView tvAddress = (TextView) lt.findViewById(R.id.deviceAddress);
5758
final TextView tvClass = (TextView) lt.findViewById(R.id.deviceClass);
5859
final TextView tvMajorClass = (TextView) lt.findViewById(R.id.deviceMajorClass);
60+
final TextView tvServices = (TextView) lt.findViewById(R.id.deviceServiceList);
5961
final TextView tvBondingState = (TextView) lt.findViewById(R.id.deviceBondingState);
6062

6163
tvName.setText(device.getName());
@@ -64,6 +66,24 @@ private void appendDeviceInfo(final MergeAdapter adapter, final BluetoothLeDevic
6466
tvMajorClass.setText(device.getBluetoothDeviceMajorClassName());
6567
tvBondingState.setText(device.getBluetoothDeviceBondState());
6668

69+
final String supportedServices;
70+
if(device.getBluetoothDeviceKnownSupportedServices().isEmpty()){
71+
supportedServices = getString(R.string.no_known_services);
72+
} else {
73+
final StringBuilder sb = new StringBuilder();
74+
75+
for(final BluetoothService service : device.getBluetoothDeviceKnownSupportedServices()){
76+
if(sb.length() > 0){
77+
sb.append(", ");
78+
}
79+
80+
sb.append(service);
81+
}
82+
supportedServices = sb.toString();
83+
}
84+
85+
tvServices.setText(supportedServices);
86+
6787
adapter.addView(lt);
6888
}
6989

sample_app/src/main/res/layout/list_item_view_device_info.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
android:id="@+id/deviceMajorClass"
5353
style="@style/GridLayoutDataTextView"/>
5454

55+
<TextView
56+
style="@style/GridLayoutTitleTextView"
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:text="@string/label_device_services"/>
60+
61+
<TextView
62+
android:id="@+id/deviceServiceList"
63+
style="@style/GridLayoutDataTextView"/>
64+
5565
<TextView
5666
style="@style/GridLayoutTitleTextView"
5767
android:layout_width="wrap_content"

sample_app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<string name="unknown_characteristic">Unknown characteristic</string>
1616
<string name="unknown_device">Unknown device</string>
1717
<string name="unknown_service">Unknown service</string>
18+
<string name="no_known_services">No known services</string>
1819

1920
<!-- Formatters -->
2021
<string name="formatter_meters">%sm</string>
@@ -54,6 +55,7 @@
5455
<string name="label_device_address">Device address:</string>
5556
<string name="label_device_class">Device Class:</string>
5657
<string name="label_device_major_class">Major Class:</string>
58+
<string name="label_device_services">Services:</string>
5759
<string name="label_device_name">Device Name:</string>
5860
<string name="label_distance">Distance:</string>
5961
<string name="label_first_rssi">First RSSI:</string>

0 commit comments

Comments
 (0)