Android BLE onCharacteristicChanged没有触发

时间:2016-12-01 14:55:58

标签: java android bluetooth bluetooth-lowenergy

我有一个医疗设备(名为SnoreCoach),我创建了一个Android应用程序并使用此设备进行蓝牙低功耗连接。一切(连接到设备,将数据写入特征等)工作正常,除了OnCharacteristicChanged没有被触发。

以下是我的BluetoothGattCallback:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            broadcastUpdate(ACTION_GATT_CONNECTED);
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            broadcastUpdate(ACTION_GATT_DISCONNECTED);
        }

    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.d(TAG, "onServicesDiscovered - success");
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        }
    }

 @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.d(TAG, "onCharacteristicRead - success");
            broadcastUpdate(ACTION_DATA_AVAILABLE);
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.d(TAG, "onCharacteristicWrite - success");
            if (writeType.equals("unPair")) {
                mBluetoothGatt.close();
                mBluetoothGatt = null;
                currentlyConnectedPeripheral = null;
            }
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
        Log.d(TAG, "onCharacteristicChanged");
        broadcastUpdate(ACTION_DATA_AVAILABLE);
    }

    @Override
    public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
        super.onReliableWriteCompleted(gatt, status);
        Log.d(TAG, "onReliableWriteCompleted");
    }

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

    @Override
    public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
        super.onMtuChanged(gatt, mtu, status);
        Log.d(TAG, "onMtuChanged");
    }
};

以下是" broadcastUpdate"从BluetoothGattCallback上面调用的函数:

private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

在我的通话活动(SettingsActivity)中,我有以下广播接收器:

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        switch (action) {
            case BLEService.ACTION_GATT_CONNECTED:
                displayToast("Connected");
                Log.d(TAG, "Connected");
                mBleService.discoverServices();
                break;
            case BLEService.ACTION_GATT_DISCONNECTED:
                displayToast("Disconnected");
                break;
            case BLEService.ACTION_GATT_SERVICES_DISCOVERED:
                Log.d(TAG, "Services Discovered");
                displayToast("Services Discovered");
                mBleService.enableNotification();
                break;
            case BLEService.ACTION_DATA_AVAILABLE:
                displayToast("Data Received");
                break;
        }
    }
};

以下是我的" enableNotification"功能:

 public void enableNotification() {
    BluetoothGattService snorecoachService = mBluetoothGatt.getService(SNORECOACH_SERVICE_UUID);
    if (snorecoachService == null) {
        Log.d(TAG, "snorecoach Service not found!");
        return;
    }

    BluetoothGattCharacteristic bodyMovementChar = snorecoachService.getCharacteristic(BODY_MOVEMENT_UUID);
    if (bodyMovementChar == null) {
        Log.d(TAG, "charateristic not found!");
        return;
    }

    mBluetoothGatt.setCharacteristicNotification(bodyMovementChar, true);

    BluetoothGattDescriptor desc = bodyMovementChar.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
    desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    mBluetoothGatt.writeDescriptor(desc);
}

以下是流程:

1)在连接时,我发送广播为"已连接"到mGattUpdateReceive,然后我开始服务发现。

2)OnServiceDiscovery,我发送广播为"发现的服务"到mGattUpdateReceive,在这里我打电话给#34;启用notofication"设置" setCharacteristicNotification"的函数,它也写入了所需的描述符。

我已尝试过从其他stackOverflow问题中找到的所有可能选项,但我不知道我在做错了onCharacteristic事件没有被触发。

我花了2天多的时间来解决这个问题,但没有运气,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

如果是您想要的通知而不是指示,请尝试更改

desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);