private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices == null || gattServices.isEmpty()) { LogTool.e(TAG, "No Gatt Services Found!"); return; } for (BluetoothGattService gattService : gattServices) { List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); LogTool.d( TAG, "Service UUID = 【" + gattService.getUuid() + "】, Characteristics Size = 【" + (gattCharacteristics == null ? 0 : gattCharacteristics.size()) + "】"); if (gattCharacteristics != null) { for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { LogTool.i(TAG, " Characteristic UUID = 【" + gattCharacteristic.getUuid() + "】"); } } } }
private boolean checkCloudWatchServiceSupported(BluetoothGatt gatt) { BluetoothGattService cloudWatchGattService = gatt.getService(UUID.fromString(BleUuid.SERVICE_DEVICE_INFORMATION)); if (cloudWatchGattService == null) { LogTool.e(TAG, "Cloud Watch Service不支持"); } else { BluetoothGattCharacteristic rfcommCharacteristic = cloudWatchGattService.getCharacteristic( UUID.fromString(BleUuid.CHAR_MANUFACTURER_NAME_STRING)); if (rfcommCharacteristic == null) { LogTool.e(TAG, "RFCOMM BLUETOOTH COMMAND不支持"); } else { gatt.setCharacteristicNotification(rfcommCharacteristic, true); } BluetoothGattCharacteristic heartbeatCharacteristic = cloudWatchGattService.getCharacteristic( UUID.fromString(BleUuid.CHAR_MODEL_NUMBER_STRING)); if (heartbeatCharacteristic == null) { LogTool.e(TAG, "HEARTBEAT OR DISCONNECT COMMAND不支持"); } else { gatt.setCharacteristicNotification(heartbeatCharacteristic, true); } } BluetoothGattService currentTimeGattService = gatt.getService(UUID.fromString(BleUuid.SERVICE_CURRENT_TIME)); if (currentTimeGattService == null) { LogTool.e(TAG, "Current Time Service不支持"); } else { BluetoothGattCharacteristic syncTimeCharacteristic = currentTimeGattService.getCharacteristic(UUID.fromString(BleUuid.CHAR_CURRENT_TIME)); if (syncTimeCharacteristic == null) { LogTool.e(TAG, "SYNCTIME COMMAND不支持"); } else { gatt.setCharacteristicNotification(syncTimeCharacteristic, true); } } return true; }
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); LogTool.d(TAG, "on services discovered success ? " + (status == BluetoothGatt.GATT_SUCCESS)); if (status == BluetoothGatt.GATT_SUCCESS) { displayGattServices(gatt.getServices()); if (gatt.getServices() == null || gatt.getServices().isEmpty()) { LogTool.e(TAG, "No gatt service found!"); onServicesDiscoveredFailed(gatt); return; } if (!checkCloudWatchServiceSupported(gatt)) { LogTool.e(TAG, "Clouder watch services does not support!"); onServicesDiscoveredFailed(gatt); gatt.disconnect(); return; } onServicesDiscoveredSuccess(gatt); } else { LogTool.e(TAG, "Can not found gatt services!"); onServicesDiscoveredFailed(gatt); } }