使用ANDROID BLE api无法写出任何特性

时间:2016-04-29 00:14:14

标签: android bluetooth-lowenergy ibeacon android-bluetooth ibeacon-android

在我的应用程序中,我想为BLE设备(iBeacon)写一些特性。问题是,在我呼叫gatt.executeReliableWrite()后,信标与我的智能手机断开连接并且调用了onConnectionStateChange回调 - 永远不会调用onReliableWriteCompleted方法。当然,这个特征并没有写在Beacon上。所以我的问题是 - 我做错了什么以及如何使用executeRealiableWrite() methodCallback方法将特性写入BLE设备:

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                    mBluetoothGatt.discoverServices();
                    mAlreadyConnected = true;
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                if (mWritingStarted) {
                    reconnectBeacon();
                }
            }
        }

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

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            if (characteristic.getValue() != mCurrentValue) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    gatt.abortReliableWrite();
                } else {
                    gatt.abortReliableWrite(mBluetoothDevice);
                }
            } else {
                boolean result = gatt.executeReliableWrite();
            }
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                mCurrentCharacteristic++;
                writeNextCharacteristic(mCurrentCharacteristic);
            } else {
                //TODO: handle error
            }
        }

我用来写特征的其他方法:

private void connectBeacon() {
        mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
        mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
    }

    private void reconnectBeacon() {
        mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
    }

    private void writeNextCharacteristic(int characteristicPosition) {
        if (characteristicPosition < mCharacteristicsToWrite.size()) {
            UUID serviceUuid = mCharacteristicsToWrite.get(characteristicPosition).getServiceUuid();
            UUID characteristicUuid = mCharacteristicsToWrite.get(characteristicPosition).getCharacteristicUuid();
            byte[] value = mCharacteristicsToWrite.get(characteristicPosition).getValue();
            mCurrentValue = value;
            BluetoothGattCharacteristic gattCharacteristic = mBluetoothGatt.getService(serviceUuid).getCharacteristic(characteristicUuid);
            mBluetoothGatt.beginReliableWrite();
            gattCharacteristic.setValue(value);
            boolean status = mBluetoothGatt.writeCharacteristic(gattCharacteristic);
            Log.d("Status: ", String.valueOf(status));
        } else {
            mWritingStarted = false;
            mBluetoothGatt.close();
            mCharacteristicWriteCallback.onAllCharacteristicsWritten();
        }
    }

0 个答案:

没有答案