java - Android低功耗蓝牙BLE写入数据很大几率会失败 求解
ringa_lee
ringa_lee 2017-04-18 09:33:31
[Java讨论组]

1、问题描述

最近进行Android ble的开发,遇到最大的问题就是在往characteristic中写入数据的时候,会有一个成功率抖动的问题,常会出现第一次写入失败,必须再写一次才成功的情况。
每次往characteristic中写入数据的时候,应该要回调onCharacteristicWrite这个方法的,如果失败了,根本就不回调这个方法,导致我甚至无出判断成功还是失败,无法再逻辑代码中去重写,只能在交互界面重复操作。

2、具体代码

下面我贴出我大致代码。

2.1BluetoothGattCallback

private void connectDevice() {
        mDevice.connectGatt(this, false, new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.d("MainActivity", "连接成功");
                    mGatt = gatt;
                    mGatt.connect();
                    gatt.discoverServices();
                }
            }

            @Override
            public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    List<BluetoothGattService> services = gatt.getServices();
                    mGattService = services.get(4);
                    BluetoothGattCharacteristic notificationCharacteristic = mGattService.getCharacteristics().get(0);
                    mWriteCharacteristic = services.get(4).getCharacteristics().get(1);
                    mSimpleIO.setString("notificationUUID", notificationCharacteristic.getUuid().toString());
                    gatt.setCharacteristicNotification(notificationCharacteristic, true);
                }
            }

            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    switch (mCurrState) {
                        case ConstantContainer.VERIFY_DEFAULT_KEY:
                            alterNewKey(gatt, characteristic);
                            break;
                        case ConstantContainer.ALTER_KEY:
                            bondSuccess();
                            break;
                        case ConstantContainer.REMOVE_BOND:
                            mGatt.disconnect();
                            try {
                                if (removeBond(mDevice)) {
                                    mSimpleIO.remove("deviceAddress");
                                    mSimpleIO.remove("notificationUUID");
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            showFindLayout();
                                        }
                                    });
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            break;
                    }
                } else {
                    switch (mCurrState) {
                        case ConstantContainer.VERIFY_DEFAULT_KEY:
                            Log.d("BluetoothActivity", "验证初始密码失败");
                            break;
                        case ConstantContainer.ALTER_KEY:
                            Log.d("BluetoothActivity", "修改密码失败");
                            break;
                        case ConstantContainer.REMOVE_BOND:
                            Log.d("BluetoothActivity", "删除配对失败");
                            break;
                    }
                }
            }

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

上面的代码是BluetoothGattCallback的几个主要回调,可以看到我在onCharacteristicWrite中对他的status做了判断,想要定位写入是否成功,但结果是失败的情况下,并不回调这个方法。

2.2 写入部分

    private void alterNewKey(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        mWriteCharacteristic.setValue("BBTM=" + mBlueKey);
        mCurrState = ConstantContainer.ALTER_KEY;
        Log.d("BluetoothActivity", "gatt.writeCharacteristic(characteristic) alterNewKey:" + gatt.writeCharacteristic(characteristic));
    }

    private void writeDefaultKey(BluetoothGatt gatt, BluetoothGattCharacteristic mWriteCharacteristic) {
        mCurrState = ConstantContainer.VERIFY_DEFAULT_KEY;
        mWriteCharacteristic.setValue(ConstantContainer.DEFAULT_KEY);
        Log.d("BluetoothActivity", "gatt.writeCharacteristic(mWriteCharacteristic) writeDefaultKey:" + gatt.writeCharacteristic(mWriteCharacteristic));
    }
    

这两个方法是我写入的部分,我把它们的返回值打印出来可发现,无论成功失败,返回的都是true。

最后

救命啊,要疯了,球大神解答。

ringa_lee
ringa_lee

ringa_lee

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号