Android BLE 开发心得 UUID获取。



我再网上看了很多关于BLE开发的技术博客已经文章,基本都是翻译Goole的API文档,基本没有解决我的问题,经过我自己的研究我基本解决了手头上的问题。现在我把它分享给大家。

问题1:UUID的获取。

看过goole文档的朋友应该对SampleGattAttributes这个类不陌生,因为文档中的UUID就是从这个类中获取到的,但是这个类又在哪里呢?

经过我的研究 我完善了SampleGattAttributes这个类。代码如下:

public class SampleGattAttributes {
private static HashMap attributes = new HashMap();
public static String HEART_RATE_MEASUREMENT = "00002902-0000-1000-8000-00805f9b34fb";
public static String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
public static String MYCJ_BLE = "服务UUID";
public static String MYCJ_BLE_READ = "读属性UUID";
public static String MYCJ_BLE_WRITE = "写属性UUID";
static {
attributes.put("0000fff00000-1000-8000-00805f9b34fb", "Heart Rate Service");
attributes.put("0000180a-0000-1000-8000-00805f9b34fb", "Device Information Service");
attributes.put(HEART_RATE_MEASUREMENT, "Heart Rate Measurement");
attributes.put("00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name String");
}
public static String getUUID(String uuidKey) {
return attributes.get(uuidKey);
}
}

关于UUID 每个设备都会有很多UUID组,那如何获取他们呢? 这就是接下来我要告诉大家的事情。

BluetoothGattCallback接口获取大家都不陌生,在BluetoothGattCallback接口中的 onServicesDiscovered(BluetoothGatt gatt, int status)方法中 使用gatt对象调用getServices()方法就可以得到这个设备的全部的UUID组了。

代码如下:

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {


@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();

}


@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
mGattServices = gatt.getServices();
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}


@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}


@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);

}


@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
}
};

获取到全部的服务,但是如何获取到服务中的读写特征的UUID呢?

代码如下:

private void getUUID(List gattServices) {
ArrayList> gattServiceData = new ArrayList>();
ArrayList>> gattCharacteristicData = new ArrayList>>();
mGattCharacteristics = new ArrayList>();
for (BluetoothGattService gattService : gattServices) {
HashMap currentServiceData = new HashMap();
uuid = gattService.getUuid().toString();
if (gattService.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY) {
uuid = gattService.getUuid().toString();
}
if (gattService.getType() == BluetoothGattService.SERVICE_TYPE_SECONDARY) {
uuid = gattService.getUuid().toString();
}
currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData);
ArrayList> gattCharacteristicGroupData = new ArrayList>();
List gattCharacteristics = gattService.getCharacteristics();
charas = new ArrayList();
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap currentCharaData = new HashMap();
uuid = gattCharacteristic.getUuid().toString();
if ((BluetoothGattCharacteristic.PROPERTY_READ | gattCharacteristic.getProperties()) > 0) {
// READ set one
uuid = gattCharacteristic.getUuid().toString();;
}
if ((BluetoothGattCharacteristic.PROPERTY_WRITE & gattCharacteristic.getProperties()) > 0) {
// write set one
uuid = gattCharacteristic.getUuid().toString();
}
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
}
mGattCharacteristics.add(charas);
gattCharacteristicData.add(gattCharacteristicGroupData);
setCharacteristicNotification();
}
}

获取到所有的服务UUID与特征UUID。

 

你可能感兴趣的:(Android,BLE,开发)