首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BluetoothGattCallback只调用onConnectionStateChange

BluetoothGattCallback只调用onConnectionStateChange
EN

Stack Overflow用户
提问于 2015-05-15 13:30:28
回答 1查看 8.9K关注 0票数 4

我的device.connectGatt()只触发onConnectionStateChage。状态为0,并建立了连接。我在4.4和5.1系统上测试了它--结果是一样的。这是我的密码:

代码语言:javascript
复制
private final BluetoothGattCallback myCallBack = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);

        //BluetoothGattCharacteristic tempChar = null;
        //tempChar.setValue("test");

        if(status == 0) {
            gatt.connect();
            //gatt.disconnect();


        }
    }

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

    @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);
        // Try to send some data to the device
        characteristic.setValue("test");
        gatt.writeCharacteristic(characteristic);
    }

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


    }

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

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

    }

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

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

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

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public String toString() {
        return super.toString();
    }
};

我在所有调用中添加了一个断点,唯一被击中的是onConnectionStateChange。这个应该是这样工作的吗?我在哪里可以写一个特征或描述,并将其推送到BLE模块?另外,BLE模块(由Arduino控制的HC-10)正在发送数据(用不同的应用程序测试).因此,我希望它也能命中onCharacteristicOnChage方法。我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-15 14:20:21

连接后,Android不知道设备上的任何服务或特性。

因此,必须一旦连接到discoverServices()

代码语言:javascript
复制
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        gatt.discoverServices();
    }
}

一旦您得到回调,就可以编写:

代码语言:javascript
复制
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic writeChar = mBluetoothGatt.getService(myServiceUUID)
                    .getCharacteristic(myWriteCharUUID);
        byte[] data = new byte[10];
        writeChar.setValue(data);
        gatt.writeCharacteristic(writeChar);
    }
}
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30260802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档