我英语不好。敬请谅解。我想知道蓝牙设置何时完成。我想在蓝牙连接和设置完成后将数据发送到我的设备。我如何知道流打开何时完成?当我尝试所有 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"); }}
1 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
您可以实现 onConnectionStateChange。这会让您知道连接是否成功。
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// you can send data here
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
}
}
添加回答
举报
0/150
提交
取消