Android BLE传感器服务不可用

时间:2014-09-18 06:52:55

标签: android bluetooth-lowenergy bluegiga

我正在尝试使用 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和charectrestics?

0 个答案:

没有答案