我正在尝试使用Bluegiga BLED 112 设备为 Android 开发 BLE 蓝牙 (SMART) 应用程序。我正在关注 android sdk 示例中给出的 BLE 演示示例代码。
这就是我的 GattCallBack。
// Implements callback methods for GATT events that the app cares about.
// For example,
// connection status has changed, services are discovered,etc...
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
// Called when device has changed connection status and appropriate
// broadcast with device address extra is sent
// It can be either connected or disconnected state
@Override
public void onConnectionStateChange(final BluetoothGatt gatt,
int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS) {
// LogUtils.LOGI("BLE_STATUS", "SUCCESS");
Log.i("BLE service", "onConnectionStateChange - status: "
+ status + " - new state: " + newState);
if (gatt.discoverServices())
LogUtils.LOGI("DISCOVER_SERVC", "STARTED");
} else
// LogUtils.LOGI("DISCOVER_SERVC", "NOT_STARTED");
if (newState == BluetoothProfile.STATE_CONNECTED) {
LogUtils.LOGI("BLE_STATUS", "CON");
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
}
Log.i("BLE service", "onConnectionStateChange - status: " + status
+ " - new state: " + newState);
}
// Called when services are discovered on remote device
// If success broadcast with device address extra is sent
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
LogUtils.LOGI("BLE_STATUS", "SUCCESS");
}
Log.i("BLE service", "onServicesDiscovered - status: " + status);
}
// Called when characteristic was read
// Broadcast with characteristic uuid and status is sent
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
Log.i("BLE service", "onCharacteristicRead - status: " + status
+ " - UUID: " + characteristic.getUuid());
}
// Called when characteristic was written
// Broadcast with characteristic uuid and status is sent
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
Log.i("BLE service", "onCharacteristicWrite - status: " + status
+ " - UUID: " + characteristic.getUuid());
}
// Called when remote device rssi was read
// If success broadcast with device address extra is sent
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
}
Log.i("BLE service", "onReadRemoteRssi - status: " + status);
}
// Called when descriptor was written
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, int status) {
Log.i("BLE service", "onDescriptorWrite - status: " + status
+ " - UUID: " + descriptor.getUuid());
}
// Called when notification has been sent from remote device
// Broadcast with characteristic uuid is sent
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.i("BLE service", "onCharacteristicChanged - status: "
+ " - UUID: " + characteristic.getUuid());
}
};
无论我没有得到任何服务,也没有获得BLE 设备的特性 我在onConnectionstateChange中收到的状态代码是133
尝试过的解决方法:由于我已经知道服务UUID,因此我尝试在 GattCallBack 中获取服务对象,如下所示
BluetoothGattService gattService = gatt.getService(device);
if (gattService == null) {
LogUtils.LOGI("GATT SERVICE", "null :" + gattService);
} else {
LogUtils.LOGI("GATT SERVICE", "not null :" + gattService);
}
但结果总是返回null
我应该怎么做才能获得 Gattservices 和特性?