gatt.writeDescriptor()之后的Android BLE Gatt超时

时间:2014-07-08 15:14:37

标签: android bluetooth bluetooth-lowenergy android-bluetooth

我目前正在尝试在Android上运行一个BLE应用程序。具体来说,我正在尝试拾取从iOS设备发送的BLE信号。以下是与我的问题相关的代码。

   public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.w(tag, "State: " + newState+"    status: "+ status + "  (0 is success)");
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                String intentAction = "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
                final Intent intent = new Intent(intentAction);
                callbackActivity.sendBroadcast(intent);
                Log.w(tag, "Connected to GATT server.");
                // Attempts to discover services after successful connection.
                Log.w(tag, "Attempting to start service discovery:" +
                        mConnectedGatt.discoverServices());
                // Clear data to prepare for new data reading
                bluetoothData.clear();
            } else {
                Log.w(tag, "GATT connection unsuccessful.");
                restartSearch(gatt);
            }
        }

        /**
         * Called when a service has been discovered in the connected device
         */
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.w(tag, "Services Discovered! "+status);
            if(gatt.getService(SERVICE_UUID)!=null){
                Log.w(tag, "Not null!");
                BluetoothGattCharacteristic characteristic=gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTC_UUID);                       
                gatt.setCharacteristicNotification(characteristic, true);
                BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                        CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                Log.w(tag, "Writing to descriptor: "+mConnectedGatt.writeDescriptor(descriptor));
            }
            else{
                Log.w(tag, "Services discovered unsuccessful");
                restartSearch(gatt);
            }

当我激活两个设备时,Android设备始终拾取连接。连接始终是成功的,并且对discoverServices()的调用总是导致对onServiceDiscovered的回调。找到我正在搜索的特性,并且调用mConnectedGatt.writeDescriptor(描述符)始终返回true。但是,我的onCharacteristicChanged回调永远不会被调用。相反,程序在writeDescriptor调用后等待大约10秒钟,然后我调用onConnectionStateChanged指定连接已丢失。这种情况发生在95%的时间。 5%的时间,一切都运行良好,我接到onCharacteristicChanged的调用。

是否有任何原因导致此超时发生?作为旁注,我相信这是我的Android代码中的问题,因为我可以在iOS设备之间完美地发送和接收此BLE信号。

0 个答案:

没有答案