我想知道BLE gatt设置的完成时间(如回调)

我英语不好。敬请谅解。


我想知道蓝牙设置何时完成。我想在蓝牙连接和设置完成后将数据发送到我的设备。我如何知道流打开何时完成?


当我尝试所有 BLE 设置时,我只是睡了几秒钟,但我想将其更改为顺序代码。


private class GattClientCallback extends BluetoothGattCallback {

@Override

        public void onServicesDiscovered( BluetoothGatt _gatt, int _status ) {

            super.onServicesDiscovered( _gatt, _status );

            // check if the discovery failed

            if( _status != BluetoothGatt.GATT_SUCCESS ) {

                Log.e( TAG, "Device service discovery failed, status: " + _status );

                return;

            }

            // find discovered characteristics

            List<BluetoothGattCharacteristic> matching_characteristics= BluetoothUtils.findBLECharacteristics(_gatt);

            if( matching_characteristics.isEmpty() ) {

                Log.e( TAG, "Unable to find characteristics" );

                return;

            }

            // log for successful discovery

            Log.d( TAG, "Services discovery is successful" );


            // find command characteristics from the GATT server

            cmd_characteristic= BluetoothUtils.findCommandCharacteristic( ble_gatt_ );


            //setCharacteristicNotification

            ble_gatt_.setCharacteristicNotification(cmd_characteristic, true);

            BluetoothGattDescriptor descriptor = cmd_characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);

            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

            ble_gatt_.writeDescriptor(descriptor); //descriptor write operation successfully started?

            // stream open complete.


            // update the connection status message

            msg_handler(SET_TV_STATUS,"connection complete");

        }


}



BIG阳
浏览 158回答 1
1回答

杨__羊羊

您可以实现 onConnectionStateChange。这会让您知道连接是否成功。@Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {&nbsp; &nbsp; if (newState == BluetoothProfile.STATE_CONNECTED) {&nbsp; &nbsp; &nbsp; // you can send data here&nbsp; &nbsp; } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java